/
caculator.go
174 lines (159 loc) · 3.05 KB
/
caculator.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
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
package main
import (
"container/list"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
const (
LEVEL1 = iota
LEVEL2
LEVEL3
)
var po = make(map[string]int)
func parseExp(s string) (exps []string, err error) {
re, err := regexp.Compile("[0-9]+|[+*/\\-\\(\\)]{1}")
if err != nil {
fmt.Println("regexp compile error:", err)
return
}
for _, exp := range re.FindAll([]byte(s), -1) {
exps = append(exps, string(exp))
}
return
}
func isPop(list *list.List, s string) (op []string, ok bool) {
switch string(s) {
case "(":
ok = false
return
case ")":
ok = true
cur := list.Back()
for {
prev := cur.Prev()
if curValue, ok2 := cur.Value.(string); ok2 {
if string(curValue) == "(" {
list.Remove(cur)
return
}
op = append(op, curValue)
list.Remove(cur)
cur = prev
}
}
default:
for cur := list.Back(); cur != nil; {
prev := cur.Prev()
if curValue, ok2 := cur.Value.(string); ok2 {
if level, ok3 := po[curValue]; ok3 && level >= po[s] {
ok = true
op = append(op, curValue)
// fmt.Println(op)
list.Remove(cur)
} else if curValue == "(" {
// fmt.Println(curValue, op)
if len(op) != 0 {
ok = true
} else {
ok = false
}
return
}
}
cur = prev
}
}
return
}
func isOperate(s string) bool {
re, _ := regexp.Compile("[+*/\\-\\(\\)]{1}")
ok := re.MatchString(s)
// fmt.Println(ok, s)
return ok
}
func pre2stuf(exps []string) (exps2 []string) {
list1 := list.New()
list2 := list.New()
for _, exp := range exps {
if isOperate(exp) {
if op, ok := isPop(list1, exp); ok {
for _, s := range op {
list2.PushBack(s)
}
}
if exp == ")" {
continue
}
list1.PushBack(exp)
} else {
list2.PushBack(exp)
}
}
for cur := list1.Back(); cur != nil; cur = cur.Prev() {
// fmt.Print(cur.Value)
list2.PushBack(cur.Value)
}
for cur := list2.Front(); cur != nil; cur = cur.Next() {
if curValue, ok := cur.Value.(string); ok {
exps2 = append(exps2, curValue)
}
}
return
}
func caculate(exps []string) int {
list1 := list.New()
for _, s := range exps {
if isOperate(s) {
back := list1.Back()
prev := back.Prev()
backVal, _ := back.Value.(int)
prevVal, _ := prev.Value.(int)
var res int
switch s {
case "+":
res = prevVal + backVal
case "-":
res = prevVal - backVal
case "*":
res = prevVal * backVal
case "/":
res = prevVal / backVal
}
list1.Remove(back)
list1.Remove(prev)
list1.PushBack(res)
} else {
v, _ := strconv.Atoi(s)
list1.PushBack(v)
}
}
if list1.Len() != 1 {
fmt.Println("caculate error")
os.Exit(1)
}
res, _ := list1.Back().Value.(int)
return res
}
func main() {
po["+"] = LEVEL1
po["-"] = LEVEL1
po["*"] = LEVEL2
po["/"] = LEVEL2
// a := "1+23*1/123+(4 + 2 *5)"
fmt.Println("Please input express:")
var a string
fmt.Scanln(&a)
fmt.Println(a)
a = strings.Replace(a, " ", "", -1)
exps, err := parseExp(a)
if err != nil {
os.Exit(1)
}
fmt.Println(exps)
exps2 := pre2stuf(exps)
fmt.Println(exps2)
fmt.Println(caculate(exps2))
}