-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem050.go
More file actions
44 lines (38 loc) · 800 Bytes
/
Copy pathproblem050.go
File metadata and controls
44 lines (38 loc) · 800 Bytes
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
package problem050
import "errors"
type TreeNode interface {
evaluate() (int, error)
}
type OperatorNode struct {
value rune
left TreeNode
right TreeNode
}
func (operatorNode OperatorNode) evaluate() (int, error) {
leftValue, err := operatorNode.left.evaluate()
if err != nil {
return 0, err
}
rightValue, err := operatorNode.right.evaluate()
if err != nil {
return 0, err
}
switch operatorNode.value {
case '+':
return leftValue + rightValue, nil
case '-':
return leftValue - rightValue, nil
case '*':
return leftValue * rightValue, nil
case '/':
return leftValue / rightValue, nil
default:
return 0, errors.New("invalid operator")
}
}
type NumberNode struct {
value int
}
func (numberNode NumberNode) evaluate() (int, error) {
return numberNode.value, nil
}