睡帮帮忙把下面c代码 转换成c++代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
char chose(int choice)
{
char c;
switch (choice)
{
case 1:c = '+';break;
case 2:c = '-';break;
case 3:c = '*';break;
case 4:c = '/';break;
default:
break;
}
return c;
}
int main()
{
char name[10] = "";
double num = 0.0;
int choice = 0;
char c;
float result[10];
int i;
int count = 0;
float a,b;
printf("现在进行考试:\n");
printf("请输入姓名:");
gets(name);
printf("请输入考号:");
scanf("%lf",&num);
printf("请选择要进行考试的运算(1:加法,2:减法,3:乘法,4:除法)");
scanf("%d",&choice);
c = chose(choice);
printf("考试要进行10道运算,每题10分,请计算:\n");
srand((unsigned)time(NULL));
for(i = 0;i<10;i++)
{
a = rand() % 20;
Sleep(100);
b = rand() % 20 + 1;
printf("(%d) %.0f %c %.0f = ",i + 1,a,c,b);
scanf("%f",&result[i]);
if(c == '+')
{
if(result[i] == a + b)
{
count++;
}
}
if(c == '-')
{
if(result[i] == a - b)
{
count++;
}
}
if(c == '*')
{
if(result[i] == a * b)
{
count++;
}
}
if(c == '/')
{
if(result[i] == a - b)
{
count++;
}
}
}
printf("考试结束!\n");
printf("%s同学(考号:%.0f) 你共做对了%d道题,做错了%d道题!得分%d分。正确率:%d%%",name,num,count,10-count,count * 10,count * 10);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
char chose(int choice)
{
char c;
switch (choice)
{
case 1:c = '+';break;
case 2:c = '-';break;
case 3:c = '*';break;
case 4:c = '/';break;
default:
break;
}
return c;
}
int main()
{
char name[10] = "";
double num = 0.0;
int choice = 0;
char c;
float result[10];
int i;
int count = 0;
float a,b;
printf("现在进行考试:\n");
printf("请输入姓名:");
gets(name);
printf("请输入考号:");
scanf("%lf",&num);
printf("请选择要进行考试的运算(1:加法,2:减法,3:乘法,4:除法)");
scanf("%d",&choice);
c = chose(choice);
printf("考试要进行10道运算,每题10分,请计算:\n");
srand((unsigned)time(NULL));
for(i = 0;i<10;i++)
{
a = rand() % 20;
Sleep(100);
b = rand() % 20 + 1;
printf("(%d) %.0f %c %.0f = ",i + 1,a,c,b);
scanf("%f",&result[i]);
if(c == '+')
{
if(result[i] == a + b)
{
count++;
}
}
if(c == '-')
{
if(result[i] == a - b)
{
count++;
}
}
if(c == '*')
{
if(result[i] == a * b)
{
count++;
}
}
if(c == '/')
{
if(result[i] == a - b)
{
count++;
}
}
}
printf("考试结束!\n");
printf("%s同学(考号:%.0f) 你共做对了%d道题,做错了%d道题!得分%d分。正确率:%d%%",name,num,count,10-count,count * 10,count * 10);
return 0;
}
