-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
56 lines (47 loc) · 1.14 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
rule = ['/', '*', '+', '-']
inp = "5+5*5/5"
inp += "z"
char = ""
x = []
for d in inp:
if d in rule or d == "z":
if d != "z":
x.append(d)
x.append(char)
char = ""
continue
if d != "z":
char += d
inp = x.copy()
print(inp)
def calculator(y, z, sym):
y, z = float(y), float(z)
if sym == "+":
return str(z + y)
elif sym == "-":
return str(y - z)
elif sym == "*":
return str(y * z)
else:
return str(y / z)
while len(inp) != 1:
for i in range(len(rule)):
while True:
if "temp" in inp:
inp.remove("temp")
else:
break
print(inp)
for j in range(len(inp)):
try:
if inp[j] == rule[i]:
temp = calculator(inp[j - 1], inp[j + 1], inp[j])
print(inp)
inp[j - 1] = temp
print(inp)
inp[j] = "temp"
print(inp)
inp[j + 1] = "temp"
print(inp)
except IndexError:
pass