-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.go
204 lines (191 loc) · 4.68 KB
/
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package junglemath
import (
"fmt"
goscan "github.com/junglehornet/goscan"
"log"
"math"
"regexp"
"strconv"
"strings"
"text/scanner"
)
func OpenCalculator() {
/*
Opens a calculator where you can solve equations with order of operations like 8 * (2/3 + 4).
Enter "q" to exit.
To Add: +
Ex. 2+4 = 6
To Subtract: -
Ex. 2-4 = -2
To Multiply: *
Ex. 2*4 = 8
To Divide: /
Ex. 2/4 = 0.5
To # Root: #r<number>
Ex. 2r4 = 2
This calculator environment is very basic and more of a template/example, so you are encouraged to make your own with some more polish, features, etc.
*/
s := goscan.NewScanner()
inpt := s.ReadLine()
first := true
var ans string
normalChars := "0123456789"
for inpt != "q" {
if inpt != "" {
if !strings.Contains(normalChars, string([]rune(inpt)[0])) {
if !first {
inpt = ans + inpt
}
}
}
ans = strconv.FormatFloat(Solve(inpt), 'f', -1, 64)
fmt.Println("= " + ans)
first = false
inpt = s.ReadLine()
}
}
func Solve(equation string) float64 {
/*
Solves an equation with order of operations like 8 * (2/3 + 4).
To Add: +
Ex. 2+4 = 6
To Subtract: -
Ex. 2-4 = -2
To Multiply: *
Ex. 2*4 = 8
To Divide: /
Ex. 2/4 = 0.5
To # Root: #r<number>
Ex. 2r4 = 2
*/
equation = strings.ReplaceAll(equation, " ", "")
equation = PrepEquation(equation)
_, isParentheses := GetParentheses(equation)
var solved bool
if isParentheses {
solved = false
} else {
solved = true
}
for !solved {
inParentheses, _ := GetParentheses(equation)
ans := Solve(inParentheses)
equation = strings.Replace(equation, "("+inParentheses+")", strconv.FormatFloat(ans, 'f', -1, 64), -1)
if !strings.Contains(equation, "(") {
solved = true
}
}
equation = SolveOperator(equation, 1)
equation = SolveOperator(equation, 2)
equation = SolveOperator(equation, 3)
equation = SolveOperator(equation, 4)
equation = SolveOperator(equation, 5)
solved = false
regex4 := regexp.MustCompile(`(-?\d*\.?\d*)min(-?\d*\.?\d*)`)
for {
if !strings.Contains(equation, "min") {
break
}
minus := regex4.FindStringSubmatch(equation)
num1, _ := strconv.ParseFloat(minus[1], 64)
num2, _ := strconv.ParseFloat(minus[2], 64)
result := strconv.FormatFloat(num1-num2, 'f', -1, 64)
equation = strings.Replace(equation, minus[0], result, -1)
}
ans, err := strconv.ParseFloat(equation, 64)
if err != nil {
return 0
}
return ans
}
func PrepEquation(equation string) string {
equation = "(" + equation + ")"
equation = SolveOperator(equation, 0)
re := regexp.MustCompile(`\((-?\d*\.?\d*)`)
res := re.FindString(equation)
newRes := strings.Replace(res, "-", "neg", -1)
equation = strings.Replace(equation, res, newRes, -1)
equation = strings.Replace(equation, "-", "min", -1)
equation = strings.Replace(equation, "neg", "-", -1)
equation = strings.TrimPrefix(equation, "(")
equation = strings.TrimSuffix(equation, ")")
return equation
}
func GetParentheses(inpt string) (string, bool) {
if !strings.Contains(inpt, "(") {
return "", false
}
var s scanner.Scanner
s.Init(strings.NewReader(inpt))
var inParantheses string
var paranMode int
paranMode = 0
for token := s.Scan(); token != scanner.EOF; token = s.Scan() {
text := s.TokenText()
if text == ")" {
paranMode -= 1
if paranMode == 0 {
break
}
}
if paranMode > 0 {
inParantheses += text
}
if text == "(" {
paranMode += 1
}
}
return inParantheses, true
}
func SolveOperator(equation string, operator int) string {
var opSymbol string
var opRegex string
switch operator {
case 0:
opRegex = `?r`
opSymbol = "r"
case 1:
opRegex = `\^`
opSymbol = "^"
case 2:
opRegex = `\*`
opSymbol = "*"
case 3:
opRegex = `/`
opSymbol = "/"
case 4:
opRegex = `\+`
opSymbol = "+"
case 5:
opRegex = `min`
opSymbol = "min"
default:
log.Fatal("Error: Invalid operator " + strconv.Itoa(operator) + " passed to SolveOperator")
}
Regex := regexp.MustCompile(`(-?\d*\.?\d*)` + opRegex + `(-?\d*\.?\d*)`)
for {
if !strings.Contains(equation, opSymbol) {
break
}
operation := Regex.FindStringSubmatch(equation)
num1, _ := strconv.ParseFloat(operation[1], 64)
num2, _ := strconv.ParseFloat(operation[2], 64)
var result string
switch operator {
case 0:
result = strconv.FormatFloat(Root(num1, num2), 'f', -1, 64)
case 1:
result = strconv.FormatFloat(math.Pow(num1, num2), 'f', -1, 64)
case 2:
result = strconv.FormatFloat(num1*num2, 'f', -1, 64)
case 3:
result = strconv.FormatFloat(num1/num2, 'f', -1, 64)
case 4:
result = strconv.FormatFloat(num1+num2, 'f', -1, 64)
case 5:
result = strconv.FormatFloat(num1-num2, 'f', -1, 64)
}
equation = strings.Replace(equation, operation[0], result, -1)
}
return equation
}