功能支持
简单演示
$ ./caculator
Please input express:
1+2*1-12+2-(1+2*3)
[1 + 2 * 1 - 12 + 2 - ( 1 + 2 * 3 )]
[1 2 1 * + 12 - 2 + 1 2 3 * + -]
-14
实现流程
后缀到前缀的转换
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() {
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
}