随机运算,怎么考虑当除法运算的时候b不等于0的情况
#include<stdio.h>#include<stdlib.h> //rand()
#include <time.h> //
int main()
{
time_t t;
int randint,flag=1;
float x;
double result,p;
int optr,a,b,c=0,i=1;
char array[4]={'+','-','*','/'};
srand( time(&t));
printf("10以内的四则运算,提示:若答案有小数,请保留小数点后两位");
while(flag==1)
{
randint = rand();
a=randint%10;
randint = rand();
b=randint%10;
randint = rand();
randint=randint%4;
optr=array[randint];
printf("\n第%d题 : %d %c %d = ", i,a,optr,b);
scanf("%f",&x);
switch(optr)
{
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
if(b==0)
{
while(b!=0)
b=randint%10;
}
result=(double)a/b;
break;
}
if(result==x)
{
printf("正确!");
c++;
printf("\n若继续请输入1,结束请输入0:");
scanf("%d",&flag);
while(flag!=0&&flag!=1)
{
printf("\n你的输入有误,请重新输入:");
scanf("%d",&flag);
}
}
else
{
printf("结果错误,答案为: %.2f ",result);
printf("\n若继续请输入1,结束请输入0:");
scanf("%d",&flag);
while(flag!=0&&flag!=1)
{
printf("你的输入有误,请重新输入:");
scanf("%d",&flag);
}
}
i++;
}
//统计结果
p=((float)c/(i-1))*100; //正确率
printf("\n\n总共%d道题,答对%d题,正确率为%%%.2f\n",i-1,c,p);
return 0;
}