-
Notifications
You must be signed in to change notification settings - Fork 0
/
interprete.py
132 lines (120 loc) · 8.75 KB
/
interprete.py
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
from type import getType
def interpreteEquasion(equasion_as_string): # splits a equasion in a string into different parts
string = equasion_as_string # code will look cleaner with shorter variables
# equasion = equasion_as_list # code will look cleaner with shorter variables
lasttype = "" # the chartype (getType()) from the last checked char
currenttype = "" # the crurrent checked chartype (getType())
# >>> remove SPACE <<<
indexcorrection = 0
for i in range(0, len(string)): # for every char in the string except the first
i = i + indexcorrection
lasttype = currenttype # the currenttype from the last run of the for-loop will now become
currenttype = getType(str(string[i])) # getting a new currenttype from a char with the getType() function from the equasion string
if currenttype == "space":
string = string[:i] + string[i + 1:]
indexcorrection = indexcorrection - 1
# >>> 2x -> 2*x <<<
currenttype = getType(str(string[0])) # the first char haven't to compared with the one before, because there is no one before :D
indexcorrection = 0
for i in range(1, len(string)): # for every char in the string except the first
i = i + indexcorrection
lasttype = currenttype # the currenttype from the last run of the for-loop will now become
currenttype = getType(str(string[i])) # getting a new currenttype from a char with the getType() function from the equasion string
if (currenttype == "variable") and (lasttype == "number"):
string = str(string[:i]) + "*" + str(string[i:])
indexcorrection = indexcorrection + 1
# >>> negative number marking -1 -> €1 -x -> $x <<<
lasttype2 = "nothing"
lasttype = "nothing"
currenttype = "nothing"
currentchar = ""
lastchar = ""
lastchar2 = ""
maxIndex = len(string) - 1
i = 0
finished = False # these 3 lines are a ranged for loop
while not finished: # , which can change its lenght
if (i == maxIndex) or (i < maxIndex): # while in work
lasttype2 = lasttype
lasttype = currenttype
currenttype = getType(string[i])
lastchar2 = lastchar
lastchar = currentchar
currentchar = string[i]
variableInnterruption = False
numberInterruption = False
numberLenght = 1
variableLenght = 1
stringFragment1 = "("
stringFragment2 = ""
stringFragment3 = ")"
if (currenttype == "number" or currenttype == "variable") and (lastchar == "-"):
if (lastchar2 == "(") or (lasttype2 == "action") or (lasttype2 == "nothing"): # detecting if the current index is an negative number
for lenght in range(i + 1, len(string) - i + 1): # scanning all chars after the number
if not numberInterruption:
if getType(string[lenght]) == "number":
numberLenght = numberLenght + 1 # detecting the lenght of the variable
else:
numberInterruption = True
else:
continue
try: # now the equasion string will parted in thre fragments
stringFragment1 = string[:i - 1] + "("
except Exception:
continue
try:
stringFragment2 = "€" + string[i:i + numberLenght] # the negative minus char will be replaced by an € sign
except Exception:
continue
try:
stringFragment3 = ")" + string[i + numberLenght:]
except Exception:
continue
string = stringFragment1 + stringFragment2 + stringFragment3 # now the equasion string get assembled by thee fragments
i = i + 1 # part of the raged for(while) loop
maxIndex = len(string) - 1 # updating the lenght of the string for the ranged for(while) loop
else:
i = i + 1 # part of the raged for(while) loop
else:
finished = True
# >>> interprete to list <<<
equasionindex = 0 # the index of the last added entry to the equasion list
equasion = [] # declaring the equasion list
currenttype = getType(str(string[0])) # the first char haven't to compared with the one before, because there is no one before :D
equasion.append(str(string[0])) # ...and will be direcly a new entry in the equasionlist
for i in range(1, len(string)): # for every char in the string except the first
lasttype = currenttype # the currenttype from the last run of the for-loop will now become
currenttype = getType(str(string[i])) # getting a new currenttype from a char with the getType() function from the equasion string
if currenttype == "bracket":
equasion.append(str(string[i])) # ...create a new entry in the equasionlist with the current char
equasionindex = equasionindex + 1 # there is anew entry, so the programm should easily remember the highest index of the equasionlist
elif lasttype == currenttype: # when the current and last chartype is equal to each other...
equasion[equasionindex] = str(equasion[equasionindex]) + str(string[i]) # ...add the current char to the last entry in the equasionlist
elif lasttype == "variable" and currenttype == "number":
equasion[equasionindex] = str(equasion[equasionindex]) + str(string[i]) # ...add the current char to the last entry in the equasionlist
else: # when it's an other type of char is should...
equasion.append(str(string[i])) # ...create a new entry in the equasionlist with the current char
equasionindex = equasionindex + 1 # there is anew entry, so the programm should easily remember the highest index of the equasionlist
for i in range(0, len(equasion)): # replacing € and $ with -
if (equasion[i][0] == "$") or (equasion[i][0] == "€"):
try:
equasion[i] = "-" + equasion[i][1:]
except Exception:
continue
for i in range(0, len(equasion)): # replacing € and $ with -
if (getType(str(equasion[i][-1])) == "action") and (len(equasion[i]) > 1):
try:
equasion[i] = equasion[i][-1]
except Exception:
continue
# >>> 2() or x() -> 2*() or x*()
multiplyInstertionIndexes = []
for i in range(1, len(equasion)):
lasttype = getType(equasion[i - 1][-1])
currenttype = getType(equasion[i][0])
if currenttype == "bracket":
if lasttype == "number" or lasttype == "variable":
multiplyInstertionIndexes.append(i)
for i in multiplyInstertionIndexes:
equasion.insert(i, "*")
return equasion # the equasion interpreted to a list will get returned