Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Desciption: Modify the postfix evaluation algorithm so that it can handle errors. We still assume that: 1. the postfix expression is a string of

Problem Desciption:

Modify the postfix evaluation algorithm so that it can handle errors. We still assume that: 1. the postfix expression is a string of tokens delimited by spaces. 2. there are only 4 Aritmetic operators * / + - . 3. the operands are single digit integer values.

Your program should be able to detect (1) 'mmissing operator' and (2) 'missing operand" errors for example.

Missing operator

7 8 + 3 2 + ..is a wrong expression b/c an operator is required after computing 7 +8 and 3+2

Missing operand

7 + 3 2 + / ...is a wrong postfix expression b/c another operand is required for 7+

Sample Input / output

$ python3 hw3.py

enter a postfix expression: 7 8 + 3 + 2 + / (this is what you enter in )

3.0

$ python3 hw3.py

enter a postfix expression : 7 8 + 3 2 +

Error!

$ python3 hw3.py

enter a postfix expression : 7 + 3 2 + /

Error!

Submission Guidelines

1) Maximum points for a non-excutable program is 2.5/5. Your program should run on Citrix

2) The file name is hw3.py

3) The output of my program is same as the output presented in the sample output

4) Using python 3 interpreter (The input () function does not work in python 2)

Hint

1. You can abort a python script by calling exit() function

e.g

for i in range (10):

print (i)

if i == 5:

exit()

2. To see the data in a list (e.g. stack), run

print (stack)

**********postfixEval**********

def postfixEval(postfixExpr):

stack = [ ]

tokenList = postfixExpr. split()

for token in tokenList:

if token in "0123456789":

stack.append(int(token))

else:

operand2 = stack.pop()

operand1 = stack.pop()

result = doMath(token,operand1, operand2)

stack.append(result)

return stack.pop()

def doMath(op,op1,op2):

if op == " * ";

return op1 * op2

elif op == "/":

return op1 / op2

elif op == "+":

return op1 + op2

else:

return op1 - op2

instr = input('enter a postfix expression : ')

print (postfixEval(instr))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions