-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInfixToPostfix.cpp
162 lines (116 loc) · 3.13 KB
/
InfixToPostfix.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
//infix to postfix conversion-using stack
#include<iostream>
#include<stack>
#include<string>
using namespace std; //pulling every function defined in std namespace to global namespace
//function to convert infix to postfix
string InpixToPostfix(string exp);
//function to check if a operator has higher precedence than other
int checkHighPrecedence(char opt1, char opt2);
//function to verify if the char is operator or not
bool isOperator(char c);
//function to check is the scanned input if operand or not
bool isOperand(char c);
//function to conver infix to postfix
string InfixToPostfix(string exp)
{
//declaring a stack from STL
stack<char> S;
string postfix = " " ; //postfix exp as empty string
//scanning character from left to right
for(int i=0;i<exp.length();i++)
{
//if delimitter continue
if(exp[i]==' ' || exp[i]==',') continue;
//if operator being scanned has lower or equal precedence than alraedy in stack ,
//pop all with higher precedence
// push low precedence into stack
else if(isOperator(exp[i])) {
while(!S.empty() && S.top()!='(' && checkHighPrecedence(S.top(),exp[i]) ) {
postfix += S.top(); //add higher precedence opt to postfix expression
S.pop(); //pop higher precedence opt from stack
}
//push lower precedence scanned opt to stack
S.push(exp[i]);
}
//if scanned char is a opening brace= push it to stack
else if(exp[i]=='(') {
S.push(exp[i]);
}
//if an operand , add it to postfix exp
else if(isOperand(exp[i])) {
postfix += exp[i];
}
//if scanned char is a right brace
//pop all from stack and add to postfix until found a opening brace on TOS
else if(exp[i]== ')') {
while(!S.empty() && S.top()!='(') {
postfix += S.top();
S.pop();
}
S.pop();
}
} //end Loop
//Now pop all elements from stack until stack is empty
while(!S.empty()) {
postfix += S.top();
S.pop();
}
return postfix;
}
bool isOperand(char c) {
if(c >='0' && c <= '9') return true;
if(c>= 'a' && c<='z') return true;
if(c>= 'A' && c<= 'Z') return true;
return false;
}
bool isOperator(char c) {
if(c == '+' || c == '-' || c == '*' || c=='/' || c ==' $' )
return true;
return false;
}
//function to assign weights to operators
int optWeight(char op) {
int weight=-1;
switch(op) {
case '+':
case '-':
weight=1;
break;
//higher weight to mul and division
case '*' :
case '/':
weight=2;
break;
case '$' :
weight=3;
break;
}
return weight;
}
//to verify whether an operator is right associative or not
int IsRightAssociative(char op)
{
if(op == '$') return true;
return false;
}
int checkHighPrecedence(char opt1, char opt2)
{
//getting weights of the operator arguments
int optWt1=optWeight(opt1);
int optWt2 = optWeight(opt2);
if(optWt1==optWt2) {
if(IsRightAssociative(opt1)) return false;
else return true;
}
return optWt1 > optWt2 ? true:false ;
}
//the main function
int main() {
string infix;
cout<<"Enter the infix empression" <<endl;
getline(cin,infix);
string postfix = InfixToPostfix(infix);
cout<<"The postfix expression is :"<<postfix<<endl;
return 0;
}