-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
319 lines (248 loc) · 8.72 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//===========================================================================
// Infix to Postfix
// Developed By: Leron Julian
// PROGRAM COMPLETED PER PROGRAM SPECIFICATION
//===========================================================================
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;
string Infix_To_Postfix(string expression);
bool is_Operand(char temp);
bool is_Operator(char temp);
int Precedence(char operator1, char operator2);
int getPrecedence(char Infix_Operator);
void evaluatePostFix(string postFix);
int do_Operation(int operand1, int operand2, char operater);
string to_base36(int val);
int main() {
int choice;
int min = 1;
int max = 3;
do{
cout << "Enter 1 to input an infix program and convert to postfix" << endl;
cout << "Enter 2 to input a postfix and evaluate the postfix expression" << endl;
cout << "Enter 3 to input an infix expression, convert to postfix, and evaluate" << endl;
cout << "Enter a choice -> ";
cin >> choice;
if (cin.fail()) {
cin.clear() ;
cin.ignore(1000, '\n');
cout << endl << "ENTER a valid input " << endl << endl;
continue ;
}
cin.clear();
cin.ignore(500, '\n');
if (choice < min || choice > max)
cout << endl << "ENTER a number in the valid range " << endl << endl;
} while (choice < min || choice > max);
cout << endl;
switch (choice) {
case 1:
{
string infix, postfix;
cout << "Enter an infix expression -> ";
getline(cin, infix);
infix.erase(remove(infix.begin(),infix.end(), ' '),infix.end());
cout << endl;
postfix = Infix_To_Postfix(infix);
cout << "Infix expression: " << infix << endl;
cout << "Postfix expression: " << postfix << endl << endl;
break;
}
case 2:
{
string postfix;
cout << "Enter a postfix expression -> ";
getline(cin, postfix);
postfix.erase(remove(postfix.begin(),postfix.end(), ' '),postfix.end());
string expression;
cout << endl;
cout << "Postfix Expression: " << postfix << endl;
cout << "Evaluated Postfix Expression: ";
evaluatePostFix(postfix);
cout << endl;
break;
}
case 3:
{
string infix, postfix;
cout << "Enter an infix expression -> ";
getline(cin, infix);
infix.erase(remove(infix.begin(),infix.end(), ' '),infix.end());
cout << endl;
postfix = Infix_To_Postfix(infix);
cout << endl;
cout << "Infix expression: " << infix << endl;
cout << "Postfix expression: " << postfix << endl;
cout << "Evaluated Postfix Expression: ";
evaluatePostFix(postfix);
cout << endl;
break;
}
}
return 0;
}
//===========================================================================
// This function is the main function that converts from infix to postfix
//===========================================================================
string Infix_To_Postfix(string expression)
{
Stack Character_Stack;
int size = expression.size();
string result;
for (int i = 0; i < (size); i++)
{
if (is_Operand(expression[i]))
result += expression[i];
else if (is_Operator(expression[i]))
{
while (!Character_Stack.is_empty() && Precedence(Character_Stack.top(), expression[i]) && Character_Stack.top() != '(')
{
result += Character_Stack.top();
Character_Stack.pop();
}
Character_Stack.push(expression[i]);
}
else if (expression[i] == '(')
Character_Stack.push(expression[i]);
else if (expression[i] == ')')
{
while (!Character_Stack.is_empty() && Character_Stack.top() != '(')
{
result += Character_Stack.top();
Character_Stack.pop();
}
if(!Character_Stack.is_empty())
Character_Stack.pop();
}
}
while (!Character_Stack.is_empty())
{
result += Character_Stack.top();
Character_Stack.pop();
}
return result;
}
//===========================================================================
// This function determines wheter a character is an operand or not
//===========================================================================
bool is_Operand(char temp)
{
if (temp >= '0' && temp <= '9')
return true;
if (temp >= 'a' && temp <= 'z')
return true;
if (temp >= 'A' && temp <= 'Z')
return true;
return false;
}
//===========================================================================
// This function determines whether a character is an operator or not
//===========================================================================
bool is_Operator(char temp)
{
if (temp == '+' || temp == '-' || temp == '*' || temp == '/')
return true;
return false;
}
//===========================================================================
// This function returns the precedence of an operator
//===========================================================================
int Precedence(char operator1, char operator2)
{
int operator1_Precedence = getPrecedence(operator1);
int operator2_Precedence = getPrecedence(operator2);
if (operator1_Precedence == operator2_Precedence)
return true;
else if (operator1_Precedence > operator2_Precedence)
return true;
else
return false;
}
//===========================================================================
// This function gets the precedence of an operator
//===========================================================================
int getPrecedence(char Infix_Operator)
{
switch (Infix_Operator)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0.;
}
}
//===========================================================================
// This function does the operation on two operands in the postfix
//===========================================================================
int do_Operation(int operand1, int operand2, char operater) {
switch (operater)
{
case '*':
return (operand2 * operand1);
case '/':
return (operand2 / operand1);
case '+':
return (operand2 + operand1);
case '-':
return (operand2 - operand1);
default :
return 0;
}
}
//===========================================================================
// This function converts an integer to a base 36 integer
//===========================================================================
string to_base36(int val)
{
string base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string result;
do {
result = base36[val % 36] + result;
} while (val /= 36);
return result;
}
//===========================================================================
// This function evaluates a postfix expression
//===========================================================================
void evaluatePostFix(string postFix)
{
Stack charStack;
int value = 0;
char ch;
int i = 0;
int size = postFix.size();
while (i < size)
{
ch = postFix[i];
if ( ch == 'A' || ch == 'B')
{
charStack.push(ch - '0');
}
else if (isdigit(ch))
charStack.push(ch - '0');
else
{
int operand1 = charStack.top();
charStack.pop();
int operand2 = charStack.top();
charStack.pop();
value = do_Operation(operand1, operand2, ch);
charStack.push(value);
}
i++;
}
string result;
if (value > 0)
{
result = to_base36(value);
cout << result << endl;
}
else
cout << value << endl;
}