#include<iostream> #include<sstream> #include<string> #include<ctime> #include<time.h> #include<fstream> #define size 10000 using namespace std; class Calculator { public: Calculator() { STop=0; NTop=0; SignTemp=NULL; NumTemp=0; } string num_str(int i); // int string int Operator(int m); void compute(); //计算函数 double Run(int &i,int &length); char cal[100][100]; private: char Sign[100],SignTemp; double Num[100],NumTemp; int STop,NTop; }; string num_str(int i) //将数字转换为字符串 { stringstream ss; ss<<i; return ss.str(); } int Operator(int m) //产生运算符 { int a,b; char oper; a=rand()%4; //产生随机整数用于除四取余后进行四种运算符号和四种数的选择 b=rand()%2; if(m==2) //无乘除法情况 { switch(b) //用于加减运算两种符选择 { case 0: oper=‘+‘; break; case 1: oper=‘-‘; break; default: break; } } if(m==1) //有乘除法情况 { switch(a) //用于四则运算四种符号的选择 { case 0: oper=‘+‘; break; case 1: oper=‘-‘; break; case 2: oper=‘*‘; break; case 3: oper=‘/‘; break; default: break; } } return oper; } void Calculator::compute() { switch (Sign[STop-1]) { case ‘+‘: Num[NTop-2]+=Num[NTop-1];//运算后将结果存入 STop--; NTop--; break; case ‘-‘: Num[NTop-2]-=Num[NTop-1]; STop--; NTop--; break; case ‘*‘: Num[NTop-2]*=Num[NTop-1]; STop--; NTop--; break; case ‘/‘: //判断除数是否为0,0的话结束 if (Num[STop-1]==0) { cout<<"data error!!"<<endl; exit(0); } else { Num[NTop-2]/=Num[NTop-1]; STop--; NTop--; break; } //其中STop--;NTop--;实现的是将数据弹出栈 case ‘)‘: STop=STop-2; break; //发现‘)‘对上个运算符进行判断 } } double Calculator::Run(int &i,int &length) //运行函数 { char temp; int p=0; while (p<=length) //读入数据 { temp=cal[i][p]; if (temp>=‘0‘&&temp<=‘9‘) { NumTemp=NumTemp*10+double(temp-48); } else { Sign[STop]=SignTemp; SignTemp=temp; STop++; //运算符入栈 if (Sign[STop-1]!=‘)‘ && temp!=‘(‘) //数字入栈 { Num[NTop]=NumTemp; NTop++; NumTemp=0; } switch (temp) //进行有关运算优先级别的考虑 { case ‘+‘: case ‘-‘: while(Sign[STop-1]!=‘(‘ && STop-1>0) compute(); break; //"+""-"的时候考虑"(" case ‘*‘: case ‘/‘: while( Sign[STop-1]!=‘(‘ && Sign[STop-1]!=‘+‘ && Sign[STop-1]!=‘-‘&& STop-1>0 ) compute(); break; case ‘(‘: break; //继续读取 case ‘)‘: while(Sign[STop-1]!=‘(‘) compute(); break; default: while(STop-1>0) compute(); //Num[0]; break; //exit(0); //结束 } } p++; } return Num[0]; } int main() //满足最多十个数值运算 { int num1,num2,jishu,i,j,n=0; char oper1; string s_num1,s_num2,s_connect[size]; srand((unsigned)time(NULL)); //通过系统时间初始化随机数种子,若不设置则每次产生随机数相同 int number,value,m; cout<<"请输入运算题目数量:"<<endl; //定制运算题目数量 cin>>number; cout<<"请输入数值产生范围:"<<endl; //选择数值产生范围 cin>>value; cout<<"请选择运算是否有乘除法:1.有 2.没有"<<endl; //乘除法选择 cin>>m; cout<<"请计算并输出结果:"<<endl; for(i=0;i<number;i++) { num1=rand()%value+1; num2=rand()%value+1; oper1=Operator(m); s_num1=num_str(num1); s_num2=num_str(num2); s_connect[i]=s_num1+oper1+s_num2; jishu=rand()%3+2; if(jishu>2) //对随机生成运算式里数值个数判断 { for(j=jishu;j>2;j--) //两个数值以上运算式生成 { s_num1=s_connect[i]; oper1=Operator(m); num2=rand()%value+1; s_num2=num_str(num2); s_connect[i]=s_num1+oper1+s_num2; } } Calculator Ca; int L; double b; L=s_connect[i].length(); int k; for(k=0;k<L;k++) { Ca.cal[i][k]=s_connect[i][k]; } double a=Ca.Run(i,L); cout<<s_connect[i]<<"="; cin>>b; if(b==a) { cout<<"恭喜你,回答正确!"<<endl; n++; } else { cout<<"很遗憾,回答错误!"<<endl; } } cout<<"本次答题共答对"<<n<<"道题!"<<endl; return 0; }
原文:http://www.cnblogs.com/fooreveryu/p/5295829.html