-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (52 loc) · 1.35 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func main() {
println()
println("*******************************************")
println("* The postfix calculator *")
for {
println("*******************************************")
println()
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a postfix expression with $ at the end: ")
postfixExpression, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
postfixExpressionWithoutNewLine := strings.Replace(postfixExpression, "\n", "", -1)
// inputs:
// ab 22 * c + ab + $
// tom jerry 123 + tom * - $
populateMapWithOneLongString(postfixExpressionWithoutNewLine)
if calulationStack.Len() == 1 && variableStack.Len() == 0 {
finalResult = calulationStack.Pop().(int)
println("Final Result:", finalResult)
} else {
println("You expression is malformed")
}
print("Continue(y/n)? ")
contBuffer := bufio.NewReader(os.Stdin)
cont, _ := contBuffer.ReadString('\n')
if cont == "n\n" {
break
}
// clear out the stacks, in case user continues
for i := 0; i < variableStack.Len()-1; i++ {
variableStack.Pop()
}
for i := 0; i < calulationStack.Len()-1; i++ {
calulationStack.Pop()
}
mapOfVariables = make(map[string]int)
println()
}
println()
println("**************** Done *********************")
println()
}