-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPostfixEval.cpp
180 lines (115 loc) · 3.78 KB
/
PostfixEval.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//postfix evaluation
#include<iostream>
#include<stack>
#include<string>
#include<math.h>
using namespace std;
//declaring functions
//1) function to get the result
double getResult(string postfix) ;
double getResultPrefix(string prefix);
//function to perform the operation between the 2 popped operands
double performOps(char opt,double opd1,double opd2);
//check if the scanned int is an operand
bool isOperand(char opd);
//check if the scanned char is a operator
bool isOperator(char c);
//for postfix expression evaluation, we scan form left to right
double getResult(string postfix)
{
//defining a stack from STL
stack<double> S;
//scanning form left to right of the expression
for(int i = 0; i < postfix.length();i++) {
//if delimitter then continue
if(postfix[i]== ' ' || postfix[i]==',') continue;
//if operator is scanned , then pop 2 operands from top of stack and
//perform the ops and push the result back to stack
else if(!S.empty() && isOperator(postfix[i])) {
//pop 2 operands
double operand1 = S.top() ; S.pop();
double operand2 = S.top() ; S.pop();
//performing the operation and getting result
double result = performOps(postfix[i],operand1,operand2);
//pushing the result to stack
S.push(result);
}
//if the scanned element is a operand-push it to stack
else if (isOperand(postfix[i])) {
//extract numeric operand from expression
int operand = 0;
//checking if the scanned expression is digit
if(isdigit(postfix[i])) {
//the below expression converts string operand to integer operant to do calculation
operand = (postfix[i]-'0');
}
//pushing operand on to stack
S.push(operand);
}
}
//top of stack contains the result so we return it.
return S.top(); //result stored at top of stack
}
//for prefix evaluation, we scan from the right to left
double getResultPrefix(string prefix)
{
//defining a stack from STL
stack<double> S;
//for prefix evaluation, we scan from the right to left i.e the operands
for(int i = prefix.size()-1; i >= 0 ;i--)
{
//if delimitter then continue
if(prefix[i]== ' ' || prefix[i]==',') continue;
//if operator is scanned , then pop 2 operands from top of stack and
//perform the ops and push the result back to stack
else if(!S.empty() && isOperator(prefix[i])) {
//pop 2 operands
double operand1 = S.top() ; S.pop();
double operand2 = S.top() ; S.pop();
//performing the operation and getting result
double result = performOps(prefix[i],operand1,operand2);
//pushing the result to stack
S.push(result);
}
//if the scanned element is a operand-push it to stack
else if (isOperand(prefix[i])) {
//extract numeric operand from expression
int operand = 0;
//checking if the scanned expression is digit
if(isdigit(prefix[i])) {
//the below expression converts string operand to integer operant to do calculation
operand = (prefix[i]-'0');
}
//pushing operand on to stack
S.push(operand);
}
}
//top of stack contains the result so we return it.
return S.top(); //result stored at top of stack
}
double performOps(char opt,double opd1,double opd2) {
if(opt=='+') return opd1+opd2;
if(opt=='-') return abs(opd1-opd2);
if(opt=='*') return opd1*opd2;
if(opt=='/') return (opd1/opd2);
else cout<<"Error"<<endl;
return -1;
}
bool isOperator(char c) {
if( c == '+' || c == '-' || c == '*' || c=='/' )
return true;
return false;
}
bool isOperand(char opd) {
if(opd>='0'&& opd<='9') return true;
else false;
}
int main()
{
string expression;
cout<<"Enter the prefix expression:"<<endl;
getline(cin,expression);
double result = getResultPrefix(expression);
cout<<"The result of "<<expression<< " is :"<<result<<endl;
return 0;
}