小程序
#include <stdio.h> #include"stdafx.h"
#include <ctime> #include <iostream> using namespace std; class produce { private: int num1; int num2; char fuhao; public: void pro(); void show(); produce(); int bijiao(int i); }; produce::produce() { num1=0; num2=0; fuhao='+'; } void produce::pro() { char c=0; while(c!='0') { int nINT=0; srand((unsigned)time(NULL)); nINT = rand()%4; num1 = rand()%10; num2 = rand()%10;//产生随机数范围是0-3(包括3) switch(nINT) { case 0://对应加法 fuhao='+'; break; case 1://对应减法 fuhao='-'; break; case 2://对应乘法 fuhao='*'; break; case 3://对应除法 fuhao='/'; break; default: break; } cin>>c; } } int produce::bijiao(int i) { int sum; if(fuhao=='+') sum=num1+num2; else if(fuhao=='-') sum=num1-num2; else if(fuhao=='*') sum=num1*num2; else sum=num1/num2; if(sum==i) return 1; else return 0; } void produce::show() { cout<<num1<<fuhao<<num2<<endl; }
int main() { int count=0; cout<<"输入0开始答题"<<endl; for(int x=0;x<10;x++) { int i; produce pp; pp.pro(); pp.show(); cout<<"第"<<x+1<<"题"<<" "<<"请输入答案"<<endl; cin>>i; count=count+pp.bijiao(i); if(x<9) cout<<"请输入0继续答题"<<endl; } cout<<"您共答对"<<count<<"题"<<endl; return 0; } 有瑕疵