-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions_basics.go
140 lines (117 loc) · 2.76 KB
/
functions_basics.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
package main
import (
"math"
"strconv"
"strings"
)
func slicerNumbers(expression string) []float64 {
match := strings.FieldsFunc(expression, split)
var result = []float64{}
for _, i := range match {
j, err := strconv.ParseFloat(i, 64)
if err != nil {
panic(err)
}
result = append(result, j)
}
return result
}
func slicerNumbersLetters(expression string) []string {
match := strings.FieldsFunc(expression, split)
var result = []string{}
for _, i := range match {
result = append(result, i)
}
return result
}
func split(r rune) bool {
return r == '+' || r == '-' || r == '*' || r == '/' || r == '^'
}
func slicerSymbols(expression string) []string {
result := []string{}
for _, element := range expression {
if element == '+' || element == '-' || element == '*' || element == '/' || element == '^' {
result = append(result, string(element))
}
}
return result
}
func containsList(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func containsString(expression string, elements []rune) bool {
for _, e := range elements {
for _, a := range expression {
if a == e {
return true
}
}
}
return false
}
func removeNumber(slice []float64, s int, t rune) []float64 {
if (s + 1) < len(slice) {
if t == '*' {
slice[s] *= slice[s+1]
} else if t == '^' {
slice[s] = math.Pow(slice[s], slice[s+1])
} else if t == '/' {
slice[s] /= slice[s+1]
} else if t == '+' {
slice[s] += slice[s+1]
} else if t == '-' {
slice[s] -= slice[s+1]
}
return append(slice[:s+1], slice[s+2:]...)
}
return []float64{slice[0]}
}
func removeSymbols(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}
func checkParenthesis(expression string) bool {
return containsString(expression, []rune{'(', ')'})
}
func handleExpressionParenthesis(expression string, position int) string {
smallExpression := ""
count := len(expression)
positionLastParenthesis := count
for i := position + 1; i < count; i++ {
if expression[i] != ')' {
smallExpression += string(expression[i])
} else {
positionLastParenthesis = i
break
}
}
result := basics(smallExpression)
end := ""
for i := 0; i < position; i++ {
end += string(expression[i])
}
end += strconv.FormatFloat(result, 'f', -1, 64)
for i := positionLastParenthesis + 1; i < count; i++ {
end += string(expression[i])
}
return end
}
func handleParenthesis(expression string) string {
open, openMax, lastOpenPosition, count := 0, 0, 0, len(expression)
for i := 0; i < count; i++ {
if expression[i] == '(' {
open++
if open > openMax {
openMax = open
lastOpenPosition = i
}
} else if expression[i] == ')' {
open--
}
}
return handleExpressionParenthesis(expression, lastOpenPosition)
}