forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix_to_postfix.cpp
206 lines (177 loc) · 4.86 KB
/
infix_to_postfix.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
/**
* Given an infix expression, convert it to postfix. Consider usual operator precedence.
* Consider only following operands in your arsenal.
* +, -, * , /, ^(power)
* Operands are only alphabets --> 'a '-->'z' and 'A'-->'Z'
* Another assumption is operand is a single char.
*/
#include <iostream>
#include <stack.h>
#include <string>
// all utility and main functions declared first
/**
* isOperator- Determines if input char is operator
* @param c - char
* @return - true or false
*/
bool isOperator( char c );
/**
* isOperand Determines if input char is operand
* @param c - char
* @return true or false.
*/
bool isOperand( char c );
/**
* getOperatorWeight returns an integer weight of the operator
* @param c - An operator
* @return Weight of operator , larger is more precedence
*/
int getOperatorWeight(char c);
/**
* isRightAssociative -Determines if operator is right associative.
* @param c operator
* @return returns true if operator is right associative
*
* Associativity only comes to picture when both operators have same weight
* Example ^ is right associative ( ^ represents power of)
* 5 ^ 4 ^ 3 ^ 2 = 5 ^ ( 4 ^ ( 3 ^ 2 ) )
* where as 7 − 4 + 2 (7 − 4) + 2 = 5 or 7 − (4 + 2) = 1
*/
bool isRightAssociative(char c);
/**
* [hasHigherPrecedence] compare op1 and op2 based on precedence
* @param op1 operaror 1
* @param op2 operator 2
* @return return true if precedence(op1) > precedence(op2), else false
*/
bool hasHigherPrecedence(char op1, char op2);
/**
* [infixToPostfix - converts infix expr to postfix expr
* @param expression - infix expre
* @return postfix expr
*/
std::string infixToPostfix( std::string expression );
//check if c is an operator
bool isOperator( char c )
{
if ( c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
return true;
}
return false;
}
//check if c is a valid operand defined in comments.
bool isOperand( char c )
{
if ( c >= 'a' && c <= 'z' ) return true;
if ( c >= 'A' && c <= 'Z' ) return true;
return false;
}
//returns precedence weight of the char c.
// more weight higher precedence.
int getOperatorWeight( char c )
{
int weight;
switch(c) {
case '+':
case '-':
weight = 1;
break;
case '*':
case '/':
weight = 2;
break;
case '^':
weight = 3;
break;
default:
weight = -1;
}
return weight;
}
// is operator right associative?
bool isRightAssociative( char op )
{
if ( op == '^') return true;
return false;
}
bool hasHigherPrecedence( char op1, char op2 )
{
// get weight of the operators
int w1 = getOperatorWeight(op1);
int w2 = getOperatorWeight(op2);
// If both operators weigh same, return true if operators are
// left associative, return false if right associative.
if ( w1 == w2 ) {
if ( isRightAssociative(op1) ) {
return false;
}
return true;
}
return w1 > w2 ? true : false;
}
// final function to convert infix to postix operator.
std::string infixToPostFix(std::string expr)
{
bool errorDetected = false;
algo::Stack<char> operatorStack;
std::string postFixExpr = "";
//scanning input infix expr left to right char by char
for ( char c : expr ) {
// char is delimiter, continue
if ( c == ',' || c == ' ') {
continue;
}
//if character is an operand
else if ( isOperand(c) ) {
postFixExpr += c;
}
//if character is operator
else if ( isOperator(c) ) {
//if stack is not empty pop the stack back and compare precedence.
//if higher precedence perform operation.
while ( !operatorStack.empty() && operatorStack.top() != '(' &&
hasHigherPrecedence(operatorStack.top(), c) ) {
postFixExpr += operatorStack.top();
operatorStack.pop();
}
operatorStack.push(c);
}
else if( c == '(' ) {
operatorStack.push(c);
}
else if( c == ')' ) {
while( !operatorStack.empty() && operatorStack.top() != '(' ) {
postFixExpr += operatorStack.top();
operatorStack.pop();
}
operatorStack.pop();
}
else {
std::cout << " ERROR An invalid operator/operand detected\n";
errorDetected = true;
break;
}
}
if (errorDetected) {
return "INVALID EXPR!";
}
while( !operatorStack.empty() ) {
postFixExpr += operatorStack.top();
operatorStack.pop();
}
return postFixExpr;
}
int main()
{
std::string expr;
std::cout << "Provide an expression such that :"
<< "1. Expression shoule be parenthesis valid.\n"
<< "2. Operators should contain +, -, /, *, ^\n"
<< "3. Operands should contain A-Z or a-z \n"
<< "Your expression is :";
std::getline(std::cin, expr);
std::cout << "Corresponding postfix expression of the express - "
<< expr
<< " is :" << infixToPostFix(expr) << std::endl;
return 0;
}