forked from rchen8/algorithms
/
Polish.java
178 lines (155 loc) · 4.44 KB
/
Polish.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
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Polish {
private static boolean isOperator(String s) {
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")
|| s.equals("%") || s.equals("^");
}
private static int precedence(String s) {
switch (s) {
case "+":
case "-":
return 0;
case "*":
case "/":
return 1;
case "^":
return 2;
}
return -1;
}
private static int evaluate(int left, int right, String operator) {
switch (operator) {
case "+":
return left + right;
case "-":
return left - right;
case "*":
return left * right;
case "/":
return left / right;
case "%":
return left % right;
case "^":
return (int) Math.round(Math.pow(left, right));
}
return -1;
}
private static String prefixToInfix(String[] prefix, boolean evaluate) {
Stack<String> stack = new Stack<>();
for (int i = prefix.length - 1; i >= 0; i--) {
if (prefix[i].matches("\\w+"))
stack.push(prefix[i]);
else {
if (evaluate) {
int left = Integer.parseInt(stack.pop());
int right = Integer.parseInt(stack.pop());
stack.push("" + evaluate(left, right, prefix[i]));
} else {
String left = stack.pop();
String right = stack.pop();
stack.push("( " + left + " " + prefix[i] + " " + right
+ " )");
}
}
}
return stack.peek();
}
private static String postfixToInfix(String[] postfix, boolean evaluate) {
Stack<String> stack = new Stack<>();
for (int i = 0; i < postfix.length; i++) {
if (postfix[i].matches("\\w+")) {
stack.push(postfix[i]);
} else {
if (evaluate) {
int right = Integer.parseInt(stack.pop());
int left = Integer.parseInt(stack.pop());
stack.push("" + evaluate(left, right, postfix[i]));
} else {
String right = stack.pop();
String left = stack.pop();
stack.push("( " + left + " " + postfix[i] + " " + right
+ " )");
}
}
}
return stack.pop();
}
private static String infixToPrefix(String[] infix) {
Stack<String> stack = new Stack<>();
Stack<String> queue = new Stack<>();
for (int i = infix.length - 1; i >= 0; i--) {
if (infix[i].matches("\\w+"))
queue.push(infix[i]);
else if (isOperator(infix[i])) {
while (!stack.isEmpty()
&& (!infix[i].equals("^")
&& precedence(infix[i]) < precedence(stack
.peek()) || infix[i].equals("^")
&& precedence(infix[i]) <= precedence(stack
.peek())))
queue.push(stack.pop());
stack.push(infix[i]);
} else if (infix[i].equals(")"))
stack.push(infix[i]);
else if (infix[i].equals("(")) {
while (!stack.peek().equals(")"))
queue.push(stack.pop());
stack.pop();
}
}
while (!stack.isEmpty())
queue.push(stack.pop());
String prefix = "";
while (!queue.isEmpty())
prefix += queue.pop() + " ";
return prefix.trim();
}
private static String infixToPostfix(String[] infix) {
Stack<String> stack = new Stack<>();
Queue<String> queue = new LinkedList<>();
for (int i = 0; i < infix.length; i++) {
if (infix[i].matches("\\w+"))
queue.add(infix[i]);
else if (isOperator(infix[i])) {
while (!stack.isEmpty()
&& (!infix[i].equals("^")
&& precedence(infix[i]) <= precedence(stack
.peek()) || infix[i].equals("^")
&& precedence(infix[i]) < precedence(stack
.peek())))
queue.add(stack.pop());
stack.push(infix[i]);
} else if (infix[i].equals("("))
stack.push(infix[i]);
else if (infix[i].equals(")")) {
while (!stack.peek().equals("("))
queue.add(stack.pop());
stack.pop();
}
}
while (!stack.isEmpty())
queue.add(stack.pop());
String postfix = "";
while (!queue.isEmpty())
postfix += queue.poll() + " ";
return postfix.trim();
}
public static void main(String[] args) {
System.out.println(prefixToInfix("* - + 3 5 4 / + 4 6 2".split("\\s+"),
true));
System.out.println(prefixToInfix("+ - + A B * C D ^ E F".split("\\s+"),
false));
System.out.println(postfixToInfix("12 5 - 4 5 3 + - *".split("\\s+"),
true));
System.out.println(postfixToInfix("6 7 * 9 + 8 2 * -".split("\\s+"),
false));
System.out.println();
System.out.println(infixToPrefix("A + B * C - D".split("\\s+")));
System.out.println(infixToPostfix("A + B * C - D".split("\\s+")));
System.out.println();
System.out.println(infixToPrefix("A ^ B ^ C".split("\\s+")));
System.out.println(infixToPostfix("A ^ B ^ C".split("\\s+")));
}
}