Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the Stack.py file from the book Create a file named inToPost.py that impleme

ID: 3796354 • Letter: U

Question

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 post fix 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 u5. 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 separa 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

Explanation / Answer

Please find the code below.

Since there is no mention of executing the code through seperate file or the same file, I have not provided the
output here. I have checked for the output as single files and it is working properly.

Also, please include the below function in Stack class which is required in the code.
   def is_empty(self):
       return self.items == []

-------------------------------------------------------      
inToPost.py

from stack import Stack

def InfixToPostfix(expression):

# Define the Precedence
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
  
# Create an empty stack called dataStack for keeping operators. Create an empty list for output.
dataStack = Stack()
  
   # Create a postfixExp list
postfixExp = []
  
   # Split the tokens.
tokens = expression.split()

for token in tokens:
       # If the token is an operand, append it to the end of the output list.
if token in "0123456789":
postfixExp.append(float(token))
  
# If the token is a left parenthesis, push it on the dataStack.
elif token == '(':
dataStack.push(token)
  
#If the token is a right parenthesis, pop the dataStack until the corresponding left parenthesis is removed.
elif token == ')':
top_token = dataStack.pop()
while top_token != '(':
               # Append each operator to the end of the output list.
postfixExp.append(top_token)
top_token = dataStack.pop()
else:
           # If the token is an operator, *, /, +, or -, push it on the dataStack.
           # first remove any operators already on the dataStack that have higher or equal precedence and append them to the output list.
while (not dataStack.is_empty()) and (prec[dataStack.top()] >= prec[token]):
postfixExp.append(dataStack.pop())
dataStack.push(token)
  
# Any operators still on the stack can be removed and appended to the end of the output list.
while not dataStack.is_empty():
postfixExp.append(dataStack.pop())
  
return " ".join(postfixExp)

# Testing
print(InfixToPostfix("6 * 7 + 8 * 9"))

-----------------------------------------------------------------------
evalPostfix.py
from stack import Stack
import re

def evalPostfix(postfixExpr):
   operand_stack = Stack()
   tokenList = postfixExpr.split()
   print tokenList
  
for token in tokenList:
       # if token is float push to stack
       # using regular expression to find if the string is float.
       if re.match("^d+?.d+?$", token):
           operand_stack.push(token)
       # If the token is an operator, *, /, +, or -, Pop the operandStack twice.   
       else:
           operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
# Perform the arithmetic operation.
result = evaluate(token,operand1,operand2)
# Push the result back on the stack.
operand_stack.push(result)
return operand_stack.pop()

def evaluate(op, op1, op2):
if op == "*":
return op1 * op2
elif op == "/":
return op1 / op2
elif op == "+":
return op1 + op2
else:
return op1 - op2

print(evalPostfix('7.2 8.2 + 3.1 2.0 + /'))