forked from Raju1822/HacktoberFest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_evaluation.cpp
49 lines (48 loc) · 999 Bytes
/
postfix_evaluation.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
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int operate(int a,int b,char c){
if(c=='+'){
return a+b;
}
else if(c=='-'){
return a-b;
}
else if(c=='*'){
return a*b;
}
else if(c=='/'){
return a/b;
}
else if(c=='^'){
return a^b;
}
}
int main() {
int op1,op2,top=-1,value=0;
vector<int> v;
string post;
cin>>post;
// cout<<post<<endl;
int i=0;
char tmp;
while(post[i]){
tmp=post[i];
if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^'){
value=operate(v[top-1],v[top],tmp);
v.pop_back();
v.pop_back();
top-=2;
v.push_back(value);
top++;
}
else{
int n=(int)tmp-'0';
v.push_back(n);
top++;
}
i++;
}
cout<<v[top]<<endl;
return 0;
}