表达式求值(数值范围为0~9)
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;
int oppriority(char c){
if(c=='+'||c=='-') return 1;
else if(c=='*'||c=='/') return 2;
else return 0;
}
string inTopost(string s){
stack<char> stk;
string res;
for(char c:s){
if(c==' ') continue;
if(isdigit(c)){
res=res+c;
}
else if(c=='(') stk.push(c);
else if(c==')'){
while(!stk.empty()&&stk.top()!='('){
res+=stk.top();
stk.pop();
}
stk.pop();
}
else{
while(!stk.empty()&&oppriority(c)<=oppriority(stk.top())){
res+=stk.top();
stk.pop();
}
stk.push(c);
}
}
while(!stk.empty()){
res+=stk.top();
stk.pop();
}
return res;
}
int getanswer(string str){
stack<int> sum;
for(char c:str){
if(isdigit(c)){
sum.push(c-'0');
}
else{
int x=sum.top();
sum.pop();
int y=sum.top();
sum.pop();
int s=0;
if(c=='+') s=y+x;
if(c=='-') s=y-x;
if(c=='*') s=y*x;
if(c=='/') s=y/x;
sum.push(s);
}
}
return sum.top();
}
int main(){
string str;
getline(cin,str);
string infix=inTopost(str);
cout<<getanswer(infix)<<endl;
return 0;
}