-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_postfix_calculator.go
64 lines (42 loc) · 1.02 KB
/
3_postfix_calculator.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
package main
import "C"
import (
"fmt"
"time"
)
// Implementing a PostFix calculator in Go
func PostFixCalculator(container Container) int {
var emptyStack Container
t1 := time.Now().Unix()
for _, item := range container.items {
switch v := item.(type) {
default:
fmt.Printf("unexpected type %T", v)
case int:
//push it into the stack
emptyStack.Push(item)
case string:
rightValue := emptyStack.Pop()
leftValue := emptyStack.Pop()
result := 0
if item.(string) == "+" {
result = leftValue.(int) + rightValue.(int)
}
if item.(string) == "*" {
result = leftValue.(int) * rightValue.(int)
}
if item.(string) == "-" {
result = leftValue.(int) - rightValue.(int)
}
if item.(string) == "%" {
result = leftValue.(int) % rightValue.(int)
}
fmt.Println(leftValue, rightValue, item, result)
emptyStack.Push(result)
}
}
t2 := time.Now().Unix()
elapsedTime := t2 - t1
fmt.Println("Finished in seconds", elapsedTime)
return emptyStack.Peek().(int)
}