-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpn.go
47 lines (39 loc) · 1.03 KB
/
rpn.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
package rpn
import (
"strconv"
"strings"
)
func main() {
}
func pop2(stack []float64) (left float64, right float64, newStack []float64) {
left = stack[len(stack)-2]
right = stack[len(stack)-1]
newStack = stack[:len(stack)-1]
return
}
type operation func(l float64, r float64) float64
func plus(l float64, r float64) float64 { return l + r }
func minus(l float64, r float64) float64 { return l - r }
func divide(l float64, r float64) float64 { return l / r }
func multiply(l float64, r float64) float64 { return l * r }
var operations = map[string]operation{
"+": plus,
"-": minus,
"/": divide,
"*": multiply,
}
func Rpn(input string) float64 {
tokens := strings.Split(input, " ")
var stack []float64
for _, token := range tokens {
current_operation, is_operation := operations[token]
if is_operation {
left, right, stack := pop2(stack)
stack = append(stack, current_operation(left, right))
} else {
value, _ := strconv.ParseFloat(token, 64)
stack = append(stack, value)
}
}
return stack[len(stack)-1]
}