-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCalculator.java
121 lines (112 loc) · 3.78 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
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
/*
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
*/
import java.util.*;
public class BasicCalculator {
public static int calculate_wrong(String s) { // fail, only can deal with plus, but if only plua, "(", ")" make no sense.
if(s.length() <= 0) return 0;
int L =s.length();
int num = 0;
Stack<Integer> operands = new Stack<>();
Stack<Character> operator = new Stack<>();
for(int i = 0; i< L; i++){
if(Character.isDigit(s.charAt(i))){
num = num*10 + s.charAt(i) - '0';
//System.out.println( i + ": " + num);
} else if(s.charAt(i) == '('){
operator.push('(');
}else if(s.charAt(i) == ' '){
continue;
}else if(s.charAt(i) == '+' || s.charAt(i) == '-'){
//System.out.println(i + ":" + s.charAt(i));
operator.push(s.charAt(i));
operands.push(num);
num = 0;
}
if(s.charAt(i) == ')' || i == L - 1){
while(!operator.isEmpty() && operator.peek() != '('){
int num2 = operands.pop();
char sign = operator.pop();
System.out.println("num : " + num);
System.out.println("num2 : " + num2);
System.out.println("sign : " + sign);
if(sign=='+'){
num = num+num2;
}else if(sign == '-'){
num = num2 - num;
}
}
if(!operator.isEmpty() && operator.peek() == '(') operator.pop();
System.out.println("cal result : " + num);
}
}
if(operands.isEmpty()){
return num;
}else{
while(!operands.isEmpty()){
int num2 = operands.pop();
char sign = operator.pop();
if(sign=='+'){
num = num+num2;
}else if(sign == '-'){
num = num2 - num;
}
}
return num;
}
}
//https://leetcode.com/discuss/39553/iterative-java-solution-with-stack
public static int calculate(String s) {
if(s.length() <= 0) return 0;
int L =s.length();
int num = 0;
int neg = 1;
int result = 0;
Stack<Integer> st = new Stack<>();
for(int i = 0; i< L; i++){
System.out.println(i + " : " + num);
if(Character.isDigit(s.charAt(i))){
num = num*10 + s.charAt(i) - '0';
}else if(s.charAt(i) == ' '){
continue;
}else if(s.charAt(i) == '+'){
result += neg*num;
neg = 1;
num = 0;
}else if(s.charAt(i) == '-'){
result += neg*num;
neg = -1;
num = 0;
}else if(s.charAt(i) == '('){
st.push(result);
st.push(neg);
result = 0;
neg = 1;
}else if(s.charAt(i) == ')'){
result += neg*num;
result *= st.pop();
result += st.pop();
neg = 1;
num = 0;
}
}
if(num != 0) result += neg * num;
return result;
}
public static void main(String[] args) {
//String s = "(1 +2 + (3) + 1)";
String s = "1 - (2-3 + 5)";
//String s = "5 ";
int res = calculate(s);
System.out.println("String: " + s);
System.out.println("Resulte: " + res);
return;
}
}