-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc.py
84 lines (57 loc) · 2.43 KB
/
calc.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
class Calculator:
operatoren = ["+","-","*","/"]
def __init__(self):
pass
def isOperator(self,item):
return item in self.operatoren
def worker(self,formel):
formelList = []
for x in formel:
if x.isnumeric():
value = x
if len(formelList) > 0 and (formelList[len(formelList) -1].isnumeric() or '.' in formelList[len(formelList) -1]):
formelList[len(formelList) -1] += value
else:
formelList.append(value)
elif self.isOperator(x):
formelList.append(x)
elif x == '.':
formelList[len(formelList) -1] += x
self.formels = { "+":lambda a,b : a+b,"-":lambda a,b : a-b,"*": lambda a,b: a*b,"/": lambda a,b: a/b}
formelList = self.removeOpteratoren(formelList)
formelList = self.pointFirst(formelList)
while len(formelList) >= 3 and not self.isOperator(formelList[0]):
value = self.formels[formelList[1]](float(formelList[0]),float(formelList[2]))
formelList.pop(0)
formelList.pop(0)
formelList[0] = value
return formelList[0]
def removeOpteratoren(self,formelList):
if self.isOperator(formelList[0]) and formelList[0] == "-":
formelList[1] = float(formelList[0] + formelList[1])
formelList.pop(0)
return formelList
elif self.isOperator(formelList[0]):
value = self.formels[formelList[0]](0,float(formelList[1]))
formelList[1] = value
formelList.pop(0)
return formelList
else:
return formelList
def pointFirst(self,formelList):
index = 0
while "/" in formelList or "*" in formelList:
if formelList[index] == "/" or formelList[index] == "*":
formelList[index] = self.formels[formelList[index]](float(formelList[index-1]),float(formelList[index+1]))
formelList.pop(index+1)
formelList.pop(index-1)
index -= 1
else:
index += 1
return formelList
def testUnit(value,defined):
return value == defined
if __name__ == "__main__":
c = Calculator()
if testUnit(c.worker("57/5*10+20*5-2"),212) and testUnit(c.worker("-57/5*10+20*5-2"),-16):
print("Work")