-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathBasicCalculator.java
More file actions
70 lines (63 loc) · 2.22 KB
/
BasicCalculator.java
File metadata and controls
70 lines (63 loc) · 2.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.programmers;
import java.util.ArrayList;
import java.util.Stack;
public class BasicCalculator {
public String doCalculate(String expression) {
int answer = toPostfix(expression);
System.out.println(answer);
return expression + " = " + String.valueOf(answer);
}
private int getPriority(String operator) {
if (operator.equals("(")) return 0;
else if (operator.equals("+") || operator.equals("-")) return 1;
else return 2;
}
private int toPostfix(String str) {
String[] strArr = str.split(" ");
ArrayList<String> output = new ArrayList<>();
Stack<String> st = new Stack<>();
for (int i = 0; i < strArr.length; i++) {
String now = strArr[i];
switch (now) {
case "(" -> st.push(now);
case ")" -> {
while (!st.isEmpty()) {
if (st.peek().equals("(")) {
st.pop();
break;
}
output.add(st.pop());
}
}
case "+", "-", "*", "/" -> {
while (!st.isEmpty() && getPriority(st.peek()) >= getPriority(now)) {
output.add(st.pop());
}
st.push(now);
}
default -> output.add(now);
}
}
while (!st.isEmpty()) output.add(st.pop());
return getResult(output);
}
public int getResult(ArrayList<String> list) {
Stack<Integer> st = new Stack<>();
for (int i = 0; i < list.size(); i++) {
String now = list.get(i);
if (!now.equals("+") && !now.equals("-") && !now.equals("*") && !now.equals("/")){
st.push(Integer.parseInt(now));
} else {
int x = st.pop();
int y = st.pop();
switch (now) {
case "+" -> st.push(y + x);
case "-" -> st.push(y - x);
case "*" -> st.push(y * x);
case "/" -> st.push(y / x);
}
}
}
return st.pop();
}
}