需求分析:
●基本功能
实现100以内的加减乘除法
设计:
●首先选择是否进入测试状态,若选择进入测试,则随机产生100以内的加减乘除的思则运算的题目
● 用Switch选择语句来选择做什么运算(0表示加法运算,1表示减法,2表示除法运算,3表示乘法)以及根据答对的题目个数做出相关的评价
●代码实现
#include <iostream> #include <ctime> using namespace std; int main() { int a,b,c,R=0,x,z,i,y,k; srand(time(NULL)); cin>>z; if (z==1) { cout<<"测试开始! "; for(i=1;i<11;i++) { a=rand()%100; b=rand()%100; x=rand()%4; k=rand()%10; switch(x) { case 0: { y=a+b; cout<<"请看题: "; cout<<a<<"+"<<b<<"="; cin>>c; if (y==c) { R+=1; cout<<"答对了,累计答对题数为"<<R<<" "; } else cout<<"答错了,继续努力! "; break; } case 1: { if (b>a) { z=a; a=b; b=z; } y=a-b; cout<<"请看题: "; cout<<a<<"-"<<b<<"="; cin>>c; if (y==c) { R+=1; cout<<"答对了,累计答对题数为"<<R<<" "; } else cout<<"答错了,继续努力! "; break; case 2: { if (a>10) { a=a/10; } if (b>10) { b=b/10; } y=a*b; cout<<"请看题: "; cout<<a<<"*"<<b<<"="; cin>>c; if (y==c) { R+=1; cout<<"答对了,累计答对题数为"<<R<<" "; } else cout<<"答错了,继续努力! "; break; } case 3: { if (b>10) { b=b/10; } if (!(a%b==0)) { a=b*k; } y=a/b; cout<<"请看题: "; cout<<a<<"/"<<b<<"="; cin>>c; if (y==c) { R+=1; cout<<"答对了,累计答对题数为"<<R<<" "; } else cout<<"答错了,继续努力! "; break; } } } } } else { cout<<"测试已完成,谢谢您的参与 "; return 0; } switch(R) { case 10: cout<<"真是个天才! ";break; case 9: cout<<"真聪明! ";break; case 8: cout<<"还不错! ";break; case 7: cout<<"多多加油! ";break; case 6: cout<<"刚及格,还得多练习! ";break; case 5: case 4: case 3: case 2: case 1: case 0: cout<<"不及格! ";break; } return 0; }
总结:注意逻辑的合理性,即使可以运行也可能是死循环导致程序没法实现预期的功能。