0%

[題解]後序式求值

a151. 後序式求值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;

int main(){
string s;
while(cin>>s){
stack<int> stk;
for(int i=0;i<s.size();i++){
if(isdigit(s[i]))stk.push(s[i]-'0');
else{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if(s[i]=='+')stk.push(a+b);
else if(s[i]=='-')stk.push(b-a);
else if(s[i]=='*')stk.push(a*b);
else if(s[i]=='/')stk.push(b/a);
else if(s[i]=='%')stk.push(b%a);
}
}
cout<<stk.top()<<endl;
}
}