-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.py
37 lines (29 loc) · 1.26 KB
/
evaluator.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
# Carter Burzlaff - PP Chapter 6
# This program requires the use of Professor Omari's stack_list.py
# This program is very similar to Professor Omari's evel-expr.py, as it is supposed to be a simpler version of it
from stack_list import Stack, Empty
ops = '+-'
test_exp = '2 + 15 + 8 - 4 + 3 - 22 - 6 + 12 - 6'
tokens = test_exp.split()
val_stack = Stack()
op_stack = Stack()
for token in tokens:
if token.isalnum():
try:
top_ops = op_stack.top()
except Empty:
val_stack.push(token)
continue
result = eval('{val1} {op} {val2}'.format(val1 = val_stack.pop(),
val2 = token,
op = op_stack.pop()))
val_stack.push(result)
elif token in ops:
op_stack.push(token)
while not op_stack.is_empty():
result = eval('{val1} {op} {val2}'.format(val1 = val_stack.pop(),
val2 = val_stack.pop(),
op = op_stack.pop()))
val_stack.push(result)
print('\nCalculated Result: {0}'.format(val_stack.pop()))
print('Expected Result: {0}\n'.format(2 + 15 + 8 - 4 + 3 - 22 - 6 + 12 - 6))