-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCalculator.java
executable file
·47 lines (43 loc) · 1.54 KB
/
BasicCalculator.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
import java.util.Stack;
public class BasicCalculator {
public int calculate(String s) {
int length = s.length();
int operator = +1;
int answer = 0;
int currentNumber = 0;
Stack<Integer> stack = new Stack<>();
for(int i=0; i<length; i++){
if(Character.isDigit(s.charAt(i))){
currentNumber = s.charAt(i) - '0';
while(i+1 < length && Character.isDigit(s.charAt(i+1))){
currentNumber = currentNumber*10 + s.charAt(++i) - '0';
}
currentNumber = currentNumber * operator;
answer += currentNumber;
currentNumber = 0;
} else if(s.charAt(i) == '+'){
operator = +1;
} else if(s.charAt(i) == '-'){
operator = -1;
} else if(s.charAt(i) == '('){
stack.push(answer);
stack.push(operator);
answer = 0;
currentNumber = 0;
operator = +1;
} else if(s.charAt(i)== ')'){
int prevSign = stack.pop();
answer = prevSign * answer;
int prevValue = stack.pop();
answer = answer + prevValue;
}
}
return answer;
}
public static void main(String[] args) {
String s = "(1+(4+5+2)-3)+(6+8)";
BasicCalculator basicCalculator = new BasicCalculator();
int result = basicCalculator.calculate(s);
System.out.println(result);
}
}