-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefixEvaluation.cpp
56 lines (48 loc) · 1.02 KB
/
prefixEvaluation.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
#include <bits/stdc++.h>
#include "MYSTACK.h"
using namespace std;
int prefixEvaluation(string chk)
{
Stack<int> st;
for (int i = chk.length() - 1; i >= 0; i--)
{
if (chk[i] >= '0' && chk[i] <= '9') // chk[i] 0 to 9 --> Operand
{
st.push(chk[i] - '0');
}
else // chk[i] ---> Operator
{
int a = st.pop();
int b = st.pop();
switch (chk[i])
{
case '+':
st.push(a + b);
break;
case '-':
st.push(a - b);
break;
case '*':
st.push(a * b);
break;
case '/':
st.push(a / b);
break;
case '^':
st.push(pow(a, b));
break;
default:
break;
}
}
}
return st.Top();
}
/*
+*423
-+7*45+20
*/
int main()
{
cout << endl << prefixEvaluation("-+7*45+20") << endl << endl;
}