forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix to Postfix Stack.cpp
202 lines (175 loc) · 4.15 KB
/
Infix to Postfix Stack.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
----------- Infix to Postfix then Evaluation ----------
Using Stack Data Structure Method
N.B. On evaluation function if you want to display only
final output just put the result displayer from loop
------- © April 2019. AI Programming, @freecodecs --------
*/
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
// Verify whether a character is numeric digit or not.
bool IsNumber(char C) {
if(C >= '0' && C <= '9')
return true;
else
return false;
}
// Verify whether a character is operator symbol or not.
bool IsOperator(char C) {
if(C == '+' || C == '-' || C == '*' || C == '/' || C == '^' || C == '(' || C == ')')
return true;
else
return false;
}
// Get weight of an operator.
int GetWeight(char op){
switch(op)
{
case '^': // square operator
return 3;
break;
case '*':
case '/':
return 2;
break;
case '+':
case '-':
return 1;
break;
default:
return 0;
break;
}
}
// Operation of precidence condition
int HighPrecedence(char op1, char op2) {
if(GetWeight(op1) <= GetWeight(op2))
return true;
else
return false;
}
// Postfix expression Converter
void expressionToPostfix(char expression[], char postfix[], int size) {
stack<char> S;
int i=0, k=0;
// insert & print into stack & postfix until expression length is empty
while(i<size)
{
if(expression[i] == '(')
{
S.push(expression[i]);
i++;
continue;
}
if(expression[i] == ')')
{
while(!S.empty() && S.top() != '(')
{
postfix[k++]=' ';
postfix[k++]=S.top();
postfix[k++]=' ';
S.pop();
}
if(!S.empty())
S.pop();
i++;
continue;
}
if(!IsOperator(expression[i]))
{
postfix[k++] = expression[i];
}
else
{
if(S.empty())
{
S.push(expression[i]);
postfix[k++]=' ';
}
else
{
while(!S.empty() && S.top() != '(' && HighPrecedence(expression[i], S.top()))
{
postfix[k++]=' ';
postfix[k++] = S.top();
S.pop();
}
S.push(expression[i]);
postfix[k++]=' ';
}
}
i++;
}
//print out all stack
while (!S.empty()) {
postfix[k++]=' ';
postfix[k++] = S.top();
S.pop();
}
//terminate array last tail
postfix[k] = 0;
}
//Calculate two oprands with given operator
int calculate(char oper, int oprd1, int oprd2)
{
if(oper == '+') return oprd1 + oprd2;
else if(oper == '-') return oprd1 - oprd2;
else if(oper == '*') return oprd1 * oprd2;
else if(oper == '/') return oprd1 / oprd2;
else if(oper == '^') return oprd1 * oprd1;
else cout<<"Unexpected Error \n";
return 0;
}
// Evaluate postfix expression to final result if the expression is number
void evaluatePostfix(string postfixexp, int sizepfx){
stack<int> S;
for(int i=0; i<sizepfx ;i++)
{
if(postfixexp[i] == ' ')
continue;
else if(!IsNumber(postfixexp[i]) && !IsOperator(postfixexp[i]))
{
cout<<"\nThe expression is not number! \n It can't be evaluated.";
break;
}
else if(IsOperator(postfixexp[i]))
{
int oprd2 = S.top();
S.pop();
int oprd1 = S.top();
S.pop();
int result = calculate(postfixexp[i], oprd1, oprd2);
S.push(result);
}
else if(IsNumber(postfixexp[i]))
{
int operand = 0;
while(i<sizepfx && IsNumber(postfixexp[i]))
{
operand = (operand * 10) + (postfixexp[i] - '0');
i++;
}
i--;
S.push(operand);
}
cout<<"\nThe evaluated expression is : "<<S.top();
}
}
int main() {
// declare expression expression & its length
char expression[30];
cout<<"Postfix Converter & Evaluator"<<endl<<endl;
cout<<"Enter Expression : ";
cin>>expression;
//declare size of expression & postfix variable
int size = strlen(expression);
char postfix[size];
//call and print expression to postfix
expressionToPostfix(expression, postfix, size);
cout<<"\nThe Postfix Expression is : "<<postfix<<"\n";
//call evaluate postfix
evaluatePostfix(postfix, strlen(postfix));
return 0;
}