-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3.1.py
59 lines (47 loc) · 1.55 KB
/
ex3.1.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
import sys
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
def evaluate_expression(expr):
stack = Stack()
# Split the expression into tokens
tokens = expr.split()
# Process each token
for token in reversed(tokens):
if token == '(':
# Do nothing, '(' is just a separator
pass
elif token in ['+', '-', '*', '/']:
# Pop the last two numbers off the stack
num1 = stack.pop()
num2 = stack.pop()
# Perform the operation and push the result back onto the stack
if token == '+':
stack.push(num1 + num2)
elif token == '-':
stack.push(num1 - num2)
elif token == '*':
stack.push(num1 * num2)
elif token == '/':
stack.push(num1 // num2) # Use integer division instead of float division
elif token == ')':
# Do nothing, ')' is just a separator
pass
else:
# Convert the token to a number and push it onto the stack
stack.push(int(token))
# The result is the last item on the stack
return stack.pop()
# Get the expression from the command line arguments
expr = sys.argv[1]
# Evaluate the expression and print the result
result = evaluate_expression(expr)
print(result)