标签:images standard srand ems lan 时间 实现 统计 operator
源代码地址:https://git.coding.net/c_xx/02.git
题目描述:
需求分析:
功能设计:
代码说明:
为了实现需求,共设计了4个功能函数,分别是:
char Operator() //随机选取运算符
int maxNumber(int k, int l) //辗转相除法求最大公约数函数
void integer() //整数运算
void fraction() //真分数运算
char Operator() //随机选取运算符 { char op; srand(time(0)); int i=rand()%4+1; switch(i) { case 1: return ‘+‘; case 2: return ‘-‘; case 3: return ‘*‘; case 4: return ‘/‘; } }
int maxNumber(int k, int l) //辗转相除法求最大公约数函数 { while(k%l!=0){ int t=k%l; k=l; l=t; } return l; }
void integer() //整数运算 { int g; int x,y; char op; srand(time(0)); x=rand()%100+1; y=rand()%100+1; op=Operator(); cout<<x<<op<<y<<"="<<endl; cout<<"请输入运算结果:"; if(op==‘+‘) //整数“+”运算 { cin>>g; if(g==x+y) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<x+y<<endl<<endl; mistake++; } } else if(op==‘-‘) //整数“-”运算 { cin>>g; if(g==x-y) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<x-y<<endl<<endl; mistake++; } } else if(op==‘*‘) //整数“*”运算 { cin>>g; if(g==x*y) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<x*y<<endl<<endl; mistake++; } } else if(op==‘/‘) //整数“/”运算 { cin>>result; element=x; denominator=y; maxNum=maxNumber(element,denominator); element=element/maxNum; denominator=denominator/maxNum; if(denominator!=1) { sprintf_s(answer, "%d/%d",element ,denominator); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } else { sprintf_s(answer, "%d",element); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } } }
void fraction() //真分数运算 { srand(time(NULL)); memset(result,0, sizeof(result)); memset(answer,0, sizeof(answer)); int a,b,c,d; a=rand()%100+1; b=rand()%100+1; c=rand()%100+1; d=rand()%100+1; char op; while(a<=b||c<=d) { a=rand()%100+1; b=rand()%100+1; c=rand()%100+1; d=rand()%100+1; } op=Operator(); cout<<"("<<b<<"/"<<a<<")"<<op<<"("<<d<<"/"<<c<<")"<<"="<<endl; cout<<"请输入运算结果:"; cin>>result; if(op==‘+‘) //分数“+”运算 { element=(b*c)+(a*d); denominator=a*c; maxNum=maxNumber(element,denominator); element=element/maxNum; denominator=denominator/maxNum; if(denominator!=1) { sprintf_s(answer, "%d/%d",element ,denominator); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } else { sprintf_s(answer, "%d",element); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } } else if(op==‘-‘) //分数“-”运算 { element=(b*c)-(a*d); denominator=a*c; maxNum=maxNumber(element,denominator); element=element/maxNum; denominator=denominator/maxNum; if(denominator!=1) { sprintf_s(answer, "%d/%d",element ,denominator); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } else { sprintf_s(answer, "%d",element); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } } else if(op==‘*‘) //分数“*”运算 { element=b*d; denominator=a*c; maxNum=maxNumber(element,denominator); element=element/maxNum; denominator=denominator/maxNum; if(denominator!=1) { sprintf_s(answer, "%d/%d",element ,denominator); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } else { sprintf_s(answer, "%d",element); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } } else if(op==‘/‘) //分数“/”运算 { element=b*c; denominator=a*d; maxNum=maxNumber(element,denominator); element=element/maxNum; denominator=denominator/maxNum; if(denominator!=1) { sprintf_s(answer, "%d/%d",element ,denominator); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } else { sprintf_s(answer, "%d",element); if (strcmp(answer, result) == 0) { cout<<"恭喜你答对了!"<<endl<<endl; correct++; } else { cout<<"很遗憾你答错了,正确答案是:"<<answer<<endl<<endl; mistake++; } } } }
测试运行:
PSP展示:
标签:images standard srand ems lan 时间 实现 统计 operator
原文地址:http://www.cnblogs.com/cxx111/p/6509436.html