-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
277 lines (257 loc) · 8.71 KB
/
eval.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from dataclasses import replace
import math
from pickle import FALSE
def unary_operation(operator, operand):
operand = float(operand)
try:
if operator == 'sin':
return math.sin(operand)
if operator == 'cos':
return math.cos(operand)
if operator == 'tan':
return math.tan(operand)
if operator == 'abs':
return math.abs(operand)
if operator == 'ln':
return math.log(operand)
if operator == 'exp':
return math.exp(operand)
if operator == 'sqrt':
return math.sqrt(operand)
except Exception as ex:
return False
def calAdvExp(expressionstr: str):
unary_operator = ['sin', 'abs', 'tan', 'cos', 'ln', 'exp', 'sqrt']
expressionstr = expressionstr.split(' ')
index = 0
while len(expressionstr) != 1:
index = 0
isSimple = True
while index < len(expressionstr):
if index == 0:
isSimple = True
if expressionstr[index] == ')':
isSimple = False
first_p_index = index
valid = False
while first_p_index != -1:
if expressionstr[first_p_index] == '(':
valid = True
break
first_p_index -= 1
if not valid:
return False
inner_value = calSimpleExp(
" ".join(expressionstr[first_p_index+1:index]))
if not inner_value:
return False
prev = expressionstr[0:first_p_index]
if index+1 < len(expressionstr):
next = expressionstr[index+1:]
else:
next = list()
expressionstr = list()
if len(prev) > 0:
expressionstr.extend(prev)
expressionstr.append(str(inner_value))
if len(next) > 0:
expressionstr.extend(next)
if len(prev) > 0:
if unary_operator.__contains__(prev[-1]):
new_index = len(prev)
res = unary_operation(
expressionstr[new_index - 1], expressionstr[new_index])
if str(res) == 'False':
return False
expressionstr[new_index-1:new_index+1] = [str(res)]
index = 0
else:
index += 1
if isSimple:
if openPcontain(expressionstr):
return False
return calSimpleExp(" ".join(expressionstr))
return expressionstr[0]
def openPcontain(expression):
for char in expression:
if char == '(':
return True
return False
def calSimpleExp(expressionstr: str):
global expression
expression = expressionstr.split(' ')
if not power():
return False
if not division():
return False
if not multiplication():
return False
if not minus():
return False
if not add():
return False
if len(expression) == 1:
return expression[0]
else:
return False
def add():
global expression
index = 0
while index != len(expression):
if expression[index] == '+':
if index - 1 >= 0 and index + 1 < len(expression):
prev = expression[0:index-1]
if index + 2 < len(expression):
next = expression[index+2:]
else:
next = list()
new_val = 0
try:
new_val = float(expression[index-1]) + \
float(expression[index+1])
except Exception as ex:
return False
expression = list()
if len(prev) > 0:
expression.extend(prev)
expression.append(str(new_val))
if len(next) > 0:
expression.extend(next)
index = 0
else:
return False
else:
index += 1
return expression
def minus():
global expression
index = 0
while index != len(expression):
if expression[index] == '-':
if index - 1 >= 0 and index + 1 < len(expression):
prev = expression[0:index-1]
if index + 2 < len(expression):
next = expression[index+2:]
else:
next = list()
new_val = 0
try:
new_val = float(expression[index-1]) - \
float(expression[index+1])
except Exception as ex:
return False
expression = list()
if len(prev) > 0:
expression.extend(prev)
expression.append(str(new_val))
if len(next) > 0:
expression.extend(next)
index = 0
else:
return False
else:
index += 1
return expression
def multiplication():
global expression
index = 0
while index != len(expression):
if expression[index] == '*':
if index - 1 >= 0 and index + 1 < len(expression):
prev = expression[0:index-1]
if index + 2 < len(expression):
next = expression[index+2:]
else:
next = list()
new_val = 0
try:
new_val = float(expression[index-1]) * \
float(expression[index+1])
except Exception as ex:
return False
expression = list()
if len(prev) > 0:
expression.extend(prev)
expression.append(str(new_val))
if len(next) > 0:
expression.extend(next)
index = 0
else:
return False
else:
index += 1
return expression
def division():
global expression
index = 0
while index != len(expression):
if expression[index] == '/':
if index - 1 >= 0 and index + 1 < len(expression):
prev = expression[0:index-1]
if index + 2 < len(expression):
next = expression[index+2:]
else:
next = list()
new_val = 0
try:
new_val = float(expression[index-1]) / \
float(expression[index+1])
except Exception as ex:
return False
expression = list()
if len(prev) > 0:
expression.extend(prev)
expression.append(str(new_val))
if len(next) > 0:
expression.extend(next)
index = 0
else:
return False
else:
index += 1
return expression
def power():
global expression
index = 0
while index != len(expression):
if expression[index] == '^':
if index - 1 >= 0 and index + 1 < len(expression):
prev = expression[0:index-1]
if index + 2 < len(expression):
next = expression[index+2:]
else:
next = list()
new_val = 0
try:
new_val = float(expression[index-1]
) ** float(expression[index+1])
except Exception as ex:
return False
expression = list()
if len(prev) > 0:
expression.extend(prev)
expression.append(str(new_val))
if len(next) > 0:
expression.extend(next)
index = 0
else:
return False
else:
index += 1
return expression
if __name__ == '__main__':
expression = input()
expression = expression.replace('(', '( ')
expression = expression.replace(')', ' )')
expression = expression.replace('sin', 'sin ')
expression = expression.replace('cos', 'cos ')
expression = expression.replace('tan', 'tan ')
expression = expression.replace('ln', 'ln ')
expression = expression.replace('sqrt', 'sqrt ')
expression = expression.replace('exp', 'exp ')
expression = expression.replace('abs', 'abs ')
answer = calAdvExp(expression)
if str(answer) == 'False':
print('INVALID')
else:
print("%.2f" % float(answer))