-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_calculator.py
62 lines (55 loc) · 1.94 KB
/
postfix_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
def infix_to_postfix(infix_expression):
precedence = {'+': 1, '-': 1, '*': 2, '/': 2}
output = []
stack = []
def is_operator(token):
return token in '+-*/'
for token in infix_expression.split():
if token.isdigit() or (token[0] == '-' and token[1:].isdigit()):
output.append(token)
elif is_operator(token):
while (stack and is_operator(stack[-1]) and
precedence[token] <= precedence[stack[-1]]):
output.append(stack.pop())
stack.append(token)
elif token == '(':
stack.append(token)
elif token == ')':
while stack and stack[-1] != '(':
output.append(stack.pop())
if stack and stack[-1] == '(':
stack.pop()
else:
raise ValueError("Mismatched parentheses")
while stack:
if stack[-1] == '(':
raise ValueError("Mismatched parentheses")
output.append(stack.pop())
return ' '.join(output)
def evaluate_postfix(postfix_expression):
stack = []
for token in postfix_expression.split():
if token.isdigit() or (token[0] == '-' and token[1:].isdigit()):
stack.append(float(token))
elif token in '+-*/':
if len(stack) < 2:
raise ValueError("Insufficient operands for operator")
b = stack.pop()
a = stack.pop()
if token == '+':
result = a + b
elif token == '-':
result = a - b
elif token == '*':
result = a * b
elif token == '/':
if b == 0:
raise ValueError("Division by zero")
result = a / b
stack.append(result)
else:
raise ValueError(f"Invalid token: {token}")
if len(stack) == 1:
return stack[0]
else:
raise ValueError("Invalid expression")