Use the Stackpy file from the book Create a file named inToP

Use the Stack.py file from the book Create a file named inToPost.py that implements the infix to postfix algorithm described in the book. The function must be named inToPost and take one parameter that is a string representing the infix expression. As you process the numbers in the string, convert them all to a floating point value. The function must return a string representing the corresponding postfix expression. Also write a function named evalPostfix that evaluates the resulting postfix expression (using the Stack class as described in the book) and returns a floating point value. Your main function must ask you to input an infix expression and call the inToPost function. Output the resulting postfix expression. Call the evalPostfix function with with the postfix expression and output the result of it (the answer to the original problem). You may not call the Python eval function. You may assume that each token in the infix expression is separated by a space. The following expression is an example of this: (U3utu4u)U*u10utu4u us. This will allow you to use the string split method to break the string into separate tokens to process. When you create your postfix expression, also put spaces between each token to make it easier to process. I suggest you append each token to a list and then turn that into a string using the statement \'u\' .join (e) where e the list of items in the postfix expression For 5 bonus points, modify your code so that the expression does not require spaces separ ach token in the infix expression. Two possible techniques are to write a function that pro the expression a character at a time and adds spaces creating a new string that can be sent to the existing function. The other technique is to directly process the expression a character at and determines where one token stops and the next one starts. You may want to append digits o a number to a list and then create the number when a non-digit is reached

Solution

import math
class Stack:
def __init__(self):
self.items = []
self.length = 0
  
def push(self, val):
self.items.append(val)
self.length += 1
  
def pop(self):
if self.empty():
return None
self.length -= 1
return self.items.pop()
  
def size(self):
return self.length
  
def peek(self):
if self.empty():
return None
return self.items[0]
  
def empty(self):
return self.length == 0
  
def __str__(self):
return str(self.items)

operatorPrecedence = {}
operatorPrecedence[\'*\'] = 3
operatorPrecedence[\'/\'] = 3
operatorPrecedence[\'+\'] = 2
operatorPrecedence[\'-\'] = 2
operatorPrecedence[\'(\'] = 1

def inToPost(expr):
print(evalPostfix(expr.split()))
  
def evalPostfix(postfixTokens):
postfixExpr = []
postfixStack = Stack()
  
for postfixToken in postfixTokens:
if postfixToken.isidentifier():
postfixExpr.append(postfixToken)
elif postfixToken == \'(\':
postfixStack.push(postfixToken)
elif postfixToken == \')\':
while True:
tempToken = postfixStack.pop()
if tempToken is None or tempToken == \'(\':
break
elif not tempToken.isidentifier():
postfixExpr.append(tempToken)
  
else: # if operator
if not postfixStack.empty():
tempToken = postfixStack.peek()
  
while not postfixStack.empty() and operatorPrecedence[tempToken] >= operatorPrecedence[postfixToken] and postfixToken.isidentifier():
postfixExpr.append(postfixStack.pop())
tempToken = postfixStack.peek()
  
postfixStack.push(postfixToken)
  
while not postfixStack.empty():
postfixExpr.append(postfixStack.pop())
res = \'\'.join(str(e) for e in postfixExpr)
   ns = vars(math).copy()
   ns[\'__builtins__\'] = None
   return eval(res, ns)
  
  
inToPost(\"1+2*3\")

 Use the Stack.py file from the book Create a file named inToPost.py that implements the infix to postfix algorithm described in the book. The function must be
 Use the Stack.py file from the book Create a file named inToPost.py that implements the infix to postfix algorithm described in the book. The function must be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site