练习1——四则运算

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{   
int a,b,c,d,i,j=0,n;   printf("请输入题数:");   scanf("%d",&n); //n选择产生题数   srand(time(NULL));   for(i=1;i<=n;i++)   {     b=rand()%100; //产生一个随机数     c=rand()%100;     a=rand()%4;     switch(a)     {       case 0:
        printf(
"%d+%d=",b,c);         scanf("%d",&d);         if(d==b+c){           printf("正确\n");           j++;} //判断对错,累计答对题数         else printf("错误\n");         break;       case 1:       if(b>=c){ //确保不出现负数         printf("%d-%d=",b,c);         scanf("%d",&d);         if(d==b-c){           printf("正确\n");           j++;}         else printf("错误\n");         break;}       else if(b<c){         printf("%d-%d=",c,b);         scanf("%d",&d);         if(d==c-b){           printf("正确\n");           j++;}         else printf("错误\n");        break;}       case 2:       printf("%d*%d=",b,c);       scanf("%d",&d);       if(d==b*c){         printf("正确\n");         j++;}       else printf("错误\n");       break;       case 3:       printf("(精确为整数)%d/%d=",b,c);       scanf("%d",&d);       if(d==b/c){         printf("正确\n");         j++;}       else printf("错误\n");       break;      }
    }    printf(
"\n恭喜,%d题答对了%d题!",n,j);
}

本次练习是一个较为简单的小程序,花费40分钟完成。但是功能仍然很不完善,有很多不足,如除法不能取余数,只能是简单的2个整数的四则运算,没有括号等优先级问题等!

原文地址:https://www.cnblogs.com/dbssb/p/4369464.html