四则运算
随机生成0~99的两个数进行四则运算
要注意除法分母为0的情况
预计用时30分钟
实际用时60分钟
代码:
#include <stdio.h>
#include <stdlib.h>#include <time.h>char OperatorChar() { int op; op=rand()%4; switch(op) { case 0: return '+'; case 1: return '-'; case 2: return '*'; case 3: return '/'; }}int Calculation(int a,char op,int b) { switch(op) { case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': return (int)a/b; }}main(){ int a; int b; int Answer; char op; srand((unsigned)time(NULL)); printf("四则运算练习(结果取整数):(输入-10000结束程序)\n"); while(Answer!=-1000) { op=OperatorChar(); a=rand()%100; b=rand()%100; if((b==0)&&(op=='/')) { break; } printf("%d%c%d=",a,op,b); scanf("%d",&Answer); if(Answer==Calculation(a,op,b)) { printf("Right!\n"); } else { printf("Wrong!\n"); } }}