-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostEvalConv.cpp
60 lines (56 loc) · 1.42 KB
/
postEvalConv.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
#include<bits/stdc++.h>
using namespace std;
int calc(int var1,int var2,char opr)
{
if(opr=='+') return var1+var2;
if(opr=='-') return var1-var2;
if(opr=='*') return var1*var2;
if(opr=='/') return var1/var2;
return 0;
}
int main()
{
string s="264*8/+3-";
stack<string> is;
stack<int> vs;
std::stack<string> ps;
int n=s.size();
for(int i=0;i<n;i++)
{
if(s[i]=='+' or s[i]=='-' or s[i]=='*' or s[i]=='/'){
int b=vs.top();
vs.pop();
int a=vs.top();
vs.pop();
int res=calc(a,b,s[i]);
vs.push(res);
string val2=is.top();
is.pop();
string val1=is.top();
is.pop();
string opr;
opr.push_back(s[i]);
string res1='('+val1+opr+val2+')';
is.push(res1);
string z=ps.top();
ps.pop();
string y=ps.top();
ps.pop();
string oprator;
oprator.push_back(s[i]);
string res2=oprator+y+z;
ps.push(res2);
}
else{
string str;
str.push_back(s[i]);
vs.push(s[i]-'0');
ps.push(str);
is.push(str);
}
}
std::cout << vs.top() << std::endl;
std::cout << is.top() << std::endl;
std::cout << ps.top() << std::endl;
return 0;
}