#include"iostream" #include"string" #include"stack" using namespace std;
void main() { string str; //printf("Shu RU Ji Suan Si Zi!!\n(Start With '#' And End With '#')\n"); //for(int j=0;str[j]!='#';j++) //gets(&str); str="#12+6*(2+3)/3-7#"; //str[0]='#';str[1]=12;str[2]='+';str[3]=6;str[4]='*';str[5]='(';str[6]=')';str[7]=2;str[8]='+';str[9]=3;str[10]=')';str[11]='/';str[12]=3;str[13]='-';str[14]=7;str[15]='#'; stack<char> s1; stack<int> s2; int i=1,x,fa,fb; if(str[0]=='#' ) s1.push(str[0]); else cout<<"error"; while(str[i]!='#') { if(str[i]!='+'&&str[i]!='-'&&str[i]!='*'&&str[i]!='/' &&str[i]!='('&&str[i]!=')') s2.push(str[i]); //else if(str[i]=='\0')goto lab; else if(str[i]=='+'||str[i]=='-') s1.push(str[i]); else if(str[i]=='*'||str[i]=='/') { if(s1.top()=='*'||'/') { fa=s2.top(); s2.pop(); fb=s2.top(); s2.pop(); if(str[i]=='*') x=fb*fa; else x=fb/fa; s2.push(x); s1.pop(); } else if(s1.top()=='(') { fa=s2.top(); s2.pop(); fb=s2.top(); s2.pop(); if(str[i]=='*') x=fb*fa; else x=fb/fa; s2.push(x); s1.pop(); } else s1.push(str[i]); } else if(str[i]=='(') s1.push(str[i]); else while(str[i]==')') { if(s1.top()==')') { s1.pop(); s1.pop(); } else { fa=s2.top(); s2.pop(); fb=s2.top(); s2.pop(); if(s1.top()=='+') x=fb+fa; else if(s1.top()=='-') x=fb-fa; else if(s1.top()=='*') x=fb*fa; else if(s1.top()=='/') x=fb/fa; s2.push(x); } i++; } if(s1.top()=='#') { s1.pop(); s1.pop(); cout<<"Da An Shi:"<<'%f'<<s2.top()<<'\n'; } else { fa=s2.top(); s2.pop(); fb=s2.top(); s2.pop(); if(s1.top()=='+') x=fb+fa; else if(s1.top()=='-') x=fb-fa; else if(s1.top()=='*') x=fb*fa; else if(s1.top()=='/') x=fb/fa; cout<<"Da An Shi:"<<'%f'<<x<<endl; }
} }