-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62nd_stack_inf_postf.cpp
More file actions
138 lines (121 loc) · 3.23 KB
/
62nd_stack_inf_postf.cpp
File metadata and controls
138 lines (121 loc) · 3.23 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
/*
(Ek baat yaad rakhna hai ki jiska precedence jyada wahi stack ke top me rah sakta hai jo jyada powerful wahi raja)
agar koi operator jiska precedence stack ke top ke operator se kam hai to wo nahi reh payega
In this code I have tried to convert an infix expression to postfix expression using a technique in which the
operator has different precedence when it is outside the stack and different precedence when it is inside the stack depending on
associativity and precendence
When operator is Left to Right associative its precedence increases from out stack to instack
but,
when an operator is right to left associative its precedence decreases from outstack to instack
(as in case of ^ operator)
For example :
----------------------------------------------------------------------------
SYMBOL OUT STACK PRECEDENCE IN STACK PRECEDENCE
+ - 1 2
* / 3 4
^ 6 5
( 7 0
) 0 ?
-----------------------------------------------------------------------------
*/
#include<iostream>
using namespace std;
char stack[1000];
int top=-1;
int isEmpty()
{
if(top==-1)
return 1;
else return 0;
}
void push(char x)
{
stack[top+1]=x;
top=top+1;
}
char pop()
{
if(!isEmpty())
{
top=top-1;
return stack[top+1];
}
}
char peek()
{
return stack[top];
}
int isOperator(char x)
{
if(x=='+'||x=='-'||x=='*'||x=='/'||x=='^'||x=='('||x==')')
return 1;
else
return 0;
}
int inStackPrecedence(char x)
{
if(x=='+'||x=='-')
return 2;
else if(x=='*' || x=='/')
return 4;
else if(x=='^')
return 5;
else if(x=='(')
return 0;
}
int outStackPrecedence(char x)
{
if(x=='+'||x=='-')
return 1;
else if(x=='*' || x=='/')
return 3;
else if(x=='^')
return 6;
else if(x=='(')
return 7;
else if(x==')')
return 0;
}
int main()
{
string infix;
cout<<"Enter the infix expression : ";
cin>>infix;
char postfix[infix.length()+1];
int i=0,j=0;
while(i<infix.length())
{
char ch=infix[i];
if(isOperator(ch))
{
if(isEmpty())//when it is the first operator or when the stack is empty
{
push(ch);
i++;
}
else
{
if(inStackPrecedence(peek())>=outStackPrecedence(ch) && peek()!='(')
postfix[j++]=pop();
else if(inStackPrecedence(peek())<outStackPrecedence(ch))
{
push(ch);
i++;
}
else
{
pop();
i++;
}
}
}
else
postfix[j++]=infix[i++];
}
while(top!=-1)
{
postfix[j++]=pop();
}
postfix[j++]='\0';
cout<<"\nPostfix : "<<postfix<<endl;
}