forked from tannuchoudhary/Data-structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infix_postfix_cnv.cpp
197 lines (139 loc) · 3.88 KB
/
infix_postfix_cnv.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
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
using namespace std;
class operatorStack
{
private:
char item[SIZE];
int top;
public:
/*Function prototype*/
void push (char);
char pop ();
int isEmpty();
int prcd(char, char);
void convert(char[], char[]);
/*Constructor for initialization*/
operatorStack ()
{
top = -1;
}
};
/*Function definition*/
void operatorStack :: push(char value)
{
if (top == SIZE -1)
{
cout<<"Stack overflow"<<endl;
return;
}
else
top++;
item[top] = value;
}
char operatorStack :: pop()
{
if(isEmpty())
{
cout<<"Stack underflow"<<endl;
return 9;
}
int value;
value = item[top];
top--;
return value;
}
int operatorStack :: isEmpty() /*It will return '1' if the stack is empty and will return '0' if not*/
{
return (top == -1);
}
/*Prcd function to check the precedence of the operator present in the top of the stack and the next token*/
/*It will return 1 if the operator in top of the stack will have more priority then the next token else 0*/
int operatorStack :: prcd(char left, char right)
{
if(left == '(' || right == '(')
return 0;
if (right == ')')
return 1;
if (left == '*' || left == '/')
{
if(right == '*' || right == '/' || right == '+' || right == '-')
return 1;
else
return 0;
}
if(left == '+' || left == '-')
{
if (right == '+' || right == '-')
return 1;
else
return 0;
}
if(left == '$' && right == '$')
{
return 0;
}
if(left == '$')
{
return 1;
}
else
return 0;
}
/*This convert function will convert the infix expression into postfix expression*/
void operatorStack :: convert(char infix[], char postfix[])
{
operatorStack stack;
int i=0, j=0;
while(infix[i] != '\0')
{
char token = infix[i];
/*If the token is an operand it will be simply appended to the postfix expression*/
if(token >= '0' && token <='9')
{
postfix[j++] = token;
}
/*If the token is an operator it will be checked that either the stack is empty or not*/
else if(token == '+' || token == '-' || token == '*' || token == '/' || token == '$')
{
/*If the stack is empty and prcd function is returning 1 then operator will be appended to the postfix string*/
while(stack.top !=-1 && prcd(stack.item[stack.top], token))
{
char top_opr = stack.pop();
postfix[j++] = top_opr;
}
/*If we will have both opening and closing parenthesis in the stack top and toke then they will be discarded*/
if(token == ')')
stack.pop();
/*And if the stack is empty or prcd function is returning 0 then the operator will be pushed into the stack*/
else
stack.push(token);
}
else
{
cout<<"Invalid symbol "<<token<<" encountered "<<endl;
exit(1);
}
i++;
}
/*When the stack will contain only one operator then it will be popped out and appended to the postfix string*/
while(stack.top !=-1)
{
char top_opr = stack.pop();
postfix[j++] = top_opr;
}
postfix[j] = '\0';
}
int main()
{
operatorStack s1;
char infix[SIZE];
char postfix[SIZE];
cout<<"Enter an infix string : "<<endl;
cin>>infix;
s1.convert(infix, postfix);
cout<<"The converted postfix string is"<<postfix;
return 0;
}