思路:用两个栈分别存储运算符和值
#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int> you={{'+',1},{'-',1},{'*',2},{'/',2}};
stack<int> s1;
stack<char> s2;
void f(){
int re=0;
int a=s1.top();
s1.pop();
int b=s1.top();
s1.pop();
char c=s2.top();
s2.pop();
if(c=='+') re=b+a;
else if(c=='-') re=b-a;
else if(c=='*') re=b*a;
else re=b/a;
s1.push(re);
}
int main(){
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
int res=0,j=i;
while(i<s.size()&&isdigit(s[j])){
res=res*10+(s[j]-'0');
j++;
}
i=j-1;
s1.push(res);
}
else if(s[i]=='('){
s2.push(s[i]);
}
else if(s[i]==')'){
while(s2.top()!='('){
f();
}
s2.pop();
}
else{
while(s2.size()&&s2.top()!='('&&you[s[i]]<=you[s2.top()]){//一定要用循环,因为前面不一定只有一个运算符
f();
}
s2.push(s[i]);
}
}
while(s2.size()){
f();
}
cout<<s1.top();
return 0;
}