-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_cli.py
220 lines (210 loc) · 7.73 KB
/
calc_cli.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
inp=""
def calculator(inp):
x=""
test=[]
testo=[]
error=0
lstap=""
sym=['+', '-', '/', '*','^']
num=['1','2','3','4','5','6','7','8','9','0']
decimal=['.']
#input formating in list START
for letter in inp :
if letter == "(" or letter == ")":
if lstap==")": testo.append('*')
if x!="" and x!=".": testo.append(float(x))
if letter=="(" and x!="": testo.append('*')
testo.append(letter)
x=""
elif letter in sym:
if x!="" and lstap==")": testo.append('*')
if x!="" and x!=".": testo.append(float(x))
testo.append(letter)
lstap=letter
x=""
#non-numeric character immunity START
elif letter in num or letter in decimal:
if letter ==".":
if "." not in x:
x=x+letter
else:
x=x+letter
else:blank="blank"
#non-numeric character imunity END
lstlap=letter
if x!="" and lstap==")": testo.append('*')
if x!="" and x!=".":testo.append(float(x))
v=0
if len(testo)>0:
for v in range(0,2):
if set([testo[0]]).issubset(sym):
testo.insert(0,0)
if set([testo[len(testo)-1]]).issubset(sym):
testo.insert(len(testo)+1,0)
#input formating END
#expression validation START
current=""
lst=""
for letter in testo:
current=letter
if set([current, lst]).issubset(sym):
error=1
return("Invalid expression ! i think you added two operators consicutively "+lst+" and "+current +" in your expression." )
break
lst=current
# . after Rbracket validation START
ll=""
ic=0
for letter in testo:
if ll==")" and letter not in sym and letter !=")":
testo.insert(ic,"*")
ll=letter
ic=ic+1
# . after Rbracket validation END
#expression validation End
#solve function START
print(testo)
def slv(test):
#setting of value after single calculation in list with function START
def setvalue(pos):
if pos>0 and pos<=len(test) : del test[pos+1]
if pos>0 and pos<=len(test) : del test[pos]
#setting of value after single calculation in list with function END
#calculation of ^ START
def power():
x=1
for element in test:
if(element=="^"):
pos=test.index(element)
for v in range(0,int(test[pos+1])):
x=x*test[pos-1]
setvalue(pos)
if pos>0 and pos<=len(test) : test[pos-1]=x
#calculation of ^ END
#calculation of / START
def divide():
x=0
for element in test:
if(element=="/"):
pos=test.index(element)
if test[pos+1]>0:
x=test[pos-1]/test[pos+1]
else:
error=1
return("Can Not Divide by Zero !\nCheck your expression.",True)
setvalue(pos)
if pos>0 and pos<=len(test) : test[pos-1]=x
#calculation of / END
#calculation of * START
def mul():
x=0
for element in test:
if(element=="*"):
pos=test.index(element)
x=test[pos-1]*test[pos+1]
setvalue(pos)
if pos>0 and pos<=len(test) : test[pos-1]=x
#calculation of * END
#calculation of + START
def add():
x=0
for element in test:
if(element=="+"):
pos=test.index(element)
x=test[pos-1]+test[pos+1]
setvalue(pos)
if pos>0 and pos<=len(test) : test[pos-1]=x
#calculation of + END
#calculation of - START
def sub():
x=0
for element in test:
if(element=="-"):
pos=test.index(element)
x=test[pos-1]-test[pos+1]
setvalue(pos)
if pos>0 and pos<=len(test) : test[pos-1]=x
#calculation of - END
er=False
rr=""
for element in test:
for elememt in test:
if(element=="^"):
power()
for element in test:
for elememt in test:
if(element=="/"):
rr,er=divide()
for element in test:
for elememt in test:
if(element=="*"):
mul()
for element in test:
for elememt in test:
if(element=="+"):
add()
for element in test:
for elememt in test:
if(element=="-"):
sub()
if len(test)>0 and er==False :return(test[0])
else:return(rr)
#solve function END
#intitial left and right bracket counter START
ilbcount=0
irbcount=0
for element in testo:
if element=="(":
ilbcount=ilbcount+1
if element==")":
irbcount=irbcount+1
#intitial left and right bracket counter END
#bracket check START
if ilbcount==irbcount:
#loop repeat START
for j in range(0,ilbcount):
#bracket position collector START
z=0
for element in testo:
if element=="(":
lbpos=z
if element==")":
rbpos=z
break
z=z+1
#bracket position collector END
#new list temp list creater START
temp=[]
for i in range(lbpos+1,rbpos):
temp.append(testo[i])
#new list temp list creater END
#solve temp list START
b=slv(temp)
#solve temp list END
#remove from temp list from testo list START
for k in range(lbpos+1,rbpos+1) :
del testo[lbpos+1]
#remove from temp list from testo list END
#replace temp result in tempo list on place of left bracket which is solved START
testo[lbpos]=b
#replace temp result in tempo list on place of left bracket which is solved END
#loop repeat END
else:
return("Invalid expression! Please check you brackets.")
error=1
#bracket check STOP
prevc=""
for xx in testo:
prevc=prevc+str(xx)
if error==0:
rslt=slv(testo)
if error==0:
return(str(rslt),prevc)
else:
return("No calculations to be made !")
if __name__ == "__main__":
print("Calculator\n########### \n")
inp=input("Calculation is done in form of expression numbers(Example : 2(4+6)^2/10*2-15*2+(5)(2)(5) in this expression ^2 means power 2 and its Answer would be -40.0 )\n Enter your expression : ")
rs=calculator(inp)
if rs!="None": print(rs)
else:print("Invalid Expression !")