-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathBasic Calculator.js
53 lines (43 loc) · 1.27 KB
/
Basic Calculator.js
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
/**
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.
*/
/**
* @param {string} s
* @return {number}
*/
var calculate = function(s) {
var stack = [],
len = s.length,
sum = 0,
num,
ch,
j,
i;
stack.push(1);
stack.push(1);
for (i = 0; i < len; i++) {
ch = s.charAt(i);
if (!isNaN(parseInt(ch))) {
num = parseInt(ch);
for (j = i + 1; j < len && !isNaN(parseInt(s.charAt(j))); j++) {
num = num * 10 + parseInt(s.charAt(j));
}
sum += stack.pop() * num;
i = j - 1;
} else if (ch === '+' || ch === '(') {
stack.push(stack[stack.length - 1]);
} else if (ch === '-') {
stack.push(stack[stack.length - 1] * (-1));
} else if (ch === ')') {
stack.pop();
}
}
return sum;
};