-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1106.go
69 lines (66 loc) · 1.56 KB
/
1106.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
package main
import "fmt"
func parseBoolExpr(expression string) bool {
var stack []string
for _, str := range expression {
if str == ',' {
continue
} else if str == ')' {
// 弹出栈顶元素直到找到一个左括号
var cur []string
for len(stack) != 0 {
if stack[len(stack)-1] != "(" {
cur = append(cur, stack[len(stack)-1])
stack = stack[:len(stack)-1]
} else {
stack = stack[:len(stack)-1]
break
}
}
// 如果栈不为空,往前取一位看是不是操作符
if len(stack) != 0 {
operator := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if operator == "!" {
// 取反,此种情况 cur 只会包含一个元素
if cur[0] == "t" {
stack = append(stack, "f")
} else if cur[0] == "f" {
stack = append(stack, "t")
}
} else if operator == "&" {
// 与,有一个为 f 即为 f
next := "t"
for _, str := range cur {
if str == "f" {
next = "f"
break
}
}
stack = append(stack, next)
} else if operator == "|" {
// 或,有一个为 t 即为 t
next := "f"
for _, str := range cur {
if str == "t" {
next = "t"
break
}
}
stack = append(stack, next)
}
} else {
// 栈为空,取与,应该不存在这种情况
}
} else {
stack = append(stack, string(str))
}
}
return stack[0] == "t"
}
func main() {
fmt.Print(parseBoolExpr("!(f)"))
fmt.Print(parseBoolExpr("|(f,t)"))
fmt.Print(parseBoolExpr("&(t,f)"))
fmt.Print(parseBoolExpr("|(&(t,f,t),!(t))"))
}