-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_calc.go
43 lines (39 loc) · 1.36 KB
/
basic_calc.go
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
package problem0224
/*
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
*/
func calculate(s string) int {
var stack = []int{} // We will use this slice a stack for order of operations
var curNum, result, sign int
sign = 1 // Sign will be 1 or -1 depending on positive or negative
for i := range s {
// If we see a digit
if s[i] >= '0' && s[i] <= '9' {
// Add it to the end of curNum
curNum = 10*curNum + int(s[i]-'0')
continue
}
switch s[i] {
case '+':
result += sign * curNum // Execute last operation
sign, curNum = 1, 0 // Create a new positive number
case '-':
result += sign * curNum // Execute last operation
sign, curNum = -1, 0 // Create a new negative number
case '(':
stack = append(stack, result, sign) // Stash the result and sign so far
sign, result = 1, 0
case ')':
result += sign * curNum // Execute last operation
curNum = 0
result *= stack[len(stack)-1] // Apply stashed sign
result += stack[len(stack)-2] // Apply stashed result
stack = stack[:len(stack)-2] // Remove from stack
}
}
if curNum != 0 { // Execute last operation if there is one
result += sign * curNum
}
return result
}