forked from arpande214/programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfixEvaluation.cpp
47 lines (46 loc) · 998 Bytes
/
postfixEvaluation.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
#include <iostream>
#include <string>
#include <stack>
#include <math.h>
using namespace std;
int postfixEvaluation(string str)
{
stack<int> st;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
st.push(str[i]-'0');
}
else{
int op2 = st.top();
st.pop();
int op1 = st.top();
st.pop();
switch (str[i])
{
case '+':
st.push(op1+op2);
break;
case '-':
st.push(op1-op2);
break;
case '*':
st.push(op1*op2);
break;
case '/':
st.push(op1/op2);
break;
case '^':
st.push(pow(op1,op2));
}
}
}
return st.top();
}
int main()
{
string equation = "46+2/5*7+";
cout<<postfixEvaluation(equation)<<endl;
return 0;
}