-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_calculator.java
More file actions
105 lines (97 loc) · 3.71 KB
/
basic_calculator.java
File metadata and controls
105 lines (97 loc) · 3.71 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
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
import java.util.Stack;
public class basic_calculator {
public static int calculate(String s) {
if (s == null || s.length() == 0) return 0;
Stack<Integer> nums = new Stack<>();
Stack<Character> ops = new Stack<>();
int num = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ') continue;
if (Character.isDigit(c)) {
num = c - '0';
while (i < s.length() - 1 && Character.isDigit(s.charAt(i+1))) {
num = num * 10 + (s.charAt(i+1) - '0');
i++;
}
nums.push(num);
num = 0;
} else if (c == '(') {
ops.push(c);
} else if (c == ')') {
while (ops.peek() != '(') nums.push(operation(ops.pop(), nums.pop(), nums.pop()));
ops.pop(); // get rid of '('
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (!ops.isEmpty() && precedence(c, ops.peek())) nums.push(operation(ops.pop(), nums.pop(),nums.pop()));
ops.push(c);
}
}
while (!ops.isEmpty()) {
nums.push(operation(ops.pop(), nums.pop(), nums.pop()));
}
return nums.pop();
}
private static int operation(char op, int b, int a) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b; // assume b is not 0
}
return 0;
}
private static boolean precedence(char op1, char op2) {
if (op2 == '(' || op2 == ')') return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) return false;
return true;
}
// https://leetcode.com/problems/basic-calculator/discuss/
public static int calculate2(String s) {
if (s == null || s.length() == 0) return 0;
Stack<Integer> nums = new Stack<>();
Stack<Character> ops = new Stack<>();
int num = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ') continue;
if (Character.isDigit(c)) {
num = c - '0';
while (i < s.length() - 1 && Character.isDigit(s.charAt(i+1))) {
num = num * 10 + (s.charAt(i+1) - '0');
i++;
}
nums.push(num);
num = 0;
} else if (c == '(') {
ops.push(c);
} else if (c == ')') {
while (ops.peek() != '(') nums.push(operation2(ops.pop(), nums.pop(), nums.pop()));
ops.pop(); // get rid of '('
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
while (!ops.isEmpty() && precedence2(c, ops.peek())) nums.push(operation2(ops.pop(), nums.pop(),nums.pop()));
ops.push(c);
}
}
while (!ops.isEmpty()) {
nums.push(operation2(ops.pop(), nums.pop(), nums.pop()));
}
return nums.pop();
}
private static int operation2(char op, int b, int a) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
// case '*': return a * b;
// case '/': return a / b; // assume b is not 0
}
return 0;
}
private static boolean precedence2(char op1, char op2) {
if (op2 == '(' || op2 == ')') return false;
// if ((op1 == '*' || op1 == '/') && op2 == '+' || op2 == '-') return false;
return true;
}
public static void main(String[] args) {
System.out.println(calculate("100*(2+12)/14"));
}
}