实验二
#include <iostream> #include <math.h> #include <time.h> using namespace std; int c1=0,c2=0; int display1(int k,int x,int y,int m,int shuzhi) //含有加减乘除法 { double jieguo; if(m==0) { cout<<"("<<k<<")"<<x<<" + "<<y<<" = "; cin>>jieguo; if(jieguo==x+y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } if(m==1) { cout<<"("<<k<<")"<<x<<" - "<<y<<" = "; cin>>jieguo; if(jieguo==x-y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } if(m==2) { cout<<"("<<k<<")"<<x<<" x "<<y<<" = "; cin>>jieguo; if(jieguo==x*y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } if(m==3) { if(y==0) { do { while(y!=0) { y=rand()%(shuzhi+1); } }while( y==0 ); } else { cout<<"("<<k<<")"<<x<<" / "<<y<<" = "; cin>>jieguo; if(jieguo==x*y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } } } int display2(int k,int x,int y,int m,int shuzhi) //含有加减 { double jieguo; if(m==0) { cout<<"("<<k<<")"<<x<<" + "<<y<<" = "; cin>>jieguo; if(jieguo==x+y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } if(m==1) { cout<<"("<<k<<")"<<x<<" + "<<y<<" = "; cin>>jieguo; if(jieguo==x+y) { cout<<" 回答正确!"<<endl; return c1++; } else { cout<<" 回答错误!"<<endl; return c2++; } } } int main() { int number; //number表示订制的题的份数 int xuanze; //xuanze用来做判断是否有乘除法,0表示无,1表示有 int shuzhi; //shuzhi表示参与运算的数值范围,比如0-100等 int fushu; //fushu表示运算结果是否存在负数的情况,0表示无,1表示有 //在自然数中,只有减法结果可能有负数存在 int yushu; //fushu表示运算结果有无余数,0表示无,1表示有 int geshu; //geshu表示每行打印的运算式的个数 int a[30],b[30],m1[30],m2[30]; srand( (unsigned)time( NULL ) ); cout<<endl; cout<<" $ 欢迎来到答题订制系统 $"<<endl; cout<<endl; cout<<" 在负数和余数的判断时,0表示无,1表示有,其他数值则默认为1"<<endl; cout<<"Start:"<<endl; cout<<" 请输入订制的份数:"; cin>>number; cout<<endl; cout<<" 请输入数值范围:0-"; cin>>shuzhi; cout<<endl; for(int k=1;k<=number;k++) { cout<<"-----------------------------------------------------------------------------"<<endl; cout<<endl; cout<<"第"<<k<<"份题如下:"<<endl; cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<" 是否含有乘除法(0表示无,1表示有),请选择: "<<endl; cout<<endl; cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<"我的选择:"; cin>>xuanze; cout<<endl; if(xuanze==1) { cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<" 1 是否含可能有负数的结果(0表示无,1表示有),请输入你的选择:"<<endl; cout<<endl; cout<<endl; cout<<" 2 是否含可能有余数的结果(0表示无,1表示有),请输入你的选择:"<<endl; cout<<endl; cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<"对1的选择(0/1):"; cin>>fushu; cout<<endl; cout<<"对2的选择(0/1):"; cin>>yushu; cout<<endl; cout<<"-----------------------------------------------------------------------------"<<endl; cout<<endl; for(int i=1;i<=30;i++) { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); m1[i]=rand()%4; for(int j=1;j<=i-1;j++) { if(a[i]==a[j]&&b[i]==b[j]&&m1[i]==m1[j]) { do { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); }while(a[i]==a[j]&&b[i]==b[j]&&m1[i]==m1[j]); } } if((fushu==1&&m1[i]==1)||(yushu==1&&m1[i]==3)) { display1(i,a[i],b[i],m1[i],shuzhi); } else if(yushu==0&&m1[i]==3) { if(a[i]%b[i]==0) { display1(i,a[i],b[i],m1[i],shuzhi); } else { do { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); while(b[i] == 0) { b[i]=rand()%(shuzhi+1); } }while( a[i]%b[i]!=0 ); display1(i,a[i],b[i],m1[i],shuzhi); } } else if(fushu==0&&m1[i]==1) { if(a[i]>=b[i]) { display1(i,a[i],b[i],m1[i],shuzhi); } else { do { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); }while(a[i]<b[i]); display1(i,a[i],b[i],m1[i],shuzhi); } } else { display1(i,a[i],b[i],m1[i],shuzhi); } if(i%geshu==0) { cout<<endl; } if(i==30) { cout<<endl; } } } else if(xuanze==0) //没有乘除法存在,只需要判断有无负数结果产生 { cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<" 是否含可能有负数的结果(0表示无,1表示有),请输入你的选择:"<<endl; cout<<endl; cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<"我的选择:"; cin>>fushu; cout<<"-----------------------------------------------------------------------------"<<endl; cout<<endl; for(int i=1;i<=30;i++) { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); m2[i]=rand()%2; for(int j=1;j<=i-1;j++) { if(a[i]==a[j]&&b[i]==b[j]&&m2[i]==m2[j]) { do { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); }while(a[i]==a[j]&&b[i]==b[j]&&m2[i]==m2[j]); } } if(fushu==0) { if(a[i]>=b[i]) { display2(i,a[i],b[i],m2[i],shuzhi); } else //当没有负数结果时,应是a>=b,条件不满足时,则系统再随机生成俩随机数,条件满足时进行输出 { do { a[i]=rand()%(shuzhi+1); b[i]=rand()%(shuzhi+1); }while(a[i]<b[i]); display2(i,a[i],b[i],m2[i],shuzhi); } } else { display2(i,a[i],b[i],m2[i],shuzhi); } if(i%geshu==0) { cout<<endl; } if(i==30) { cout<<endl; } } } else { cout<<"*****************************************************************************"<<endl; cout<<endl; cout<<" 输入错误,请重新输入!"<<endl; cout<<endl; cout<<"*****************************************************************************"<<endl; cout<<endl; k=k-1; } cout<<endl; cout<<"正确题数为:"<<c1<<"\t"<<"错误题数为:"<<c2<<endl; cout<<"-----------------------------------------------------------------------------"<<endl; cout<<endl; cout<<"--------------$----------------$---------------$--------------$--------------"<<endl; } cout<<endl; cout<<" $ 感谢您使用答题订制系统,欢迎下次再来 $"<<endl; cout<<endl; return 0; }
伙伴博客:http://www.cnblogs.com/10Watson/