-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathPostfixConverter.java
More file actions
28 lines (24 loc) · 926 Bytes
/
PostfixConverter.java
File metadata and controls
28 lines (24 loc) · 926 Bytes
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
package com.programmers.engine.module.convert;
import com.programmers.engine.model.Operator;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class PostfixConverter {
public List<String> convertInfixToPostfix(String expression) {
String[] strArr = expression.split(" ");
List<String> output = new ArrayList<>();
Stack<String> st = new Stack<>();
for (String now: strArr) {
if (now.equals("+") || now.equals("-") || now.equals("*") || now.equals("/")) {
while (!st.isEmpty() && Operator.matchOperator(st.peek()).getPriority() >= Operator.matchOperator(now).getPriority()) {
output.add(st.pop());
}
st.push(now);
continue;
}
output.add(now);
}
while (!st.isEmpty()) output.add(st.pop());
return output;
}
}