-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_to_Postfix
More file actions
48 lines (44 loc) · 1.22 KB
/
Copy pathInfix_to_Postfix
File metadata and controls
48 lines (44 loc) · 1.22 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
package main;
import java.util.*;
public class infix_postfix {
String InPost(String f) {
// infix to postfix -> done using stack
String output="";
String s = "";
Stack<String> op = new Stack<String>();
char c;
String fix=f;
String pf = "";
for(int i = 0; i<fix.length(); i++) {
c = fix.charAt(i);
if(c!='+'&&c!='-'&&c!='%'&&c!='*'&&c!='/'&&c!='('&&c!=')'&&c!=' ') {
System.out.println("In if : "+c);
pf = pf + c;
}
// else if(c == 6367022399 )
else if(c=='+'||c=='-'||c=='%'||c=='*'||c=='/'||c=='(') {
op.push(c+"");
System.out.println("Stack : "+c);
}
else if(c==')') {
// jab bhi / aata hai, we remove the operator is stack along with ( --- always
System.out.println(" ");
System.out.println(":Stack Now is :::::::::::::::: "+op);
s = op.pop();
op.pop(); // for popping ( with every operator
System.out.println("Exiting : "+s);
if(!s.equals("(")){
output = output +pf+s;
}
pf = "";
}
}
return output;
}
public static void main(String[] args) {
infix_postfix a = new infix_postfix();
// String res = a.InPost("((a-b)*((m+n)/(p+q)))");
String res = a.InPost("((a+b) * (c-d))");
System.out.println("Result is : "+res);
}
}