-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4b) Postfix evaluation.cpp
47 lines (42 loc) · 1.1 KB
/
4b) 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
// WAP to evaluate any postfix expression
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
// Time and Space :- O(N)
float calc(float a , char op , float b){
switch(op){
case '^' : return pow(a,b);
case '*' : return a*b;
case '/' : return a/b ;
case '+' : return a+b;
default : return a-b;
}
}
float evaluatePostfix(string s){
stack<float>st;
for(int i=0;s[i];i++){
if(isdigit(s[i])){
float num = s[i] - '0' ;
st.push(num);
}
else{
// Operator found pop out last two elements
if(!st.empty()){
float last_ele = st.top();
st.pop();
if(!st.empty()){
float last_second_ele = st.top();
st.pop();
st.push(calc(last_second_ele,s[i],last_ele));
}
}
}
}
return (not st.empty())? st.top() : -1;
}
int main(){
string s = "92*74+/3-";
cout << s << " = " << evaluatePostfix(s) << endl;
return 0;
}