Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code currently only accepts and evaluates a postfix expression. How to covert it so it only takes prefix expressions? from stack import * def

My code currently only accepts and evaluates a postfix expression. How to covert it so it only takes prefix expressions?

from stack import *

def postfix(exp): expList = exp.split(" ") stack = Stack() for item in expList: if item == "+": x1 = stack.pop() x2 = stack.pop() stack.push(x2 + x1)

elif item == "-": x1 = stack.pop() x2 = stack.pop() stack.push(x2 - x1) elif item == "*": x1 = stack.pop() x2 = stack.pop() stack.push(x2 * x1)

elif item == "/": x1 = stack.pop() x2 = stack.pop() stack.push(x2 / x1)

else: num = int(item) stack.push(num)

return stack.pop()

print("Format: 4 6 + 2 / -1 * 7 + 7 +") exp = input("Please enter postfix expression: ")

# Stack.py class Stack: #------------------------------------------------------------ def __init__(self): '''post: creates an empty LIFO stack''' self.items = [] #------------------------------------------------------------ def push(self, item): '''post: places x on top of the stack''' self.items.append(item) #------------------------------------------------------------ def pop(self): '''post: removes and returns the top element of the stack''' return self.items.pop() #------------------------------------------------------------ def top(self): '''post: returns the top element of the stack without removing it''' return self.items[-1] #------------------------------------------------------------ def size(self): '''post: returns the number of elements in the stack''' return len(self.items)

image text in transcribed

from stack import def postfix (exp) expList exp . split(" stack Stack ( ) for item in expList: ") if item"+": x1 = stack.pop ( ) x2 stack.pop ( ) stack.push (x2 + xl) elif item- x1 = stack.pop ( ) x2 stack.pop ( ) stack.push (x2 - xl) elif tem == xl - 3tack.pop () x2 stack.pop ( ) stack.push (x2 * xl) '" * " : elif item " x1 = stack.pop ( ) x2 - stack.pop) stack.push (x2 / xl) else: num = int (item) stack.push (num) return stack.pop () exp input ("Please enter postfix expression: total postfix (exp ) print (total) ")

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_2

Step: 3

blur-text-image_3

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

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

Describe the micturition reflex.

Answered: 1 week ago