一、作业要求
- 分别能够实现小学一、二、三、四、五年级的四则运算要求, 逐步实现各个年级的难度
- 要求能够通过输入来选择不同年级,每个年级还得区分难,中,易三个等级
- 对于三、四、五年级需要支持括号与多个运算符
- 程序支持判断对错及累计得分与时间
- 一次可以出100道题目,而且不能重复(比如2+3 与 3+2 算重复的)
- 充分发挥想象增加满足小学生数学检测需要的功能
二、原代码及运行结果
#include<iostream> #include<stdarg.h> #include<stdlib.h> #include<time.h> #include<math.h> #include<fstream> using namespace std; int main() { ofstream Savefile; Savefile.open("cpp-home.txt"); cout<<endl; cout<<"中小学四则运算在线答题系统"<<endl; cout<<endl; cout<<endl; Again: double Q1=1; int Q01; int Q2=1; int Q3=100; char Q4='y'; int Q5=1; char Q6='n'; char Q7='n'; double ans=0; int right=0; cout<<" 输入题目数量:"; cin>>Q1; Q01=floor(Q1); // cout<<"输入每行打印题目数(1-5):"; // cin>>Q2; cout<<" 每行间距(正整数):"; cin>>Q5; cout<<" 请输入算式中数值的最大值:"; cin>>Q3; while(1) { cout<<" 是否为三年级以上学生?是:y;不是:n"<<endl; cin>>Q4; cout<<" 是否为初中生?是:y;不是:n"<<endl; cin>>Q7; cout<<"-------------------------------------------------------------------------"<<endl; cout<<"开始进行答题:"<<endl; srand(time(NULL)); if(Q4=='y') { while(1) { if(Q1<1) { cout<<"输入有误,请重新输入题目数量:"; cin>>Q1; Q01=floor(Q1); } else { for(int j=0;j<Q01;j++) { if(j!=0&&j%Q2==0) { for(int i=0;i<Q5;i++) { cout<<endl; } } int num1=rand()%Q3; int num2=rand()%Q3; int sign=rand()%4; switch(sign) { case 0: cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<" "; cin>>ans; if(ans==num1+num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; Savefile<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; cout<<"正确答案是:"<<num1+num2<<endl; Savefile<<"正确答案是:"<<num1+num2<<endl; } break; case 1: if(Q7=='y') { cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" "; cin>>ans; if(ans==num1-num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1-num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1-num2<<endl; } } else { if(num1>num2) { cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" "; cin>>ans; if(ans==num1-num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1-num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1-num2<<endl; } } else { cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<" "; cin>>ans; if(ans==num2-num1) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num2-num1<<endl; Savefile<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num2-num1<<endl; } } } break; case 2: cout<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<" "; cin>>ans; if(ans==num1*num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1*num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"*"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1*num2<<endl; } break; case 3: if(num2!=0) { cout<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<" "; cin>>ans; if(ans==num1/num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1/num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"/"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1/num2<<endl; } } else { j--; } break; } } break; } } cout<<"共答对"<<right<<"道题;"<<"答错"<<Q01-right<<"道题。"; break; } if(Q4=='n') { while(1) { if(Q01<1) { cout<<"输入有误,请重新输入题目数量:"; cin>>Q1; Q01=floor(Q1); } else { for(int j=0;j<Q01;j++) { if(j!=0&&j%Q2==0) { for(int i=0;i<=Q5;i++) { cout<<endl; } } int num1=rand()%Q3; int num2=rand()%Q3; int sign=rand()%2; switch(sign) { case 0: cout<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<" "; cin>>ans; if(ans==num1+num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1+num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"+"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1+num2<<endl; } break; case 1: if(Q7=='y') { cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" "; cin>>ans; if(ans==num1-num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1-num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1-num2<<endl; } break; } else { if(num1>num2) { cout<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<" "; cin>>ans; if(ans==num1-num2) { cout<<"正确"<<endl; right++; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num1-num2<<endl; Savefile<<j+1<<":"<<" "<<num1<<"-"<<num2<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num1-num2<<endl; } break; } else { cout<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<" "; cin>>ans; if(ans==num2-num1) { cout<<"正确"; } else { cout<<"答错了!"<<endl; cout<<"正确答案是:"<<num2-num1<<endl; Savefile<<j+1<<":"<<" "<<num2<<"-"<<num1<<"="<<ans<<" "; Savefile<<"答错了"<<endl; Savefile<<"正确答案是:"<<num2-num1<<endl; } break; } } break; } } } break; } cout<<"共答对"<<right<<"道题;"<<"答错"<<Q01-right<<"道题。"<<endl; break; } else { cout<<"输入有误,请按要求输入!"<<endl; } } cout<<endl; while(1) { cout<<"是否继续?(y:继续;n:退出)"; cin>>Q6; if(Q6=='y') { goto Again; } if(Q6=='n') { goto Exit; } else { cout<<"输入有误,请重新输入:"; } } Savefile.close(); Exit: // SaveFile.close(); return 0; }
p.s 如何产生对应题目的txt文件:http://blog.sina.com.cn/s/blog_48d2efee010003k2.html
三、托管于gitee上的链接
1.git如何建立本地仓库:https://blog.csdn.net/sclibingqing/article/details/81179470
2.如何使用gitte代码托管仓库:https://www.jianshu.com/p/2a03c225ea93
3.托管于gitee上的链接:https://gitee.com/sun_si_jiao/firstwarehous.git