-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfixSingleStringFunctions.go
144 lines (123 loc) · 4.21 KB
/
postfixSingleStringFunctions.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
import (
"github.com/golang-collections/collections/stack"
)
var variableStack = stack.New()
var calulationStack = stack.New()
var mapOfVariables = make(map[string]int)
var finalResult = 0
func operatorToFunction(s string, x, y int) int {
if s == "+" {
return x + y
} else if s == "-" {
return x - y
} else if s == "*" {
return x * y
} else if s == "/" {
if y == 0 {
panic("You tried to divide by zero.")
}
return x / y
}
return 0
}
func populateMapWithStringGroup(s string, l int) {
// this will take an individual string,
// and populate a map with that string.
wordRegex := regexp.MustCompile(`[a-zA-Z]+|[0-9]+`)
digitRegex := regexp.MustCompile(`[0-9]+`)
if wordRegex.MatchString(s) {
_, keyExists := mapOfVariables[s]
if !keyExists {
if digitRegex.MatchString(s) {
i, err := strconv.Atoi(s)
if err == nil {
mapOfVariables[s] = i
} else {
println(err)
panic("There was an issue assigning variable: " + s + " to an integer \n")
}
} else {
mapOfVariables[s] = 0
}
}
}
}
// ab 22 * c + ab + $
// [ab 22 * c + ab + $]
// ab 0 * c + ab + $
// tom jerry 123 + tom * - $
// tom: 2
// jerry: 3
func populateMapWithOneLongString(s string) {
alphaRegex := regexp.MustCompile(`[a-zA-Z]+`)
// this takes one big string and poplates a map with that sring
strSliced := strings.Fields(s)
for i, v := range strSliced {
populateMapWithStringGroup(v, len(v))
if v == "$" && i == len(strSliced)-1 { // once we see the $ sign
for k := range mapOfVariables {
// if the value of the variable has already been assigned:
if alphaRegex.MatchString(k) {
var j int
// error checking
print("Enter the value of ", k, ": ")
_, err := fmt.Scanf("%d", &j)
if err != nil {
print(err)
panic("That is not a valid number...exiting")
}
mapOfVariables[k] = j
}
}
} else if v != "$" && i == len(strSliced)-1 { // if we don't see the $ sign as the last element
panic("Please put a $ sign at the end of your expression...exiting")
}
}
for _, v := range strSliced {
calculateWithALongerString(v)
}
}
func calculateWithALongerString(s string) {
wordRegex := regexp.MustCompile(`[a-zA-Z]+|[0-9]+`)
operatorRegex := regexp.MustCompile(`[+-\\*\\/]`)
if wordRegex.MatchString(s) {
variableStack.Push(s)
} else if operatorRegex.MatchString(s) {
if variableStack.Len() >= 1 {
if calulationStack.Len() == 0 {
xStr := variableStack.Pop().(string) // assert that the variable is a string
yStr := variableStack.Pop().(string) // assert that the variable is a string
x, _ := mapOfVariables[xStr] // assign the value fot that variable for calculations
y, _ := mapOfVariables[yStr] // assign the value fot that variable for calculations
calulationStack.Push(operatorToFunction(s, x, y))
} else if calulationStack.Len() == 1 {
x, _ := calulationStack.Pop().(int) // assert that the variable is an int
yStr := variableStack.Pop().(string) // assert that the variable is a string
y, _ := mapOfVariables[yStr] // assign the value fot that variable for calculations
calulationStack.Push(operatorToFunction(s, x, y))
} else if variableStack.Len() == 2 {
xStr := variableStack.Pop().(string) // assert that the variable is a string
yStr := variableStack.Pop().(string) // assert that the variable is a string
x, _ := mapOfVariables[xStr] // assign the value fot that variable for calculations
y, _ := mapOfVariables[yStr] // assign the value fot that variable for calculations
calulationStack.Push(operatorToFunction(s, x, y))
} else {
x, _ := calulationStack.Pop().(int) // assert that the variable is an int
yStr := variableStack.Pop().(string) // assert that the variable is a string
y, _ := mapOfVariables[yStr] // assign the value fot that variable for calculations
calulationStack.Push(operatorToFunction(s, x, y))
}
} else if calulationStack.Len() == 2 {
x, _ := calulationStack.Pop().(int) // assert that the variable is an int
y, _ := calulationStack.Pop().(int) // assert that the variable is an int
calulationStack.Push(operatorToFunction(s, x, y))
}
}
}