-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixToPostfix.cpp
More file actions
120 lines (112 loc) · 3.64 KB
/
infixToPostfix.cpp
File metadata and controls
120 lines (112 loc) · 3.64 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
/*
Author @ Pradeep Kumar
Objective : To Convert Infix Expression To Postfix Expression
Function :-
1) getWeight(char)
-> Return a precedence of a operator and default means it may be a oprend
Input Parameter -> None
2) infixToPostfix(char,char,int)
-> Function that Actual convert infix to postfix expression
passing parameter ->
infix -> character type variable that store infix expression enter by user
postfix ->character type variable that store postfix expression after computation
size -> integer type variable store the size of expression
Other parameter ->
i and k -> use for index purposing
ch -> charecter that store a operend and operator of the expression
Return ->None
Result: a postfix expression
*/
// Approach : Itrative
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int getWeight(char ch) //Checking precedence of operator
{
switch (ch)
{
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default : return 0; // means it may be a operator
}
}
void infixToPostfix(char infix[], char postfix[], int size) // convert infix expression to postfix using a stack
{
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
// iterate over the infix expression
while (i < size) {
ch = infix[i];
if (ch == '(') {
// simply push the opening parenthesis
s.push(ch);
i++;
continue;
}
if (ch == ')') {
// if we see a closing parenthesis,
// pop of all the elements and append it to
// the postfix expression till we encounter
// a opening parenthesis
while (!s.empty() && s.top() != '(') {
postfix[k++] = s.top();
s.pop();
}
// pop off the opening parenthesis also
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
weight = getWeight(ch);
if (weight == 0) {
// we saw an operand
// simply append it to postfix expression
postfix[k++] = ch;
}
else {
// we saw an operator
if (s.empty()) {
s.push(ch); // simply push the operator onto stack if stack is empty
}
else {
// pop of all the operators from the stack and append it to the postfix expression till we
// see an operator with a lower precedence that the current operator
while (!s.empty() && s.top() != '(' &&
weight <= getWeight(s.top())) {
postfix[k++] = s.top();
s.pop();
}
// push the current operator onto stack
s.push(ch);
}
}
i++;
}
while (!s.empty())
{
postfix[k++] = s.top(); // pop of the remaining operators present in the stack and append it to postfix expression
s.pop();
}
postfix[k] = 0; // null terminate the postfix expression
}
// main
int main() {
char infix[50] ;
cout<<"enter the infix expression"<<endl;
cin>>infix;
int size = strlen(infix);
char postfix[size];
infixToPostfix(infix,postfix,size);
cout<<"\nInfix Expression :: "<<infix;
cout<<"\nPostfix Expression :: "<<postfix;
cout<<endl;
return 0;
}