让程序能接受用户输入答案,并判定对错。最后给出总共对/错的数量
//随机四则运算:题目避免重复可定制数量,数值范围、加减有无负数、除法有无余数等 #include<iostream> #include<time.h> using namespace std; void basic(int num,int cc,int mhs,int kgs,int pyu,int max,int nnum) //基础四则运算函数 { srand(time(NULL)); int a[100]; //存放第一个操作数的数组 int b[100]; //存放第二个操作数的数组 int c[100]; //存放运算符的数组 int n,o,p,q,m,x=1; int repet; //控制是否重复 int d,e,f,g,sum=0; for(int i=0;i<num;i++) { n=rand()%max; a[i]=n; o=rand()%max; b[i]=o; p=rand()%cc; c[i]=p; q=rand()%2; m=rand()%2; while(nnum) //有无负数 { switch(q) { case(0): a[i]=-a[i]; break; case(1): break; } break; } while(nnum) { switch(m) { case(0): b[i]=-b[i]; break; case(1): break; } break; } repet=1; for(int j=0;j<i;j++) { if(a[i]==a[j]&&b[i]==b[j]&&c[i]==c[j]) //判断是否有重复 { i=i-1; repet=0; } } while(repet) { if(i%mhs==0) { cout<<endl; } switch(c[i]) { case(0): { cout<<"("<<i+1<<") "<<a[i]<<"+ "<<b[i]<<"="; cin>>d; if(d==a[i]+b[i]) sum++; } break; case(1): { cout<<"("<<i+1<<") "<<a[i]<<"- "<<b[i]<<"="; cin>>e; if(e==a[i]-b[i]) sum++; } break; case(2): { cout<<"("<<i+1<<") "<<a[i]<<"* "<<b[i]<<"="; cin>>f; if(f==a[i]*b[i]) sum++; } break; case(3): { if(b[i]==0) //判断被除数是否为0,是0则不输出 { i=i-1; break; } else { while(pyu) { if(a[i]%b[i]!=0) { i=i-1; x=0; break; } } while(x) { cout<<"("<<i+1<<") "<<a[i]<<"/ "<<b[i]<<"="; cin>>g; if(g==a[i]/b[i]) sum++; } break; } } } for(int k=0;k<kgs;k++) { cout<<" "; } break; } } cout<<endl; cout<<"共答对"<<sum<<"道题目"<< endl; } void main() { int num1,cc1,m,n; int mhs1,kgs1,pyu1,max1,nnum1; while(n) { cout<<" 随机四则运算"<<endl; cout<<"--------------------------------------------------------"<<endl; cout<<"1、请输入运算题的数目:"; cin>>num1; cout<<endl; while(1) { cout<<"2、有乘除法请输入0,没有输入1:"; cin>>m; cout<<endl; if(m==0) { cc1=4; break; } else if(m==1) { cc1=2; break; } else cout<<" 输入有误,请重新输入:1代表有乘除法,2代表无乘除法:"; } if(m==0) { cout<<" 请选择除法是否有余数,有余数则输入0,没有余数输入1:"; cin>>pyu1; cout<<endl; } cout<<"3、请输入是否有负数(0代表无负数,1代表有负数):"; cin>>nnum1; cout<<endl; cout<<"4、请输入数值范围(最大值,默认从0开始,若有负数则从最大值的负数开始):"; cin>>max1; cout<<endl; cout<<"5、请输入打印方式:每行运算题的个数(建议3或4):"; cin>>mhs1; cout<<endl; cout<<" 每列运算题之间的间隔(建议1或2):"; cin>>kgs1; cout<<endl; cout<<"结果为:"; basic(num1,cc1,mhs1,kgs1,pyu1,max1,nnum1); cout<<endl; cout<<"继续请输入1,退出输入0:"; cin>>n; cout<<endl; } }