-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
81 lines (60 loc) · 2.29 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
71
72
73
74
75
76
77
78
79
80
81
def main(): #added by contributor
b=input('enter the expression') #added by contributor
return b #added by contributor
a = main()
original_lst=list()
original_lst_1=list()
original_lst_2=list()
original_lst_3=list()
for i in a :
if a.index(i)!=len(a)-1:
if i==a[a.index(i)+1]:
original_lst.append(i+a[a.index(i)+1])
else:
original_lst.append(i)
else:
original_lst.append(i)
for i in original_lst:
if i=='/':
div_index=original_lst.index(i)
div_index_copy=div_index
numerator=float(original_lst[div_index-1])
denominator=float (original_lst[div_index+1])
if denominator == 0: #added by contributor
print("Cannot Divide by zero") #added by contributor
main() #added by contributor
div_result=numerator/denominator
del original_lst[div_index-1:div_index+2]
original_lst.insert(div_index_copy-1,div_result)
for i in original_lst:
original_lst_1.append(str(i))
for i in original_lst_1:
if i=='*':
mul_index=original_lst_1.index(i)
term_one=float(original_lst_1[mul_index-1])
term_two=float(original_lst_1[mul_index+1])
mul_result=term_one*term_two
del original_lst_1[mul_index-1:mul_index+2]
original_lst_1.insert(mul_index-1,mul_result)
for i in original_lst_1:
original_lst_2.append(str(i))
for i in original_lst_2:
if i=='+':
add_index=original_lst_2.index(i)
term_one=float(original_lst_2[add_index-1])
term_two=float(original_lst_2[add_index+1])
add_result=term_one+term_two
del original_lst_2[add_index-1:add_index+2]
original_lst_2.insert(add_index-1,add_result)
for i in original_lst_2:
original_lst_3.append(str(i))
for i in original_lst_3:
if i=='-':
sub_index=original_lst_3.index(i)
term_one=float(original_lst_3[sub_index-1])
term_two=float(original_lst_3[sub_index+1])
sub_result=term_one-term_two
del original_lst_3[sub_index-1:sub_index+2]
original_lst_3.insert(sub_index-1,sub_result)
print('THE RESULT OF THE FOLLOWING EXPRESSION IS',original_lst_3[0])
b=int(input(''))