将下列代码改成用c++写void sub()//个位数减法 { int rand1 , rand2 , i , k , sum=0 ; float answer ; printf("请输入挑战的题量:");//k输入的题目数量 scanf("%d",&k); getchar(); if(k>=9999||k<0) { printf("输入错误!请重新输入!\n"); printf("请输入挑战的题量:");//k输入的题目数量 scanf("%d",&k); } for( i=1 ; i<=k ; i++) { srand ((unsigned)time(0)); loop: { rand1=(rand()%10); rand2=(rand()%10); } if(rand1>=rand2) { printf("%d-%d=",rand1,rand2); scanf("%f",&answer); getchar(); if(k>=9999||k<0) { printf("答案错误,请输入正确答案:\n"); printf("%d-%d=",rand1,rand2); scanf("%f",&answer); } if(answer!=(rand1-rand2)) //错一题就减1 { sum--; } while(answer!=(rand1-rand2)) { printf("答案错误,请输入正确答案:\n"); printf("%d-%d=",rand1,rand2); scanf("%f",&answer); getchar(); if(k>=9999||k<0) { printf("答案错误,请输入正确答案:\n"); printf("%d-%d=",rand1,rand2); scanf("%f",&answer); } } sum++; printf("回答正确!\n"); } else goto loop ; } printf("答对题数:%d\t答错题数:%d\t一次答对率为:%.2f%%\n",sum,(k-sum),(sum/(float)k*100)); if((sum/(float)k)>=0.6) //晋级 { printf("恭喜您晋级了!\n"); sub1(); } else { printf("抱歉您未能及格!\n"); tc(); } }
时间: 2023-06-15 20:06:43 浏览: 130
void sub() //个位数减法
{
int rand1, rand2, i, k, sum = 0;
float answer;
printf("请输入挑战的题量:");//k输入的题目数量
scanf("%d", &k);
getchar();
if (k >= 9999 || k < 0)
{
printf("输入错误!请重新输入!\n");
printf("请输入挑战的题量:");//k输入的题目数量
scanf("%d", &k);
}
for (i = 1; i <= k; i++)
{
srand((unsigned)time(0));
loop:
{
rand1 = (rand() % 10);
rand2 = (rand() % 10);
}
if (rand1 >= rand2)
{
printf("%d-%d=", rand1, rand2);
scanf("%f", &answer);
getchar();
if (k >= 9999 || k < 0)
{
printf("答案错误,请输入正确答案:\n");
printf("%d-%d=", rand1, rand2);
scanf("%f", &answer);
}
if (answer != (rand1 - rand2)) //错一题就减1
{
sum--;
}
while (answer != (rand1 - rand2))
{
printf("答案错误,请输入正确答案:\n");
printf("%d-%d=", rand1, rand2);
scanf("%f", &answer);
getchar();
if (k >= 9999 || k < 0)
{
printf("答案错误,请输入正确答案:\n");
printf("%d-%d=", rand1, rand2);
scanf("%f", &answer);
}
}
sum++;
printf("回答正确!\n");
}
else
goto loop;
}
printf("答对题数:%d\t答错题数:%d\t一次答对率为:%.2f%%\n", sum, (k - sum), (sum / (float)k * 100));
if ((sum / (float)k) >= 0.6) //晋级
{
printf("恭喜您晋级了!\n");
sub1();
}
else
{
printf("抱歉您未能及格!\n");
tc();
}
}
阅读全文