Infix to Postfix Due Thursday 02232017 at 1159PM Use the sta

Infix to Postfix Due Thursday 02/23/2017 at 11:59PM Use the stack.py file from the book. Create a file named inToPost.py that implements the i 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 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: (U3utuAu) *u10utuAu 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 join (e) where e is the list of items in the postfix expression. For 5 bonus points, modify your code so that the expression does not require spaces separating each token in the infix expression. Two possible techniques are to write a function that processes 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 a time and determines where one token stops and the next one starts. You may want to append digits of number to a list and then create the number when a non-digit is reached

Solution

# inToPost.py

from Stack import Stack
# Python program to convert infix expression to postfix

# Class to convert the expression
class Conversion:

# Constructor to initialize the class variables
def __init__(self):
self.stack = Stack()
# Precedence setting
self.output = Stack()
self.precedence = {\'+\':1, \'-\':1, \'*\':2, \'/\':2, \'^\':3}

# A utility function to check is the given character
# is operand
def isOperand(self, ch):
return ch.isalpha()

# Check if the precedence of operator is strictly
# less than top of stack or not
def notGreater(self, i):
try:
a = self.precedence[i]
b = self.precedence[self.stack.top()]
return True if a <= b else False
except KeyError:
return False

# The main function that converts given infix expression
# to postfix expression
def inToPost(self, exp):

# Iterate over the expression for conversion
for i in exp:
# If the character is an operand,
# add it to output
if i.isspace():
continue
if self.isOperand(i):
self.output.push(i)

# If the character is an \'(\', push it to stack
elif i == \'(\':
self.stack.push(i)

# If the scanned character is an \')\', pop and
# output from the stack until and \'(\' is found
elif i == \')\':
while( (not self.stack.isEmpty()) and self.stack.top() != \'(\'):
a = self.stack.top()
self.stack.pop()
self.output.push(a)
if (not self.stack.isEmpty() and self.stack.top() != \'(\'):
return -1
else:
self.stack.pop()

# An operator is encountered
else:
while(not self.stack.isEmpty() and self.notGreater(i)):
self.output.push(self.stack.top())
self.stack.pop()
self.stack.push(i)

# pop all the operator from the stack
while not self.stack.isEmpty():
self.output.push(self.stack.top())
self.stack.pop()

result = []
while not self.output.isEmpty():
result.append(self.output.top())
self.output.pop()
print \" \".join(result)

# Driver program to test above function
exp = \"a + b * ( c ^ d - e ) ^ ( f + g * h ) - i\"
obj = Conversion()
obj.inToPost(exp)

# Stack.py

class Stack(object):
def __init__(self):
self.items = []
  
# Push the element to the stack
def push(self, item):
self.items.append(item)
  
# Pop the element from the stack
def pop(self):
if not self.isEmpty():
self.items.pop()
  
# top the element from the stack
def top(self):
return self.items[-1]
  
# return size of stack
def size(self):
return len(self.items)
  
def isEmpty(self):
if self.size() == 0:
return True
else:
return False

Code link

https://goo.gl/2dHaPc

 Infix to Postfix Due Thursday 02/23/2017 at 11:59PM Use the stack.py file from the book. Create a file named inToPost.py that implements the i to postfix algor
 Infix to Postfix Due Thursday 02/23/2017 at 11:59PM Use the stack.py file from the book. Create a file named inToPost.py that implements the i to postfix algor
 Infix to Postfix Due Thursday 02/23/2017 at 11:59PM Use the stack.py file from the book. Create a file named inToPost.py that implements the i to postfix algor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site