-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator
61 lines (57 loc) · 2.68 KB
/
Calculator
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
from collections import deque
import re
class MathExpressionException(ValueError):
pass
def calculator_math_express(expr: str) -> float:
work_stack = deque()
work_list = []
i = 0
temp_expr = expr
try:
while len(temp_expr) > 0:
work_element = re.match(r'(\d+(\.\d+)?)|\(|\)|\*|\+|-|/', temp_expr).group(0)
temp_expr = temp_expr[len(work_element):]
if re.match(r'\d+(\.\d+)?', work_element) != None:
work_list.append(float(work_element))
if len(work_stack) != 0:
if work_stack[len(work_stack) - 1] == '*' or work_stack[len(work_stack) - 1] == '/':
work_list.append(work_stack.pop())
elif work_element == '+' or work_element == '-' or work_element == '(' or work_element == '*' or work_element == '/':
if work_element == '+' or work_element == '-':
if len(work_stack) != 0:
if work_stack[len(work_stack) - 1] == '-' or work_stack[len(work_stack) - 1] == '+':
work_list.append(work_stack.pop())
work_stack.append(work_element)
elif work_element == ')':
while work_stack[len(work_stack) - 1] != '(':
work_list.append(work_stack.pop())
work_stack.pop()
if len(work_stack) != 0:
if work_stack[len(work_stack) - 1] == '*' or work_stack[len(work_stack) - 1] == '/':
work_list.append(work_stack.pop())
if len(temp_expr) == 0:
while len(work_stack) > 0:
work_list.append(work_stack.pop())
temp = ''
while len(work_list) > 1:
for i in range(len(work_list)):
if work_list[i] in ['*', '/', '+', '-']:
if work_list[i] == '*':
temp = work_list[i - 2] * work_list[i - 1]
elif work_list[i] == '/':
temp = work_list[i - 2] / work_list[i - 1]
elif work_list[i] == '+':
temp = work_list[i - 2] + work_list[i - 1]
elif work_list[i] == '-':
temp = work_list[i - 2] - work_list[i - 1]
if len(work_list) > 3:
temp_list = work_list[i+1:]
work_list = work_list[:i - 2]
work_list.append(temp)
work_list += temp_list
else:
work_list = [temp]
break
return float(work_list[0])
except:
raise MathExpressionException(expr)