Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!