-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursive_calc.py
236 lines (223 loc) · 7.46 KB
/
recursive_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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import math
def brac(exp): ##This function evaluates the expression inside parentheses
while "(" in exp:
c_brac = o_pos = exp.index(")")
while exp[o_pos] != "(":
o_pos -= 1
o_brac = o_pos
brac_exp = exp[o_brac+1 : c_brac]
while 'pi' in brac_exp:
p = brac_exp.find('pi')
p_left = brac_exp[:p]
i = p + 1
i_right = brac_exp[i+1:]
brac_exp = p_left + '3.14' + i_right
# if brac_exp[0] == '-':
# brac_exp = brac_exp[1:]
if '*-' in brac_exp or '/-' in brac_exp or '--' in exp or '+-' in exp:
val = neg(brac_exp)
exp = exp[:o_brac] + str(val) + exp[c_brac + 1:]
else:
exp = exp[:o_brac] + str(evaluate(brac_exp)) + exp[c_brac + 1:]
return evaluate(exp)
#This function identifies the number to the left of the operator
def lft(exp, pos):
i = pos-1
while exp[i].isnumeric() is True or exp[i] == '.':
i=i-1
if i<0:
break
return exp[i+1:pos]
#This function identifies the number to the right of the operator
def rgt(exp, pos):
i = pos+1
# if exp[i] == '-':
# i = i+1
# if len(exp)+1 < i:
# exp = exp + ''
while exp[i].isnumeric() is True or exp[i] == '.' or exp[i] == 'p' or exp[i] == 'i':
i=i+1
if i >= len(exp):
break
return exp[pos+1:i]
#If there is a * and / followed by a - sign then move the '-' sign until it encounters a '+' or '-' sign.
#If it encounters a '-' sign, it changes it to '+' sign and vice-versa.
#If there is a ^ followed by a - then it changes the expression to 1/a^b form
def stmin(exp, sgn):
if sgn == '^':
star_pos = exp.find(sgn+'-')
l = lft(exp, star_pos)
r = rgt(exp, star_pos)
exp = exp[:star_pos - len(l)] + str('1/') + l + str('^') + r + exp[star_pos + len(r) + 2:]
elif sgn == '-':
star_pos = exp.find(sgn+'-')
min_pos = star_pos + 1
left = exp[:star_pos]
right = exp[min_pos + 1:]
exp = left + '+' + right
elif sgn == '+':
star_pos = exp.find(sgn+'-')
min_pos = star_pos + 1
left = exp[:star_pos]
right = exp[min_pos + 1:]
exp = left + '-' + right
else:
star_pos = exp.find(sgn+'-')
min_pos = star_pos + 1
left = exp[:min_pos]
right = exp[min_pos+1:]
i = star_pos - 1
while exp[i].isnumeric() is True or exp[i] == '.' or exp[i] == '*' or exp[i] == '/':
i=i-1
if exp[i] == '+' or exp[i] == '-':
break
elif i<0: break
sign = i
if exp[sign] == '+':
exp = exp[:sign] + str('-') + exp[sign+1:min_pos] + exp[min_pos+1:]
elif exp[sign] == '-':
exp = exp[:sign] + str('+') + exp[sign+1:min_pos] + exp[min_pos+1:]
if exp[0] == '+':
exp = exp[1:]
else:
exp = exp[min_pos] + left + right
return brac(exp)
#This function is used to identify is two operators are together. If there are, then the it is further executed in the "stmin" function.
def neg(exp):
if '*-' in exp:
exp = stmin(exp, '*')
elif '/-' in exp:
exp = stmin(exp, '/')
elif '^-' in exp:
exp = stmin(exp, '^')
elif '+-' in exp:
exp = stmin(exp, '+')
elif '--' in exp:
exp = stmin(exp, '-')
elif 'n-' in exp:
exp = stmin(exp, 'n')
elif 's-' in exp:
exp = stmin(exp, 's')
return exp
#This function checks if there are two or more '-' sign in an expression
def check(exp, pos_op):
minus = exp[pos_op]
if '-' in exp[pos_op + 1:]:
r = rgt(exp, pos_op)
# print(r)
f = r.find(r)
l = exp[:pos_op+1]
l = len(l)
rpos = len(r)
if exp[pos_op + rpos + 1] == '-':
s = rgt(exp, pos_op + rpos + 1)
exp = exp[:pos_op] + minus + '(' + r + '+' + s + ')' + exp[pos_op + rpos + len(s)+2:]
return brac(exp)
def cos(exp, pos_op): #Solves cos function
soc = pos_op + 2
cos = exp[pos_op:soc + 1]
left = exp[:pos_op]
cos_exp = rgt(exp, soc)
if cos_exp == 'pi' or cos_exp == 'pi radians' or cos_exp == '3.14':
cos_exp = '3.14'
val = math.cos(float(cos_exp))
val = math.floor(val)
else:
val = math.cos(float(cos_exp))
return val
def sin(exp, pos_op): #Solves sin function
nos = pos_op + 2
sin = exp[pos_op:nos + 1]
left = exp[:pos_op]
sin_exp = rgt(exp, nos)
# if exp[nos+3] == '-':
if sin_exp == 'pi' or sin_exp == 'pi radians' or sin_exp == '3.14':
sin_exp = '3.14'
val = math.sin(float(sin_exp))
val = math.floor(val)
else:
val = math.sin(float(sin_exp))
return val
def evaluate(exp): #This is the function in which to expression actually executes
if '*-' in exp or '/-' in exp or '^-' in exp or '--' in exp or '+-' in exp or 'n-' in exp or 's-' in exp:
exp = neg(exp)
if type(exp) is float:
exp = str(exp)
if "+" in exp:
pos_op = exp.find("+")
left = exp.split("+")[0]
right = exp[pos_op+1:]
return evaluate(left) + evaluate(right)
elif "-" in exp:
pos_op = exp.find("-")
count = 0
for x in exp:
if x == '-':
count+=1
if count>=2:
return float(check(exp, pos_op))
else:
left = exp.split("-")[0]
if left == '':
left = '0'
right = exp[pos_op + 1:]
return evaluate(left) - evaluate(right)
elif "*" in exp:
pos_op = exp.find("*")
left = exp.split("*")[0]
right = exp[pos_op + 1:]
return evaluate(left) * evaluate(right)
elif "/" in exp:
pos_op = exp.find("/")
left = exp.split("/")[0]
right = exp[pos_op + 1:]
return evaluate(left) / evaluate(right)
elif "^" in exp:
pos_op = exp.find("^")
left = exp.split("^")[0]
right = exp[pos_op + 1:]
return evaluate(left) ** evaluate(right)
elif 'cot' in exp:
pos_op = exp.find('cot')
cos_val = cos(exp, pos_op)
sin_val = sin(exp, pos_op)
cot_val = cos_val/sin_val
return cot_val
elif 'sec' in exp:
pos_op = exp.find('sec')
cos_val = cos(exp, pos_op)
sec_val = 1/cos_val
return sec_val
elif 'cosec' in exp:
pos_op = exp.find('cosec')
c_pos = pos_op+4
left = exp[:pos_op]
right = exp[c_pos+1:]
exp = left + 'sin' + right
pos_op = exp.find('sin')
sin_val = sin(exp, pos_op)
cosec_val = 1/sin_val
return cosec_val
elif 'tan' in exp:
pos_op = exp.find('tan')
sin_val = sin(exp, pos_op)
cos_val = cos(exp, pos_op)
tan_val = sin_val/cos_val
return tan_val
elif 'cos' in exp:
pos_op = exp.find('cos')
cos_val = cos(exp, pos_op)
return cos_val
elif 'sin' in exp:
pos_op = exp.find('sin')
sin_val = sin(exp, pos_op)
return sin_val
else:
return float(exp)
inp = input("Enter an expression: ")
if "(" in inp:
print(brac(inp))
elif inp == '':
quit()
else:
print(evaluate(inp))