1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #include <time.h> 5 #define eps 1e-6 6 int main() 7 { 8 srand(time(NULL)); 9 int a,b; 10 char op[5] = "+-*/"; 11 int index; 12 /* 计算 */ 13 printf("除法精确到小数点后2位,四舍五入!\n"); 14 while(1) 15 { //随机操作数 16 a = rand()%101; 17 b = rand()%100+1; 18 printf("请选择(输入数字):1.加法 2.减法 3.乘法 4.除法 5.随机 6.退出\n" ); 19 // 1.随机、退出、+-*/ 20 scanf("%d",&index); 21 if(5==index) 22 index = rand()%4; 23 else if(6==index) 24 exit(0); 25 else 26 index--; 27 // 2.题目、答案输入 28 printf("%d%c%d=",a,op[index],b); 29 double answer, result; 30 scanf("%lf",&answer); 31 // 3.判题 32 switch(op[index]){ 33 case '+': 34 result = a+b; break; 35 case '-': 36 result = a-b; break; 37 case '*': 38 result = a*b; break; 39 case '/': 40 if(b==0)printf("除数不可以为零\n");//b已经处理过了 41 else { 42 int r = ((double)a/b+0.005)*100;//4舍5入保留2位 43 result = (double)r/100; 44 } 45 break; 46 } 47 if(fabs(result - answer)<=eps) 48 printf("对\n"); 49 else 50 printf("错\n"); 51 printf("正确答案:%.2f\n", result); 52 } 53 return 0; 54 }