-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (145 loc) · 2.95 KB
/
main.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
175
176
package main
import (
"fmt"
"strings"
)
var truthValues map[rune]trival
var exp logicalExpression
func start(n logicalExpression) (exp logicalExpression) {
if n != "" {
exp = n
askValues(exp)
return
}
fmt.Println("Enter a logical expression: ") // "(a&b)>c" // "(a|b)>c"
fmt.Scanln(&exp)
askValues(exp)
return
}
func main() {
exp = start("(a&b)|c")
a := parse(exp)
printInorder(a, 0)
a.setTruthValues()
fmt.Printf("Given that values, the expression %v, is %v\n", exp, a.eval().String())
}
func parse(input logicalExpression) *Node {
var depth int
var a *Node
var opSeenOutside int
if input.isOnlyVar() {
a = &Node{kind: LEAF, binOp: EMPTY, left: nil, right: nil, leafName: string(input)}
return a
}
for i, c := range input {
isOp, op := isOperation(c)
if isOp && depth == 0 {
opSeenOutside++
}
switch c {
case ' ':
continue
case '(':
depth++
case ')':
depth--
if i == len(input)-1 && depth == 0 && opSeenOutside == 0 {
s1 := input[1 : len(input)-1]
return parse(s1)
}
default:
if isOp && depth == 0 {
lhs := input[:i]
rhs := input[i+1:]
a = &Node{binOp: op, kind: OPERATION, left: parse(lhs), right: parse(rhs)}
} else {
continue
}
}
}
return a
}
func isOperation(c rune) (bool, operationType) {
a := map[rune]operationType{
'&': AND,
'|': OR,
'=': IFF,
'!': NOT,
'>': IMPLIES,
}
return c == '&' || c == '|' || c == '=' || c == '>' || c == '!', a[c]
}
func (exp logicalExpression) isOnlyVar() bool {
r := []rune(exp)
if len(exp) == 1 && r[0] >= 'a' && r[0] <= 'z' {
return true
}
return false
}
func (n *Node) eval() trival {
switch n.kind {
case LEAF:
return n.truthValue
case OPERATION:
return n.doOp()
default:
return Undefined
}
}
func (t trival) String() string {
switch t {
case True:
return "True"
case False:
return "False"
case Undefined:
return "Undefined"
}
return "Undefined"
}
func (n *Node) doOp() (ans trival) {
lhs, op, rhs := n.left.truthValue, n.binOp, n.right.truthValue
switch op {
case OR:
ans = OrValues[lhs][rhs]
case AND:
ans = AndValues[lhs][rhs]
case IFF:
ans = IffValues[lhs][rhs]
case IMPLIES:
ans = ImpValues[lhs][rhs]
}
return
}
func askValues(input logicalExpression) {
truthValues = make(map[rune]trival)
for _, elem := range input {
if elem >= 'a' && elem <= 'z' {
truthValues[elem] = Undefined
}
}
fmt.Println("Los valores validos son (T/F/U), insertar el valor de verdad de: ")
for key := range truthValues {
var in string
var val trival
fmt.Printf("variable %v\n", string(key))
fmt.Scanln(&in)
in = strings.ToLower(in)
if in == "true" || in == "t" || in == "v" {
val = True
} else if in == "false" || in == "f" {
val = False
} else {
val = Undefined
}
truthValues[key] = val
}
}
func (n *Node) setTruthValues() {
if n.kind == LEAF {
n.truthValue = truthValues[[]rune(n.leafName)[0]]
} else {
n.left.setTruthValues()
n.right.setTruthValues()
}
}