-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixToPostfix.cpp
More file actions
179 lines (158 loc) · 4.55 KB
/
InfixToPostfix.cpp
File metadata and controls
179 lines (158 loc) · 4.55 KB
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
/**
* Author: Hemant Tripathi
*/
//This program will convert infix notation to postfix
//Condition: All numbers must be positive
#include <iostream>
using namespace std;
#define MAXSTACKSIZE 10
#define MAXEXPSIZE 30
class StackOperator {
int top;
public:
string postfix;
char stack[MAXSTACKSIZE];
StackOperator() {
top = -1;
postfix = "";
}
int PushToOpStack(char op);
int PushToPostfix(char op);
int PopFromOpStack();
bool Precedence(char op1, char op2); //This will check if op1 precendence is greater than op2 or not
bool IsOperand(char x);
bool IsOpStackEmpty();
};
int StackOperator::PushToOpStack(char op) {
if(top < MAXSTACKSIZE) {//Stack has space, insert the operator
cout << "Inserted operator into OpStack at positon: " << top << endl;
//If op is ),}or], then pop all the elements to Postfix until ( will not meet
if(op == (char)41) {
cout << "Operator = )";
while(1) {
op = PopFromOpStack();
if(op >= 0) {
if(op == (char)40) {
break;
} else {
PushToPostfix(op);
}
} else {
return -1;
}
}
return 1;
} else {
stack[++top] = op;
cout << "Operator = " << op << endl;
//Now check the precedence of top operator vs below operator
if(top > 0) {
cout << "Checking operator precedence with " << stack[top-1] << endl;
bool isLowOrEqual = Precedence(stack[top], stack[top-1]);
if(isLowOrEqual) {//True, Pop the top and top-1, push top-1 into Postfix and push top back into OpStack, so that top will always be top
cout << "Lower Precedence" << endl;
int topElement = PopFromOpStack();
if(topElement >= 0) {
PushToPostfix(PopFromOpStack());
cout << "Pushing back top element to OpStack: " << (char)topElement << endl;
PushToOpStack((char)topElement);
} else {
cout << "Cannot perform operation. OpStack is emptty!"<<endl;
return -1;
}
} else {
cout << "Higher Precedence"<<endl;
return 1;
}
} else {
cout << "Only one operator in stack at top: " << stack[top] << endl;
return 1;
}
}
} else { //show warning message of stack overflow
cout << "Operator Buffer Overflow. Cannot perform this operation" << endl;
return -1;
}
}
int StackOperator::PushToPostfix(char op) {
postfix = postfix + op;
cout << "Postfix String : " << postfix << endl;
}
int StackOperator::PopFromOpStack() {
if(top >=0) {
cout << "Popping operator: "<< stack[top];
return stack[top--];
} else {
cout << "Operator stack is empty" << endl;
return -1;
}
}
bool StackOperator::Precedence(char op1, char op2) {
cout << "Checking precedence for "<<op1<<" and " << op2<<endl;
if((int)op1 == 40 || (int)op2 == 40 || (int)op1 == 36 || (int)op1 == 94) {
return false;
} else if(((int)op1 == 42 || (int)op1 == 47) && ((int)op2 == 36 || (int)op2 == 94 || (int)op2 == 42 || (int)op2 == 47)) {
return true;
} else if(((int)op1 == 43 || (int)op1 == 45) && ((int)op2 == 36 || (int)op2 == 94 || (int)op2 == 42 || (int)op2 == 47 || (int)op1 == 43 || (int)op1 == 45)) {
return true;
} else {
return false;
}
}
bool StackOperator::IsOperand(char x) {
if(((int)x >= 48 && (int)x <= 57) || ((int)x >= 65 && (int)x <= 90) || ((int)x >= 97 && (int)x <= 122)) {
return true;
} else {
return false;
}
}
bool StackOperator::IsOpStackEmpty() {
if(top >= 0) {
return false;
} else {
return true;
}
}
int main() {
class StackOperator stackOperator;
string input;
char inputarr[50];
bool isValid = true;
cout << "Enter any expression to convert to postfix:" << endl;
cin >> input;
char *ptr;
int counter = 0;
for(auto x:input) {
if(x == '\0') {
break;
}
inputarr[counter++] = x;
}
inputarr[counter] = '\0';
ptr = inputarr;
while(*ptr != '\0') {
cout << "next character: "<<*ptr << endl;
bool isOperand = stackOperator.IsOperand(*ptr);
if(isOperand > 0) {//Operand
cout << "This is and operand. Send to Postfix String." << endl;
stackOperator.PushToPostfix(*ptr);
} else { //Operator
cout << "This is an Operator. Send to Operator Stack." << endl;
int result = stackOperator.PushToOpStack(*ptr);
if(result <= 0) {//some error occured. Stop the process
isValid = false;
break;
}
}
ptr++;
}
if(isValid) {//Append stack operators into postfix string
cout << "==============================================="<<endl;
while(!stackOperator.IsOpStackEmpty()) {
char nextChar = (char)stackOperator.PopFromOpStack();
cout << "Making Postfix. Next Popped character : " << nextChar << endl;
stackOperator.postfix += nextChar;
}
}
cout << "Postfix value: " << stackOperator.postfix << endl;
}