-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infix to Postfix Using Stack
155 lines (128 loc) · 3.81 KB
/
Infix to Postfix Using Stack
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
//Considered only for () type brackets
//Works for only valid Infix expression
#include<bits/stdc++.h>
using namespace std;
int precedence(char c)
{
if(c=='^')
return 3;
else if(c=='*' || c=='/')
return 2;
else if(c=='+' || c=='-')
return 1;
return -1;
}
int main()
{
string infix;
cout<<"Enter Infix : "<<endl;
cin >> infix;
string postfix;
stack<char>counter;
counter.push('#');
//Length of expression
int len=infix.length();
for(int i=0;i<len;i++)
{
//If operand find add directly to postfix
if((infix[i]>=65 && infix[i]<=90) || (infix[i]>=97 && infix[i]<=122))
{
postfix+=infix[i];
}
else if(infix[i]=='(')
{
counter.push(infix[i]); //If opening bracket found add directly to stack
}
else if(infix[i]==')')
{
//If closing bracket is found pop stack untill opening bracket found and concatenate popped
// characters in postfix
while(counter.top()!='#' && counter.top()!='(')
{
char c=counter.top();
counter.pop();
postfix+=c;
}
//delete latest opening bracket
if(counter.top()=='(')
{
char c=counter.top();
counter.pop();
}
}
else{
//If operator found pop top operators of precedence greater than equal to current and concatenate
// to postfix
while(counter.top()!='#' && precedence(infix[i])<=precedence(counter.top()))
{
char c=counter.top();
counter.pop();
postfix+=c;
}
counter.push(infix[i]);
}
}
// concatenate all remaining operands
while(counter.top() != '#')
{
char c = counter.top();
counter.pop();
postfix += c;
}
cout<<endl<<"Postfix"<<endl;
cout<<postfix;
return 0;
}
// Explanation
/*
*ALGORITHM*
1-> If the scanned character is an operand, output it.
2-> Else,
…..1) If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
…..2) Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
3-> If the scanned character is an ‘(‘, push it to the stack.
4-> If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
5-> Repeat steps 2-6 until infix expression is scanned.
6-> Pop and output from the stack until it is not empty.
7. Print the output
consider the Infix : (A+B)+C*D/E
len=11
when i=0
postfix="";
counter= (
i=1
postfix="A"
counter= (
i=2
postfix="A"
counter= (+
i=3
postfix="AB"
counter= (+
i=4
postfix="AB+"
counter=
i=5
postfix="AB+"
counter= +
i=6
postfix="AB+C"
counter= +
i=7
postfix="AB+C"
counter= +*
i=8
postfix="AB+CD"
counter= +*
i=9
postfix="AB+CD*"
counter= +/
i=10
postfix="AB+CD*E"
counter= +/
i=11
out
postfix="AB+CD*E/+"
counter =
FINAL : postfix="AB+CD*E/+"
*/