forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap03.question.23.InfixPostfix.java
140 lines (131 loc) · 5.08 KB
/
Chap03.question.23.InfixPostfix.java
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
//Chap03.question.23.InfixPostfix.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
/**
* + - * / ^
* ( )
* +d -d
*/
public class InfixPostfix {
public static void main(String... args) {
String s1 = "1 + 2 ^ 3 - 4 * ( -3 + +4 ) + 12";
System.out.println(s1);
System.out.println(infix2Postfix(s1.split(" ")));
System.out.println(evaluatePostfix(infix2Postfix(s1.split(" ")).split(" ")));
System.out.println(postfix2Infix(infix2Postfix(s1.split(" ")).split(" ")));
}
public static String postfix2Infix(String[] tokens) {
LinkedList<String> stack = new LinkedList<>();
for (String s : tokens) {
if (!new ArrayList<String>(Arrays.asList("+", "-", "*", "/", "^", "(", ")")).contains(s)) {
stack.push(s);
} else {
if (stack.isEmpty())
throw new IllegalArgumentException();
String b = stack.pop();
if (stack.isEmpty())
throw new IllegalArgumentException();
String a = stack.pop();
stack.push("( " + a + " " + s + " " + b + " )");
}
}
if (stack.size() != 1)
throw new IllegalArgumentException();
return stack.pop();
}
public static String infix2Postfix(String[] tokens) {
LinkedList<String> stack = new LinkedList<>();
ArrayList<String> r = new ArrayList<>();
for (String s : tokens) {
if (!new ArrayList<String>(Arrays.asList("+", "-", "*", "/", "^", "(", ")")).contains(s)) {
r.add(s);
} else {
switch (s) {
case "(":
stack.push(s);
break;
case "+":
case "-":
while (!stack.isEmpty() && !stack.peek().equals("(")) {
r.add(stack.pop());
}
stack.push(s);
break;
case "*":
case "/":
while (!stack.isEmpty() && (stack.peek().equals("*")
|| stack.peek().equals("/") || stack.peek().equals("^"))) {
r.add(stack.pop());
}
stack.push(s);
break;
case "^":
while (!stack.isEmpty() && stack.peek().equals("^")) {
r.add(stack.pop());
}
stack.push(s);
break;
case ")":
while (!stack.peek().equals("(")) {
r.add(stack.pop());
}
stack.pop();
break;
}
}
}
while (!stack.isEmpty())
r.add(stack.pop());
StringBuilder sb = new StringBuilder();
for (String s : r) {
sb.append(s);
sb.append(" ");
}
return sb.toString().trim();
}
public static int evaluatePostfix(String[] tokens) {
LinkedList<String> stack = new LinkedList<>();
for (String s : tokens) {
if (!new ArrayList<String>(Arrays.asList("+", "-", "*", "/", "^")).contains(s)) {
stack.push(s);
} else {
String b, a;
try {
switch (s) {
case "+":
b = stack.pop();
a = stack.pop();
stack.push(String.valueOf(Integer.valueOf(a) + Integer.valueOf(b)));
break;
case "-":
b = stack.pop();
a = stack.pop();
stack.push(String.valueOf(Integer.valueOf(a) - Integer.valueOf(b)));
break;
case "*":
b = stack.pop();
a = stack.pop();
stack.push(String.valueOf(Integer.valueOf(a) * Integer.valueOf(b)));
break;
case "/":
b = stack.pop();
a = stack.pop();
stack.push(String.valueOf(Integer.valueOf(a) / Integer.valueOf(b)));
break;
case "^":
b = stack.pop();
a = stack.pop();
stack.push(String.valueOf((int) (Math.pow(Integer.valueOf(a), Integer.valueOf(b)))));
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (stack.size() != 1)
throw new IllegalArgumentException();
return Integer.valueOf(stack.pop());
}
}