代码结构:
Stack文件(实现栈)
package Stack
type Node struct {
data interface{}
next *Node
}
type ListNode struct {
Len int
Nodes *Node
}
type Stack struct {
Node *ListNode
}
func (s *Stack) Push(v interface{}) {
if s.Node == nil {
s.Node = &ListNode{Len: 0, Nodes: nil}
}
p := s.Node.Nodes
for {
if p == nil {
s.Node.Nodes = &Node{next: nil, data: v}
s.Node.Len++
break
}
if p.next == nil {
p.next = &Node{next: nil, data: v}
s.Node.Len++
break
}
p = p.next
}
}
func (s *Stack) Pop() (v interface{}) {
if s.Node.Len == 0 {
return
} else {
if s.Node.Len == 1 {
temp := s.Node.Nodes
s.Node.Nodes = nil
s.Node.Len--
return temp.data
} else {
p := s.Node.Nodes
cnt := 0
for {
cnt++
if cnt == s.Node.Len-1 {
temp := p.next
p.next = nil
s.Node.Len--
return temp.data
}
p = p.next
}
}
}
}
这个栈由一个链表实现,一并还实现了记录链表的长度
Convert文件(实现中缀表达式转换为前缀表达式)
package Convert
import (
"fmt"
"math"
"strconv"
. "testmode/Stack"
)
func Judge(a string, b string) bool {
inA := a[0]
inB := b[0]
if inA == 43 || inA == 45 {
inA = 0
} else {
inA = 1
}
if inB == 43 || inB == 45 {
inB = 0
} else {
inB = 1
}
if inA >= inB {
return true
} else {
return false
}
}
func ConvertPrefix(formula string) Stack {
s1 := Stack{}
s2 := Stack{}
record := Stack{}
i := len(formula) - 1
for ; i >= 0; i-- {
intV, err := strconv.Atoi(string(formula[i]))
if err == nil {
record.Push(intV)
continue
} else {
if record.Node != nil && record.Node.Len != 0 {
b := record.Node.Len
sum := 0
for b > 0 {
intV, _ := strconv.Atoi(fmt.Sprint(record.Pop()))
sum += int(float64(intV) * math.Pow(10.0, float64(b-1)))
b--
}
s2.Push(sum)
}
// 遇到括号时的判断
if string(formula[i]) == "(" || string(formula[i]) == ")" {
if string(formula[i]) == ")" {
s1.Push(string(formula[i]))
continue
}
if string(formula[i]) == "(" {
for {
val := s1.Pop()
if fmt.Sprintf("%v", val) == ")" {
break
}
s2.Push(val)
}
continue
}
}
// 遇到运算符时
if string(formula[i]) == "+" || string(formula[i]) == "-" || string(formula[i]) == "*" || string(formula[i]) == "/" {
for {
if s1.Node == nil || s1.Node.Len == 0 {
s1.Push(string(formula[i]))
break
} else {
val_ := s1.Pop()
if fmt.Sprintf("%v", val_) == ")" {
s1.Push(")")
s1.Push(string(formula[i]))
break
} else {
s1.Push(val_)
val := fmt.Sprintf("%v", s1.Pop())
if Judge(string(formula[i]), val) {
s1.Push(val)
s1.Push(string(formula[i]))
break
} else {
s2.Push(val)
}
}
}
}
}
}
}
if record.Node != nil && record.Node.Len != 0 {
b := record.Node.Len
sum := 0
for b > 0 {
intV, _ := strconv.Atoi(fmt.Sprint(record.Pop()))
sum += int(float64(intV) * math.Pow(10.0, float64(b-1)))
b--
}
s2.Push(sum)
}
if s1.Node == nil || s1.Node.Len == 0 {
} else {
for {
if s1.Node.Len == 0 {
break
}
s2.Push(s1.Pop())
}
}
return s2
}
Judge函数实现了运算符优先级的判断,conver函数返回一个栈弹出后就是前缀表达式
Utils模块
package utils
import (
"fmt"
"strconv"
"testmode/Stack"
)
func Compute(s Stack.Stack) int {
recS := Stack.Stack{}
for {
recS.Push(s.Pop())
if s.Node.Len == 0 {
break
}
}
NumS := Stack.Stack{}
for {
val := recS.Pop()
intV, err := strconv.Atoi(fmt.Sprintf("%v", val))
if err == nil {
NumS.Push(intV)
} else {
n1, _ := strconv.Atoi(fmt.Sprintf("%v", NumS.Pop()))
n2, _ := strconv.Atoi(fmt.Sprintf("%v", NumS.Pop()))
if fmt.Sprintf("%v", val) == "+" {
NumS.Push(n1 + n2)
} else if fmt.Sprintf("%v", val) == "-" {
NumS.Push(n1 - n2)
} else if fmt.Sprintf("%v", val) == "*" {
NumS.Push(n1 * n2)
} else if fmt.Sprintf("%v", val) == "/" {
NumS.Push(n1 / n2)
}
}
if recS.Node.Len == 0 {
break
}
}
intR, _ := strconv.Atoi(fmt.Sprintf("%v", NumS.Pop()))
return intR
}
Compute函数接收一个前缀表达式的栈,最后返回一个整数就是运算的结果