-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix.java
More file actions
278 lines (210 loc) · 5.98 KB
/
postfix.java
File metadata and controls
278 lines (210 loc) · 5.98 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//Q) infix to postfix conversions
import java.io.*;
class postfix{
public char[] fString;
private int max;
private char[] arr;
private int top;
private int TOP;
//Stack implemented by arr character array
public postfix(int s){
max=s;
arr= new char[max];
fString= new char[max];
top=-1;
TOP=-1;
}
public void push(char item) {
top++;
arr[top]=item;
//arr[++top];
}
public char pop() {
char item = arr[top];
top--;
return item;
//return arr[top--];
}
public char peek() {
return arr[top];
}
public boolean isFull() {
return(top==max-1);
//return top==max-1;
}
public boolean isEmpty(){
return top==-1;
}
public void PUSH(char item) {
TOP++;
fString[TOP]=item;
//arr[++top];
}
public char POP() {
char item = fString[TOP];
TOP--;
return item;
//return arr[top--];
}
//The function check the prcedence of operator
public int prec(char x)
{
switch (x)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '$':
return 3;
}
return -1;
}
// The Function convert the infix string to postfix string
public void conversion(char x){
char item;
//operator checking condition
if (x=='+'||x=='-'||x=='*'||x=='/'||x=='$')
{
if(isEmpty())
push(x);
else
{
while(!isEmpty())
{
item = pop();
if(item=='(')
push(item);
else
{
if(prec(item)<prec(x))
push(item);
if(prec(item)>=prec(x))
{
//System.out.println(item);
PUSH(item);
}
}
if(prec(item)<x || prec(item) == '(')
break;
}
push(x);
}
}
// '(' checking condition
else if(x=='(')
{push(x);
}
// ')' checking condition
else if(x==')')
{
while(!isEmpty())
{
item =pop();
if(item == '(')
break;
else
{
// System.out.println(item);
PUSH(item);
}
}
}
else
// condition for operand
{// System.out.println(x);
PUSH(x);
}
}
// MAIN FUNCTION
public static void main(String args[])throws IOException
{
int l,i,x;String str;
char[] arr;
char item;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("*****Converter******");
do{
System.out.println("\n1. Infix -> Postfix \n2.Infix -> Prefix \nPress any number for exit");
x = Integer.parseInt(in.readLine());
switch(x)
{
case 1:
{
System.out.println("Enter the Infix String");
str=in.readLine();
l=str.length();
arr= new char[l];
System.out.println("The Required Postfix string is");
postfix obj=new postfix(l);
//Repeat untill all characters are empty
for(i=0;i<l;i++)
{
obj.conversion(str.charAt(i));
}
//to output the operators remaining in the stack
while(!obj.isEmpty())
{
item = obj.pop();
obj.PUSH(item);
}
for(i=0;i<l;i++)
System.out.println(obj.fString[i]);
}
break;
case 2:
{
System.out.println("Enter the Infix String");
str=in.readLine();
l=str.length();
arr= new char[l];
System.out.println("The Required Prefix string is");
postfix rev=new postfix(l);
//step1 1. Reverse the infix expression.
for(i=0;i<l;i++)
rev.push(str.charAt(i));
for(i=0;i<l;i++)
{
arr[i]=rev.pop();
}
//step2. Make Every '(' as ')' and every ')' as '('.
for(i=0;i<l;i++)
{
if(arr[i]==')')
{
arr[i]='(';
continue;
}
if(arr[i]=='(')
{
arr[i]=')';
}
}
//step 3: Convert expression to postfix form
postfix obj=new postfix(l);
for(i=0;i<l;i++)
{
obj.conversion(arr[i]);
}
while(!obj.isEmpty())
{
item = obj.pop();
obj.PUSH(item);
}
//step 4: TAke the string reversal of postfix form
System.out.println("Final Answer");
for(i=0;i<l;i++)
{
arr[i]=obj.POP();
System.out.println(arr[i]);
}
break;
}
default : System.out.println("Invalid input");
}
}while(x==1||x==2);
}
}