-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode227_BasicCalculator.java
More file actions
109 lines (92 loc) · 3.8 KB
/
Copy pathleetcode227_BasicCalculator.java
File metadata and controls
109 lines (92 loc) · 3.8 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
106
107
108
109
import java.util.Stack;
import java.util.StringTokenizer;
public class leetcode227_BasicCalculator {
int compute(int num1, int num2, char ch) {
if(ch == '+')
return num1 + num2;
else if (ch == '-')
return num1 - num2;
else if(ch == '*')
return num1*num2;
else
return num1/num2;
}
public int calculate(String s) {
StringBuilder strToken = new StringBuilder();
char ch,operator;
int operand1, operand2,res=0;
StringBuilder str = new StringBuilder();
//StringTokenizer stringTokenizer = new StringTokenizer(s,"\\\\s+");
String[] strArr = s.split("\\s+");
Stack<String> operands = new Stack<>();
Stack<Character> operators = new Stack<>();
//if(strArr.length ==1 )
// return Integer.parseInt(strArr[0]);
for(String st : strArr) {
str.append(st);
}
//StringTokenizer stringTokenizer = new StringTokenizer(s,"+-*/");
for(int i=0;i<str.length();i++) {
ch = str.charAt(i);
//character is operand
if(ch != '+' && ch != '-' && ch != '*' && ch != '/') {
strToken.append(ch);
//operands.push(ch);
}
//character is operator
else {
operands.push(strToken.toString());
strToken.setLength(0);
if(operators.isEmpty()) {
operators.add(ch);
continue;
}
if((ch == '*' || ch == '/') && (operators.peek() == '/' || operators.peek() == '*')) {
operand1 = Integer.parseInt(operands.pop());
operand2 = Integer.parseInt(operands.pop());
operator = operators.pop();
res = compute(operand2,operand1,operator);
operands.push(Integer.toString(res));
operators.push(ch);
}
else if ((ch == '*' || ch == '/') && (operators.peek() == '+' || operators.peek() == '-')) {
operators.push(ch);
}
else if( (ch == '+' || ch == '-') && (operators.peek() == '+' || operators.peek() == '-')) {
operand1 = Integer.parseInt(operands.pop());
operand2 = Integer.parseInt(operands.pop());
operator = operators.pop();
res = compute(operand2,operand1,operator);
operands.push(Integer.toString(res));
operators.push(ch);
}
else if((ch == '+' || ch == '-') && (operators.peek() == '*' || operators.peek() == '/')) {
operand1 = Integer.parseInt(operands.pop());
operand2 = Integer.parseInt(operands.pop());
operator = operators.pop();
res = compute(operand2,operand1,operator);
operands.push(Integer.toString(res));
operators.push(ch);
}
}
}
operands.push(strToken.toString());
if(operands.size() == 1)
return Integer.parseInt(operands.pop());
while(!operators.isEmpty()) {
operand1 = Integer.parseInt(operands.pop());
operand2 = Integer.parseInt(operands.pop());
operator = operators.pop();
res = compute(operand2,operand1,operator);
operands.push(Integer.toString(res));
}
//System.out.println(res);
return res;
}
public static void main(String[] args) {
String str = "2-3+4";
leetcode227_BasicCalculator obj = new leetcode227_BasicCalculator();
int res = obj.calculate(str);
System.out.println(res);
}
}