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
| var opts = map[rune]int{ '+': -1, '-': -2, '*': -3, '/': -4, '(': -5, ')': -6, }
var optFuncs = map[int]func(a, b int) int{ opts['+']: func(a, b int) int { return a + b }, opts['-']: func(a, b int) int { return a - b }, opts['*']: func(a, b int) int { return a * b }, opts['/']: func(a, b int) int { return a / b }, }
var priority = map[int]int{ opts['+']: 1, opts['-']: 1, opts['*']: 2, opts['/']: 2, }
func calculate(s string) int { var postFixs []int stack := Stack{} num := 0 s += " "
for i, b := range s { switch { case isNumber(b): num = num*10 + int(b-'0') if !isNumber(rune(s[i+1])) { postFixs = append(postFixs, num) num = 0 } case isOpt(b): for !stack.Empty() && stack.Peek() != opts['('] && priority[stack.Peek()] >= priority[opts[b]] { postFixs = append(postFixs, stack.Pop()) } stack.Push(opts[b]) case isBracket(b): if b == '(' { stack.Push(opts[b]) } if b == ')' { for !stack.Empty() && stack.Peek() != opts['('] { postFixs = append(postFixs, stack.Pop()) } if !stack.Empty() { stack.Pop() } } } } for !stack.Empty() { postFixs = append(postFixs, stack.Pop()) } tmpStack := Stack{} for _, v := range postFixs { if v >= 0 { tmpStack.Push(v) } else { v2 := tmpStack.Pop() v1 := tmpStack.Pop() tmpStack.Push((optFuncs[v])(v1, v2)) } } return tmpStack.Pop() }
func isNumber(b rune) bool { return b >= '0' && b <= '9' }
func isOpt(b rune) bool { return b == '+' || b == '-' || b == '*' || b == '/' }
func isBracket(b rune) bool { return b == '(' || b == ')' }
type Stack []int
func (s *Stack) Push(d int) { *s = append(*s, d) }
func (s *Stack) Pop() int { l := len(*s) d := (*s)[l-1] *s = (*s)[:l-1] return d }
func (s Stack) Peek() int { return s[len(s)-1] }
func (s Stack) Empty() bool { return len(s) == 0 }
func main() { calculate("(1+(4+5/2)-3)+(6+8)") }
|