-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_postfix.cpp
61 lines (50 loc) · 1.08 KB
/
eval_postfix.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<stack>
using namespace std;
void evalpostfix(string s){
stack<char> st;
int res;
for(int i = 0;i<s.length();i++){
char c=s[i];
if(isdigit(c)){
st.push(c);
}else{
char a = st.top();
st.pop();
char b = st.top();
st.pop();
int n1=a-'0';
int n2=b-'0';
int m;
char d;
switch(c){
case '+':
m = n1+n2;
d = '0'+m;
st.push(d);
break;
case '-':
m = n2-n1;
d = '0'+m;
st.push(d);
break;
case '*':
m = n1*n2;
d = '0'+m;
st.push(d);
break;
case '/':
m= n1/n2;
d = '0'+m;
st.push(d);
break;
}
}
}
cout<<st.top()-'0';
}
int main(){
string exp = "231*+9-";
evalpostfix(exp);
return 0;
}