-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculator.tidy.js
More file actions
60 lines (55 loc) · 1.46 KB
/
BasicCalculator.tidy.js
File metadata and controls
60 lines (55 loc) · 1.46 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
// https://leetcode-cn.com/problems/basic-calculator/
var Test = require('./Common/Test');
var calculate = function (s) {
const tokens = s.replace(/([-/+*()])/g, " $1 ").replace(/ +/g, ' ').trim().split(' ');
tokens.push('$');
const calc = (op, b, a) => {
switch (op) {
case '+': return a + b;
case '-': return a - b;
}
}
const rpn = [];
const ops = [];
for (const token of tokens) {
if (/\d+/.test(token)) {
rpn.push(Number(token));
}
else if (token == '(') {
ops.push(token);
}
else {
while (ops.length > 0) {
const op = ops[ops.length - 1];
if (op == '(') {
if (token == ')') {
ops.pop();
}
break;
}
else {
rpn.push(calc(ops.pop(), rpn.pop(), rpn.pop()));
}
}
if (token != ')') ops.push(token);
}
}
return rpn[0];
};
function test(s) {
Test.test(calculate, s);
}
// test("1 + 1");
// test(" 2-1 + 2 ");
// test("(1+(4+5+2)-3)+(6+8)");
test("2-4-(8+2-6+(8+4-(1)+8-10))");
// test("2-4-(8+2-6+(8+4-(3-2)+8-10))");
// test("2-4-(8+2-6+(8+4-1+8-10))");
// [ -2, 4, 12, 1 ]
// [ '-', '(', '+', '(', '-' ]
// [ -2, 4, 11 ]
// [ '-', '(', '+', '(', '+' ]
// [ -2, 4, 12, 1 ]
// [ '-', '(', '+', '(', '-', '(' ]
// [ -17 ]
// []