四则运算二
要求:
悲催的二柱子接到了老师要求给软件增加一些小小的功能,具体要求如下:
1、除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性);
2、一次出的题目避免相互重复;
3、可定制出题的数量。
设计思想:
程序的主要设计思想为用字符串数组保存生成的运算题,将操作数采用单独算法处理,然后进行类型转换和操作符一起存入数组中,鉴于字符串的特性,可以对字符串的长度进行随意添加而不必考虑长度问题,最后进行字符串数组的输出产生客户要求的运算题;
#include<stdio.h> #include<stdlib.h> #include<time.h> int A(); int B(); int C(); int A() {int m,n,r,num1,num2,num3; srand((unsigned)time(NULL)); m=rand()%100+1; n=rand()%100+1; if (m<n) {r=m;m=n;n=r;} char op_lib[4]={'+','*','/','-'}; int rand_oper_idx=rand()%4; char cur_oper=op_lib[rand_oper_idx]; if(cur_oper=='+')num1=m+n; else if(cur_oper=='*')num1=m*n; else if(cur_oper=='/')num1=m/n; else (cur_oper=='-')num1=m-n; printf ("%d,%c,%d=\n",m,cur_oper,n); scanf(“%d”,&num2); if (num2==num1) {printf("答案正确”);num3=1;} else {printf("答案错误");num3=0;} } int B() {double m,n,r,num1,num2;int num3; m=rand()/(double)(RAND_MAX); n=rand()/(double)(RAND_MAX); if (m<n) {r=m;m=n;n=r;} char op_lib[4]={'+','*','/','-'}; int rand_oper_idx=rand()%4; char cur_oper=op_lib[rand_oper_idx]; if(cur_oper=='+')num1=m+n; else if(cur_oper=='*')num1=m*n; else if(cur_oper=='/')num1=m/n; else (cur_oper=='-')num1=m-n; printf ("%lf,%c,%lf=\n",m,cur_oper,n); scanf(“%lf”,&num2); if (num2==num1) {printf("答案正确”);num3=1;} else {printf("答案错误");num3=0;} } int C() {double m,r,num1,num2,num3;int n; srand((unsigned)time(NULL)); m=rand()/(double)(RAND_MAX); n=rand()%100+1; if (m<n) {r=m;m=n;n=r;} char op_lib[4]={'+','*','/','-'}; int rand_oper_idx=rand()%4; char cur_oper=op_lib[rand_oper_idx]; if(cur_oper=='+')num1=m+n; else if(cur_oper=='*')num1=m*n; else if(cur_oper=='/')num1=m/n; else (cur_oper=='-')num1=m-n; printf ("%d,%c,%d=\n",m,cur_oper,n); scanf(“%d”,&num2); if (num2==num1) {printf("答案正确”);num3=1;} else {printf("答案错误");num3=0;} } int main() {int num4,num5,num6,num7=0; printf("请输入题目数量”); scanf("%d",&num4); while(num7<num4) {srand((unsigned)time(NULL)); num5=rand()%100+1; if (num5<34) num6=A(); else if(num5>=34&&num5<=67) num6=B(); else(num5>=68&&num5<=100) num6=C(); } num7++; return 0; }