代码如下:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int num1,num2,op,result1,result2; //num1,num2:操作数;op:运算符;result1,result2:结果 do { srand(time(NULL)); num1=rand()*10/(RAND_MAX+1); num2=rand()*10/(RAND_MAX+1); //生成运算数 op=rand()*4/(RAND_MAX+1); //生成运算符 0-- +,1-- -,2-- *,3-- / switch (op) { case 0: cout<<num1<<"+"<<num2<<"=? "; cin>>result1; if (num1+num2 == result1) cout<<"you are right"<<endl; else cout<<"you are wrong"<<endl; break; case 1: cout<<num1<<"-"<<num2<<"=? "; cin>>result1; if (num1-num2 == result1) cout<<"you are right"<<endl; else cout<<"you are wrong"<<endl; break; case 2: cout<<num1<<"*"<<num2<<"=? "; cin>>result1; if (num1*num2 == result1) cout<<"you are right"<<endl; else cout<<"you are wrong"<<endl; break; case 3: cout<<num1<<"/"<<num2<<"=? "; cin>>result1; cout<<"余数为=? "; cin>>result2; if ((num1/num2 == result1)&&(num1%num2==result2)) cout<<"you are right"<<endl; else cout<<"you are wrong"<<endl; break; } }while(1); return 0; }
原文地址:http://blog.csdn.net/liuchang54/article/details/44015895