Question: Trying to create a python program that changes infix notation (e.g: 2+3) into postfix notation (2 3 +) using a class in Python 3 I
Trying to create a python program that changes infix notation (e.g: 2+3) into postfix notation (2 3 +) using a class in Python 3
I need to implement a function called postfix(exp) that takes a string exp containing a postfix math expression as input and returns the result as a float.
Here is what I have so far:
class Stack:
#Create a New Empty Stack
def __init__(self):
self.__S = []
def __str__(self):
return str(self.__S)
def push(self,x):
self.__S.append(x)
def pop(self):
return self.__S.pop()
def top(self):
return self.__S[-1]
def postfix(exp): #Needs to be implemented
if __name__ == "__main__":
print('Welcome')
print('Enter Exit to Quit')
input = input('Enter Expression')
while input != "exit":
result = Stack(input)
print("Result:", result)
if input == "exit":
break
here is an example output:
Welcome Enter Exit to Quit Enter Expression: 1 2 + Result: 3.0 Enter Expression: 2 3 + Result: 5.0 Enter Expression: exit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
