最恨那些一点不珍惜别人劳动成果,问一两句就否定之类的老师,我也只能是呵呵呵呵呵。
说是什么没有按他说的那个什么破表写,然后看也不看就走了,呵呵呵呵
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<math.h> #include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> #define max 100 using namespace std;//push(i) pop() top() empty() class a_stack { private: double donser[max]; int length=0; public: void push_num(a_stack &l,double i);//入栈push void pop_num(a_stack &l);//出栈pop double top_num(a_stack &l);//返回栈顶数据top int check_empty(a_stack &l);//检查是否为空empty }; class b_stack { private: char donser1[max]; int length1=0; public: void push_num(b_stack &l,char i);//入栈push void pop_num(b_stack &l);//出栈pop int top_num(b_stack &l);//返回栈顶数据top int check_empty(b_stack &l);//检查是否为空empty }; void a_stack::push_num(a_stack &l,double i) { int k; l.length++; k=l.length; l.donser[k]=i; } void a_stack::pop_num(a_stack &l) { int k; k=l.length; l.length--; l.donser[k]=0; } double a_stack::top_num(a_stack &l) { return l.donser[l.length]; } int a_stack::check_empty(a_stack &l)//1为空 { if(l.length==0){return 1;} else return 0; } void b_stack::push_num(b_stack &l,char i) { int k; l.length1++; k=l.length1; l.donser1[k]=i; } void b_stack::pop_num(b_stack &l) { int k; k=l.length1; l.length1--; l.donser1[k]=0; } int b_stack::top_num(b_stack &l) { return l.donser1[l.length1]; } int b_stack::check_empty(b_stack &l)//1为空 { if(l.length1==0){return 1;} else return 0; } int i; double a,b; char s[250],c; int main() { cout<<"输入算式:"; while(gets(s),strcmp(s,"#")!=0) { //stack<char>s1; //stack<double>s2; b_stack s1; a_stack s2; for(i=0;s[i];i++) { if(s[i]>='0'&&s[i]<='9') //如果是数字继续找数字 { a=0; while(s[i]>='0'&&s[i]<='9') { a=a*10+s[i]-'0'; i++; } i--; s2.push_num(s2,a); } else if(s[i]=='(') //如果( { s1.push_num(s1,s[i]); } else if(s[i]==')') //如果) { while(s1.top_num(s1)!='(')//找不到前括号就循环 { c=s1.top_num(s1);//符号top s1.pop_num(s1);//删掉 a=s2.top_num(s2);//数字top s2.pop_num(s2);//删掉 b=s2.top_num(s2);//当前数字top s2.pop_num(s2);//删掉 if(c=='+') a+=b; if(c=='-') a=b-a; if(c=='*') a=b*a; if(c=='/') a=b/a; s2.push_num(s2,a); } s1.pop_num(s1);//删除前括号 if(s1.check_empty(s1)==1){continue;} if(s1.top_num(s1)=='*') //去掉括号以后栈还是乘 { s1.pop_num(s1);//删掉 a=s2.top_num(s2);//数字top s2.pop_num(s2);//删掉 b=s2.top_num(s2);//当前数字top s2.pop_num(s2);//删掉 a=b*a; s2.push_num(s2,a); } } else if(s[i]=='-'||s[i]=='+') //如果是+- { if(s1.check_empty(s1)==0&&s1.top_num(s1)!='(')//优先级低或者一样交换符号 { c=s1.top_num(s1);//字符栈顶 s1.pop_num(s1);//删掉 a=s2.top_num(s2);//数字栈顶1 s2.pop_num(s2);//删掉 b=s2.top_num(s2);//数字栈顶2 s2.pop_num(s2);//删掉 if(c=='+') a+=b; if(c=='-') a=b-a; if(c=='*') a=b*a; if(c=='/') a=b/a; s2.push_num(s2,a);//运算以后的入数字栈 s1.push_num(s1,s[i]);//字符入栈 } else if(s1.check_empty(s1)==1||s1.top_num(s1)=='(')//如果空或者前括号 { s1.push_num(s1,s[i]);//字符入栈 } } else if(s[i]=='/') //如果除 { b=0; c=s[i];//存一下符号 if(s1.check_empty(s1)==1||s1.top_num(s1)=='(') //空就入栈不运算 { s1.push_num(s1,c); continue; } i+=2;//找符号后面的数字 while(s[i]>='0'&&s[i]<='9') { b=b*10+s[i]-'0'; i++; } i--;//找到数字 a=s2.top_num(s2);//取出数字栈顶 s2.pop_num(s2);//删掉 if(s1.top_num(s1)=='*') //优先级一样交换符号 { a=a*b; s1.pop_num(s1);//删除原来的 s1.push_num(s1,c);//换成新的 } else a=a/b;//优先级高做除法 s2.push_num(s2,a);//新数字入栈 } else if(s[i]=='*') //如果乘 { b=0; c=s[i]; if(s1.check_empty(s1)==1||s1.top_num(s1)=='(') { s1.push_num(s1,c); continue; } i+=2; if(s[i]=='(') { s1.push_num(s1,c); i--; continue; } while(s[i]>='0'&&s[i]<='9') { b=b*10+s[i]-'0'; i++; } i--; a=s2.top_num(s2); s2.pop_num(s2); if(s1.top_num(s1)=='/') { a=a/b; s1.pop_num(s1); s1.push_num(s1,c); } else if(s1.top_num(s1)!='/') { a=a*b; } s2.push_num(s2,a); } } while(!s1.check_empty(s1))//如果符号栈非空就循环 { c=s1.top_num(s1);//符号top s1.pop_num(s1);//删掉 a=s2.top_num(s2);//数字top s2.pop_num(s2);//删掉 b=s2.top_num(s2);//当前数字top s2.pop_num(s2);//删掉 if(c=='+') a+=b; if(c=='-') a=b-a; if(c=='*') a=b*a; if(c=='/') a=b/a; s2.push_num(s2,a); } printf("%.2f ",s2.top_num(s2)); } return 0; }