-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathinfix-to-prefix-postfix.cpp
180 lines (170 loc) · 3.53 KB
/
infix-to-prefix-postfix.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
#include <iostream>
#include<string>
using namespace std;
struct stack
{
int top;
int size;
char elements[100];
string str_ele[100];
};
void push_char(struct stack &s,char x)
{
if(s.top==s.size-1)
cout<<"Stack is full \n";
else
{
s.elements[++s.top]=x;
}
}
char peek_char(struct stack &s)
{
if(s.top==-1)
return 'a' ;
else
return s.elements[s.top];
}
char pop_char(struct stack &s)
{
if(s.top==-1)
return 'a' ;
else
return s.elements[s.top--];
}
void push_string(struct stack &s,string x)
{
if(s.top==s.size-1)
cout<<"Stack is full \n";
else
{
s.str_ele[++s.top]=x;
}
}
string peek_string(struct stack &s)
{
if(s.top==-1)
return ".." ;
else
return s.str_ele[s.top];
}
string pop_string(struct stack &s)
{
if(s.top==-1)
return ".." ;
else
return s.str_ele[s.top--];
}
bool isoperator(char a)
{
return (a=='*' || a=='/' || a=='-' || a=='+');
}
bool isbracket(char a)
{
return (a=='(' || a==')'||a=='[' || a==']' || a=='{' ||a=='}' );
}
int operation(char c)
{
switch(c){
case '(' : return 0;
case '+' : return 1;
case '-' : return 2;
case '*' : return 3;
case '/' : return 4;
}
return -1;
}
string ques1(string str)
{
string res="";
int i;
for(i=str.size()-1;i>-1;i--)
res+=str[i];
str=res;
for(i=0;i<str.size();i++)
{
if(str[i]=='(')
str[i]=')';
else if(str[i]==')')
str[i]='(';
}
res="";
struct stack s1;
s1.top=-1;
s1.size=100;
for(i=0;i<str.size();i++)
{
if(!isbracket(str[i]) && !isoperator(str[i]))
res+=str[i];
else if(s1.top==-1 || str[i]=='(')
push_char(s1,str[i]);
else if(str[i]==')')
{
while(peek_char(s1)!='(')
res+=pop_char(s1);
pop_char(s1);
}
else
{
while(operation(str[i])<operation(peek_char(s1)))
{
res+=pop_char(s1);
if(s1.top==-1)
break;
}
push_char(s1,str[i]);
}
}
while(s1.top>=0)
{
res+=pop_char(s1);
}
str="";
for(i=res.size()-1;i>-1;i--)
str+=res[i];
return str;
}
string ques2(string str)
{
int i,n=str.size();
string res="";
struct stack s2;
s2.top=-1;
s2.size=100;
for(i=0;i<n;i++)
{
if(!isbracket(str[i]) && !isoperator(str[i]))
res+=str[i];
else if(s2.top==-1 || str[i]=='(')
push_char(s2,str[i]);
else if(str[i]==')')
{
while(peek_char(s2)!='(')
res+=pop_char(s2);
pop_char(s2);
}
else
{
while(operation(str[i])<=operation(peek_char(s2)))
{
res+=pop_char(s2);
if(s2.top==-1)
break;
}
push_char(s2,str[i]);
}
}
while(s2.top>=0)
{
res+=pop_char(s2);
}
return res;
}
int main()
{
string q1,q2,q3;
cout<<"Enter a infix :\n";
cin>>q1;
cout<<"infix to prefix : "<<ques1(q1)<<"\n";
cout<<"infix to postfix : "<<ques2(q1)<<"\n";
return 0;
}