-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
70 lines (62 loc) · 1.87 KB
/
calculator.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
eqn = input("Enter an arithmatic equation: ")
lopr = "/*+-"
def digits_r(eqn, i):
i+=1
count = 0
while i < len(eqn) and (eqn[i].isnumeric() or eqn[i]=="."):
count+=1
i+=1
return count
def digits_l(eqn, i):
i-=1
count = 0
while i >= 0 and (eqn[i].isnumeric() or eqn[i]=="."):
count+=1
i-=1
return count
def bodmas(eqn, l):
for x in lopr:
while x in eqn:
for i in range(len(eqn)):
if eqn[i] == x:
count_r = digits_r(eqn, i)
count_l = digits_l(eqn, i)
if count_l == 0:
return eqn
result = str(one_opp(eqn, i, x, count_l, count_r))
eqn = eqn.replace(eqn[i-count_l:i+count_r+1], result)
break
return eqn
def one_opp(eqn, i, opr, l, r):
if eqn[i] == '+':
result = float(eqn[i-l:i]) + float(eqn[i+1:i+r+1])
if eqn[i] == '-':
result = float(eqn[i-l:i]) - float(eqn[i+1:i+r+1])
if eqn[i] == '*':
result = float(eqn[i-l:i]) * float(eqn[i+1:i+r+1])
if eqn[i] == '/':
result = float(eqn[i-l:i]) / float(eqn[i+1:i+r+1])
if result%1 == 0:
result = int(result)
return result
def eval(eqn):
b_open = b_close = -1
for i in range(len(eqn)):
if eqn[i] == ')':
b_close = i
break
for j in range(i,0,-1):
if eqn[j] == '(':
b_open = j
break
if b_close == b_open == -1:
z = bodmas(eqn, len(eqn))
return z
result = str(bodmas(eqn[b_open+1:b_close], len(eqn)))
eqn = eqn.replace(eqn[b_open:b_close+1], result)
if result != eqn:
eqn = eval(eqn)
return eqn
eqn = eqn.replace(" ", "")
result = eval(eqn)
print("Result:", result)