-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleStackBasedCalculator.java
More file actions
192 lines (179 loc) · 7.19 KB
/
DoubleStackBasedCalculator.java
File metadata and controls
192 lines (179 loc) · 7.19 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
*
* Copyright 2016 Xiaofei
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package xiaofei.algorithm;
import java.util.Stack;
/**
* Created by Xiaofei on 16/5/28.
*
* 我又在星巴克一个人写代码了,五月周末都是这么过来的,好忧伤。
*
* 双栈计算器是一个很常见的算法题,第一次接触是在2004年。记得华为校园招聘的时候,也出现了同样的上机题。
*
* 我当时看时间充裕,就蛋疼地写了一个编译器来求解,最搞笑的是四个上机题,我每个题目都是没编译,写好代码后直接上传,居然每个题目都是直接AC。
*
* 感觉特别厉害,然后并没有什么卵用。
*
* 华为上机并不是acm,他妈的没有罚时,所以做题速度不影响得分。几次提交之后AC也不影响得分。
*
* 算了,直接进入正题。我其实都忘了这个题目用双栈要怎么解决了。现场分析一下,顺便看看我的思路怎么样。
*
* 2004年的时候这题目解决的方法是给运算符定义优先级,然后看优先级pop。细节全忘了,好像最后要有一个特殊字符。
*
* 现在这样想。
* 如果是加减号,pop之前的加减乘除号,直到前括号。
* 如果是乘除号,pop之前的乘除号,直到碰到加减号。
* 如果是前括号,直接push。
* 如果是后括号,那么pop直到前括号。
*
* 进一步细化
* 如果是加减号,pop之前的加减乘除号,直到前括号和空。
* 如果是乘除号,pop之前的乘除号,直到碰到加减号和前括号和空。
* 如果是前括号,直接push。
* 如果是后括号,那么pop直到前括号。
*
* 这他妈的怎么定义优先级???
* 算了,直接if判断吧
*
*
*/
public class DoubleStackBasedCalculator {
private static Stack<Integer> numberStack = new Stack<>();
private static Stack<Character> operatorStack = new Stack<>();
private static int pos;
private static int type;
private static String input;
private static Wrapper getNext() {
int length = input.length();
int number = 0;
while (pos < length) {
char ch = input.charAt(pos);
if ('0' <= ch && ch <= '9') {
if (type == 2) {
type = 1;
number = 0;
}
number = number * 10 + ch - '0';
++pos;
} else {
if (type == 1) {
type = 2;
return new Wrapper(number);
} else {
++pos;
return new Wrapper(ch);
}
}
}
return null;
}
private static int solveInternal() {
Wrapper wrapper;
while (true) {
wrapper = getNext();
if (wrapper.type == 1) {
numberStack.push(wrapper.number);
} else if (wrapper.operator != '#') {
if (wrapper.operator == '+' || wrapper.operator == '-') {
char ch;
while (!operatorStack.isEmpty() && (operatorStack.peek()) != '(') {
ch = operatorStack.pop();
if (ch == '+') {
numberStack.push(numberStack.pop() + numberStack.pop());
} else if (ch == '-') {
numberStack.push(-numberStack.pop() + numberStack.pop());
} else if (ch == '*') {
numberStack.push(numberStack.pop() * numberStack.pop());
} else {
int n1 = numberStack.pop(), n2 = numberStack.pop();
numberStack.push(n2 / n1);
}
}
operatorStack.push(wrapper.operator);
} else if (wrapper.operator == '*' || wrapper.operator == '/') {
char ch;
while (!operatorStack.isEmpty() && ((ch = operatorStack.peek()) == '*' || ch == '/')) {
ch = operatorStack.pop();
if (ch == '*') {
numberStack.push(numberStack.pop() * numberStack.pop());
} else {
int n1 = numberStack.pop(), n2 = numberStack.pop();
numberStack.push(n2 / n1);
}
}
operatorStack.push(wrapper.operator);
} else if (wrapper.operator == '(') {
operatorStack.push(wrapper.operator);
} else if (wrapper.operator == ')') {
char ch;
while ((ch = operatorStack.pop()) != '(') {
if (ch == '+') {
numberStack.push(numberStack.pop() + numberStack.pop());
} else if (ch == '-') {
numberStack.push(-numberStack.pop() + numberStack.pop());
} else if (ch == '*') {
numberStack.push(numberStack.pop() * numberStack.pop());
} else {
int n1 = numberStack.pop(), n2 = numberStack.pop();
numberStack.push(n2 / n1);
}
}
}
} else {
char ch;
while (!operatorStack.isEmpty()) {
ch = operatorStack.pop();
if (ch == '+') {
numberStack.push(numberStack.pop() + numberStack.pop());
} else if (ch == '-') {
numberStack.push(-numberStack.pop() + numberStack.pop());
} else if (ch == '*') {
numberStack.push(numberStack.pop() * numberStack.pop());
} else {
int n1 = numberStack.pop(), n2 = numberStack.pop();
numberStack.push(n2 / n1);
}
}
break;
}
}
return numberStack.pop();
}
public static int solve(String input) {
numberStack.clear();
operatorStack.clear();
pos = 0;
type = 2;
DoubleStackBasedCalculator.input = input + "#";
return solveInternal();
}
private static class Wrapper {
int type;
int number;
char operator;
Wrapper(int number) {
type = 1;
this.number = number;
operator = ' ';
}
Wrapper(char operator) {
type = 2;
number = 0;
this.operator = operator;
}
}
}