单元测试
设计思想:
.在结构方面,将题目拆分开来,一共有四个部分,首先是只有加减法,其次是既有加减法也有乘除法,再其次是加减乘除都有,还有括号,最后是加减乘除没有括号。
程序代码:
#include "stdafx.h" #include "iostream.h" #include "stdlib.h" #include "time.h" #include "Caculation.h" #include "iomanip.h" //*****判断回答是否正确*****bool RightOrWrong(Caculation &Q,int answer) { if(Q.sign==0) { Q.answer=Q.num1+Q.num2; } else if(Q.sign==1) { Q.answer=Q.num1-Q.num2; } else if(Q.sign==2) { Q.answer=Q.num1*Q.num2; } else Q.answer=Q.num1/Q.num2; if(answer==Q.answer) return true; else return false; }//******重新生成算术题******void ReBuild(Caculation &Q) { if(para[4]==1) Q.sign=rand()%4; else Q.sign=rand()%2; Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } //*****输出函数*****void Display1(LinkC C,Caculation &Q) { int temp,count=0; //count记录重新生成题目的次数 for(int i=1;i<=para[1];i++) { cout<<"("<<i<<")"; if(para[4]==1) Q.sign=rand()%4; //有乘除法 else Q.sign=rand()%2; //没有乘除法 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; //随机的(下限~上限)以内的整数 Q.num2=rand()%(para[6]-para[5]+1)+para[5]; RB: ReBuild(Q); //检查是否有出过的题目 if(count>((para[6]-para[5]+1)*(para[6]-para[5]+1)*100)) { cout<<endl<<"该难度的题目已出完,请更改出题设置!"<<endl; break; } switch(Q.sign) { case 0: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"+"<<Q.num2<<"="<<setw(5); break; case 1: if((para[7]==0)&&(Q.num1<Q.num2)) { //若为负数,则交换 temp=Q.num1; Q.num1=Q.num2; Q.num2=temp; } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"-"<<Q.num2<<"="<<setw(5);break; case 2: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"*"<<Q.num2<<"="<<setw(5);break; case 3: while(Q.num2==0) Q.num2=rand()%(para[6]-para[5]+1)+para[5]; if(!para[8]) { while((Q.num1%Q.num2)!=0||Q.num2==0) { //重新生成 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"/"<<Q.num2<<"="<<setw(5);break; } InsertQues(C,Q); if(i%para[2]==0) //一行打印完规定列数,换行 for(int j=0;j<=para[3];j++) cout<<endl; } cout<<endl<<endl; }//*****回答题目函数******void Display2(LinkC C,Caculation &Q) { int temp,count=0,answer=0,right=0; //count记录重新生成题目的次数 for(int i=1;i<=para[1];i++) { cout<<"("<<i<<")"; if(para[4]==1) Q.sign=rand()%4; //有乘除法 else Q.sign=rand()%2; //没有乘除法 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; //随机的(下限~上限)以内的整数 Q.num2=rand()%(para[6]-para[5]+1)+para[5]; RB: ReBuild(Q); //检查是否有出过的题目 if(count>((para[6]-para[5]+1)*(para[6]-para[5]+1)*100)) { cout<<endl<<"该难度的题目已出完,请更改出题设置!"<<endl; break; } switch(Q.sign) { case 0: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"+"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } else cout<<"\t×"; break; case 1: if((para[7]==0)&&(Q.num1<Q.num2)) { //若为负数,则交换 temp=Q.num1; Q.num1=Q.num2; Q.num2=temp; } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"-"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } else cout<<"\t×"; break; case 2: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"*"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } cout<<"\t×"; break; case 3: while(Q.num2==0) Q.num2=rand()%(para[6]-para[5]+1)+para[5]; if(!para[8]) { while((Q.num1%Q.num2)!=0||Q.num2==0) { //重新生成 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"/"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } cout<<"\t×"; break; } InsertQues(C,Q); cout<<endl; } cout<<"共回答正确"<<right<<"道题。"; cout<<endl<<endl; } //*****判断难度*****void YesOrNo(int para) { if(para) cout<<"是"; else cout<<"否"; }//*****查看设置*****void ShowSetting() { system("cls"); cout<<"\t/*************四则运算出题系统*************/"<<endl; cout<<"\t题目数量:"<<para[1]<<"个\t\t打印列数:"<<para[2]<<"列"<<endl; cout<<"\t每行间隔:"<<para[3]<<"行\t\t是否有乘除法:";YesOrNo(para[4]);cout<<endl; cout<<"\t数值范围下限:"<<para[5]<<"\t\t数值范围上限:"<<para[6]<<endl; cout<<"\t是否有负数:";YesOrNo(para[7]);cout<<"\t\t是否有余数:";YesOrNo(para[8]);cout<<endl; cout<<"\t/******************************************/"<<endl; }//*****设置打印方式*****void SetPrint() { system("cls"); char move1; cout<<"\t/*************设置打印方式*************/"<<endl; cout<<"\t 0.设置打印列数("<<para[2]<<"列)"<<endl; cout<<"\t 1.设置每行间隔("<<para[3]<<"行)"<<endl; cout<<"\t 2.返回主菜单"<<endl; cout<<"\t/**************************************/"<<endl; cout<<"请选择后续操作(0~2):"; cin>>move1; while(move1<'0'||move1>'2') { cout<<"错误!请正确输入操作序号(0~2):"; cin>>move1; } switch(move1) { case '0': reset2: cout<<"新的打印列数(建议7列以内):"; cin>>para[2]; if(para[2]<0) { cout<<"出错!请重新输入!"<<endl; goto reset2; } break; case '1': reset3: cout<<"新的间隔行数:"; cin>>para[3]; if(para[3]<0) { cout<<"出错!请重新输入!"<<endl; goto reset3; } break; case '2':break; } }//*****判断输入正确*****void Reset(int i) { do { cout<<"请重新设置(是1/否0):"; cin>>para[i]; //此处输入字符出错,但没解决 }while(para[i]!=0&¶[i]!=1); }//*****设置题目难度*****void SetLevel() { system("cls"); char move2; cout<<"\t/*************设置题目难度*************/"<<endl; cout<<"\t 0.是否有乘除法(";YesOrNo(para[4]);cout<<")"<<endl; cout<<"\t 1.数值范围("<<para[5]<<"~"<<para[6]<<")"<<endl; cout<<"\t 2.是否有负数(";YesOrNo(para[7]);cout<<")"<<endl; cout<<"\t 3.是否有余数(";YesOrNo(para[8]);cout<<")"<<endl; cout<<"\t 4.返回主菜单"<<endl; cout<<"\t/**************************************/"<<endl; cout<<"请选择后续操作(0~4):"; cin>>move2; while(move2<'0'||move2>'4') { cout<<"错误!请正确输入操作序号(0~4):"; cin>>move2; } switch(move2) { case '0':Reset(4);break; case '1': //此处若输入字符,则出错 reset1: cout<<"新的数值下限:"; //但没找到解决方法 cin>>para[5]; cout<<"新的数值上限:"; cin>>para[6]; if(para[5]>=para[6]) { cout<<"出错!请重新输入数值范围!"<<endl; goto reset1; } break; case '2':Reset(7);break; case '3':Reset(8);break; case '4':break; } }//****主页面*****void MainMenu(LinkC &C,Caculation &Q) { char move,save; cout<<"\t/*************四则运算出题系统*************/"<<endl; cout<<"\t 0.开始出题"<<endl; cout<<"\t 1.设置出题数量"<<endl; cout<<"\t 2.设置打印方式"<<endl; cout<<"\t 3.设置题目难度"<<endl; cout<<"\t 4.查看当前设置"<<endl; cout<<"\t 5.开始答题"<<endl; cout<<"\t 6.退出系统"<<endl; cout<<"\t/******************************************/"<<endl; cout<<"请选择后续操作(0~6):"; cin>>move; while(move<'0'||move>'6') { cout<<"错误!请正确输入操作序号(0~6):"; cin>>move; } switch(move) { case '0':Display1(C,Q);break; case '1': reset4: cout<<"请设置出题数量(建议100道以内):"; cin>>para[1]; if(para[1]<=0) { cout<<"出错!请重新输入!"<<endl; goto reset4; } break; case '2':SetPrint();break; case '3':SetLevel();break; case '4':ShowSetting();break; case '5':Display2(C,Q);break; case '6': cout<<"是否保存出题记录(是1/否0):"; cin>>save; while(save!='1'&&save!='0') { cout<<"出错!请正确输入(是1/否0):"; cin>>save; } if(save=='1') WriteQues(C); cout<<"感谢您的使用,再见!"<<endl; para[0]=0;break; } } int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); //srand()函数产生一个以当前时间开始的随机种子 LinkC Cacu; Caculation ques; InitList(Cacu); ReadQues(Cacu); while(para[0]) { system("cls"); MainMenu(Cacu,ques); system("pause"); } return 0; } (2) #include "iostream.h" #include "fstream.h" //0退出、1出题数量、2打印列数、3每行间隔、4乘除、5数值范围下限、6数值范围上限、7负数、8余数、9出过的题目数int para[10]={1,30,3,0,0,0,5,0,0,0}; //默认参数 //*****四则算术题的数据结构***** typedef struct { int num1; int num2; int sign; int answer; }Caculation; typedef struct CNode //结点{ Caculation ques; struct CNode * next; }CNode,*LinkC;//******题目初始化******void InitList(LinkC &C) { C=new CNode; C->next=NULL; }//******添加题目信息******void InsertQues(LinkC &C,Caculation Q) { //尾插入 LinkC tail,temp; tail=C; while(tail&&tail->next!=NULL) tail=tail->next; temp=new CNode; temp->ques=Q; temp->next=NULL; tail->next=temp; tail=temp; para[9]++; }//******判断题目存在******int ExistQues(LinkC C,Caculation Q) { LinkC temp; temp=C->next; while(temp) { if((temp->ques.num1==Q.num1)&&(temp->ques.num2==Q.num2)&&(temp->ques.sign==Q.sign)) return 1; //当两个数字和算符与链表中的一样,则存在 else temp=temp->next; } return 0; }//******读取出过的问题******void ReadQues(LinkC &C) { LinkC temp; ifstream infile("question.txt"); for(int i=0;i<10;i++) //读取参数表 infile>>para[i]; for(i=0;i<para[9];i++) //读取出过的题目 { temp=new CNode; infile>>temp->ques.num1; infile>>temp->ques.num2; infile>>temp->ques.sign; temp->next=NULL; } }//******写入文件******void WriteQues(LinkC C) { LinkC temp; ofstream outfile("question.txt"); if(!outfile) { cout<<"文件存储失败!"<<endl; exit(0); } for(int i=0;i<10;i++) outfile<<para[i]<<" "; for(temp=C->next;temp;temp=temp->next) { outfile<<temp->ques.num1<<" "; outfile<<temp->ques.num2<<" "; outfile<<temp->ques.sign<<" "; } 二、截图 #include "stdafx.h" #include "iostream.h" #include "stdlib.h" #include "time.h" #include "Caculation.h" #include "iomanip.h" //*****判断回答是否正确*****bool RightOrWrong(Caculation &Q,int answer) { if(Q.sign==0) { Q.answer=Q.num1+Q.num2; } else if(Q.sign==1) { Q.answer=Q.num1-Q.num2; } else if(Q.sign==2) { Q.answer=Q.num1*Q.num2; } else Q.answer=Q.num1/Q.num2; if(answer==Q.answer) return true; else return false; }//******重新生成算术题******void ReBuild(Caculation &Q) { if(para[4]==1) Q.sign=rand()%4; else Q.sign=rand()%2; Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } //*****输出函数*****void Display1(LinkC C,Caculation &Q) { int temp,count=0; //count记录重新生成题目的次数 for(int i=1;i<=para[1];i++) { cout<<"("<<i<<")"; if(para[4]==1) Q.sign=rand()%4; //有乘除法 else Q.sign=rand()%2; //没有乘除法 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; //随机的(下限~上限)以内的整数 Q.num2=rand()%(para[6]-para[5]+1)+para[5]; RB: ReBuild(Q); //检查是否有出过的题目 if(count>((para[6]-para[5]+1)*(para[6]-para[5]+1)*100)) { cout<<endl<<"该难度的题目已出完,请更改出题设置!"<<endl; break; } switch(Q.sign) { case 0: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"+"<<Q.num2<<"="<<setw(5); break; case 1: if((para[7]==0)&&(Q.num1<Q.num2)) { //若为负数,则交换 temp=Q.num1; Q.num1=Q.num2; Q.num2=temp; } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"-"<<Q.num2<<"="<<setw(5);break; case 2: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"*"<<Q.num2<<"="<<setw(5);break; case 3: while(Q.num2==0) Q.num2=rand()%(para[6]-para[5]+1)+para[5]; if(!para[8]) { while((Q.num1%Q.num2)!=0||Q.num2==0) { //重新生成 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"/"<<Q.num2<<"="<<setw(5);break; } InsertQues(C,Q); if(i%para[2]==0) //一行打印完规定列数,换行 for(int j=0;j<=para[3];j++) cout<<endl; } cout<<endl<<endl; }//*****回答题目函数******void Display2(LinkC C,Caculation &Q) { int temp,count=0,answer=0,right=0; //count记录重新生成题目的次数 for(int i=1;i<=para[1];i++) { cout<<"("<<i<<")"; if(para[4]==1) Q.sign=rand()%4; //有乘除法 else Q.sign=rand()%2; //没有乘除法 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; //随机的(下限~上限)以内的整数 Q.num2=rand()%(para[6]-para[5]+1)+para[5]; RB: ReBuild(Q); //检查是否有出过的题目 if(count>((para[6]-para[5]+1)*(para[6]-para[5]+1)*100)) { cout<<endl<<"该难度的题目已出完,请更改出题设置!"<<endl; break; } switch(Q.sign) { case 0: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"+"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } else cout<<"\t×"; break; case 1: if((para[7]==0)&&(Q.num1<Q.num2)) { //若为负数,则交换 temp=Q.num1; Q.num1=Q.num2; Q.num2=temp; } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"-"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } else cout<<"\t×"; break; case 2: if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"*"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } cout<<"\t×"; break; case 3: while(Q.num2==0) Q.num2=rand()%(para[6]-para[5]+1)+para[5]; if(!para[8]) { while((Q.num1%Q.num2)!=0||Q.num2==0) { //重新生成 Q.num1=rand()%(para[6]-para[5]+1)+para[5]; Q.num2=rand()%(para[6]-para[5]+1)+para[5]; } } if(ExistQues(C,Q)) { count++; goto RB; } cout<<Q.num1<<"/"<<Q.num2<<"="; cin>>answer; if(RightOrWrong(Q,answer)) { cout<<"\t√"; right++; } cout<<"\t×"; break; } InsertQues(C,Q); cout<<endl; } cout<<"共回答正确"<<right<<"道题。"; cout<<endl<<endl; } //*****判断难度*****void YesOrNo(int para) { if(para) cout<<"是"; else cout<<"否"; }//*****查看设置*****void ShowSetting() { system("cls"); cout<<"\t/*************四则运算出题系统*************/"<<endl; cout<<"\t题目数量:"<<para[1]<<"个\t\t打印列数:"<<para[2]<<"列"<<endl; cout<<"\t每行间隔:"<<para[3]<<"行\t\t是否有乘除法:";YesOrNo(para[4]);cout<<endl; cout<<"\t数值范围下限:"<<para[5]<<"\t\t数值范围上限:"<<para[6]<<endl; cout<<"\t是否有负数:";YesOrNo(para[7]);cout<<"\t\t是否有余数:";YesOrNo(para[8]);cout<<endl; cout<<"\t/******************************************/"<<endl; }//*****设置打印方式*****void SetPrint() { system("cls"); char move1; cout<<"\t/*************设置打印方式*************/"<<endl; cout<<"\t 0.设置打印列数("<<para[2]<<"列)"<<endl; cout<<"\t 1.设置每行间隔("<<para[3]<<"行)"<<endl; cout<<"\t 2.返回主菜单"<<endl; cout<<"\t/**************************************/"<<endl; cout<<"请选择后续操作(0~2):"; cin>>move1; while(move1<'0'||move1>'2') { cout<<"错误!请正确输入操作序号(0~2):"; cin>>move1; } switch(move1) { case '0': reset2: cout<<"新的打印列数(建议7列以内):"; cin>>para[2]; if(para[2]<0) { cout<<"出错!请重新输入!"<<endl; goto reset2; } break; case '1': reset3: cout<<"新的间隔行数:"; cin>>para[3]; if(para[3]<0) { cout<<"出错!请重新输入!"<<endl; goto reset3; } break; case '2':break; } }//*****判断输入正确*****void Reset(int i) { do { cout<<"请重新设置(是1/否0):"; cin>>para[i]; //此处输入字符出错,但没解决 }while(para[i]!=0&¶[i]!=1); }//*****设置题目难度*****void SetLevel() { system("cls"); char move2; cout<<"\t/*************设置题目难度*************/"<<endl; cout<<"\t 0.是否有乘除法(";YesOrNo(para[4]);cout<<")"<<endl; cout<<"\t 1.数值范围("<<para[5]<<"~"<<para[6]<<")"<<endl; cout<<"\t 2.是否有负数(";YesOrNo(para[7]);cout<<")"<<endl; cout<<"\t 3.是否有余数(";YesOrNo(para[8]);cout<<")"<<endl; cout<<"\t 4.返回主菜单"<<endl; cout<<"\t/**************************************/"<<endl; cout<<"请选择后续操作(0~4):"; cin>>move2; while(move2<'0'||move2>'4') { cout<<"错误!请正确输入操作序号(0~4):"; cin>>move2; } switch(move2) { case '0':Reset(4);break; case '1': //此处若输入字符,则出错 reset1: cout<<"新的数值下限:"; //但没找到解决方法 cin>>para[5]; cout<<"新的数值上限:"; cin>>para[6]; if(para[5]>=para[6]) { cout<<"出错!请重新输入数值范围!"<<endl; goto reset1; } break; case '2':Reset(7);break; case '3':Reset(8);break; case '4':break; } }//****主页面*****void MainMenu(LinkC &C,Caculation &Q) { char move,save; cout<<"\t/*************四则运算出题系统*************/"<<endl; cout<<"\t 0.开始出题"<<endl; cout<<"\t 1.设置出题数量"<<endl; cout<<"\t 2.设置打印方式"<<endl; cout<<"\t 3.设置题目难度"<<endl; cout<<"\t 4.查看当前设置"<<endl; cout<<"\t 5.开始答题"<<endl; cout<<"\t 6.退出系统"<<endl; cout<<"\t/******************************************/"<<endl; cout<<"请选择后续操作(0~6):"; cin>>move; while(move<'0'||move>'6') { cout<<"错误!请正确输入操作序号(0~6):"; cin>>move; } switch(move) { case '0':Display1(C,Q);break; case '1': reset4: cout<<"请设置出题数量(建议100道以内):"; cin>>para[1]; if(para[1]<=0) { cout<<"出错!请重新输入!"<<endl; goto reset4; } break; case '2':SetPrint();break; case '3':SetLevel();break; case '4':ShowSetting();break; case '5':Display2(C,Q);break; case '6': cout<<"是否保存出题记录(是1/否0):"; cin>>save; while(save!='1'&&save!='0') { cout<<"出错!请正确输入(是1/否0):"; cin>>save; } if(save=='1') WriteQues(C); cout<<"感谢您的使用,再见!"<<endl; para[0]=0;break; } } int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); //srand()函数产生一个以当前时间开始的随机种子 LinkC Cacu; Caculation ques; InitList(Cacu); ReadQues(Cacu); while(para[0]) { system("cls"); MainMenu(Cacu,ques); system("pause"); } return 0; } (2) #include "iostream.h" #include "fstream.h" //0退出、1出题数量、2打印列数、3每行间隔、4乘除、5数值范围下限、6数值范围上限、7负数、8余数、9出过的题目数int para[10]={1,30,3,0,0,0,5,0,0,0}; //默认参数 //*****四则算术题的数据结构***** typedef struct { int num1; int num2; int sign; int answer; }Caculation; typedef struct CNode //结点{ Caculation ques; struct CNode * next; }CNode,*LinkC;//******题目初始化******void InitList(LinkC &C) { C=new CNode; C->next=NULL; }//******添加题目信息******void InsertQues(LinkC &C,Caculation Q) { //尾插入 LinkC tail,temp; tail=C; while(tail&&tail->next!=NULL) tail=tail->next; temp=new CNode; temp->ques=Q; temp->next=NULL; tail->next=temp; tail=temp; para[9]++; }//******判断题目存在******int ExistQues(LinkC C,Caculation Q) { LinkC temp; temp=C->next; while(temp) { if((temp->ques.num1==Q.num1)&&(temp->ques.num2==Q.num2)&&(temp->ques.sign==Q.sign)) return 1; //当两个数字和算符与链表中的一样,则存在 else temp=temp->next; } return 0; }//******读取出过的问题******void ReadQues(LinkC &C) { LinkC temp; ifstream infile("question.txt"); for(int i=0;i<10;i++) //读取参数表 infile>>para[i]; for(i=0;i<para[9];i++) //读取出过的题目 { temp=new CNode; infile>>temp->ques.num1; infile>>temp->ques.num2; infile>>temp->ques.sign; temp->next=NULL; } }//******写入文件******void WriteQues(LinkC C) { LinkC temp; ofstream outfile("question.txt"); if(!outfile) { cout<<"文件存储失败!"<<endl; exit(0); } for(int i=0;i<10;i++) outfile<<para[i]<<" "; for(temp=C->next;temp;temp=temp->next) { outfile<<temp->ques.num1<<" "; outfile<<temp->ques.num2<<" "; outfile<<temp->ques.sign<<" "; }