Using Python code convert the following infix expressions to postfix using the d
ID: 3562986 • Letter: U
Question
Using Python code convert the following infix expressions to postfix using the direct conversion algorithm. Show the stack as the conversion takes place.
Explanation / Answer
import sys, os stack = [] #list (we can use as a stack) for conversion. postfix = [] #list holder for postfix expression. temp = [] #list holder for temp postfix. infix = [] def main() : #get filename or assume input.txt if len( sys.argv ) < 2 : fileName = "input.txt" else : fileName = sys.argv[1] infix2postfix(fileName) evalPostfix() def infix2postfix(fileName) : fh = open( fileName, "r" ) for line in fh : #Go line by line inside file line = line.strip() #strip off newlines infix = line.split(' ') # split by spaces into a list. infix.append( ")" ) # Add right paren to infix expression. stack.append( "(" ) # Push left paren onto stack. for c in infix : # Go through each token in line. if c == "(" : # Put left paren on stack if in infix exp. stack.append( "(" ) elif c == ")" : # Start popping if right paren encountered. while len(stack) > 0 and stack[-1] != '(' : # Dont check empty list. temp.append( stack.pop() ) if len(stack) > 0 : stack.pop() # Remove ( paren we encountered. elif c == '+' or c == '-' or c == '*' or c == '/' or c == '%' : p = pres(c) # get presedence lastp = pres( stack[-1] ) # presedence of last on stack while p 0 : # Make sure list is not empty. lastp = pres( stack[-1] ) else : # Done. break stack.append( c ) else : temp.append( c ) postfix.append( temp[:] ) # Copy temp, add it to list of postfix exps. del temp[:] # Delete temp so we can start over. # Fuction uses the list of lists postfix created by infix2postfix to # evaluate the mathmatical expressions. def evalPostfix() : del temp[:] # Should already be empty from other function, just in case. a = 0 for entry in postfix : for i in entry : # For each list item in each list. if i == '+' or i == '-' or i == '*' or i == '/' or i == '%' : y = temp.pop() x = temp.pop() temp.append( postfixOp( i,x,y )) else : temp.append(i) print ' '.join(postfix[a]) + " = " + str(temp[a]) a = a+1 # Helper func. to do the actual arithmatic, using the appropriate operator # passed to it. # Params: Operator, number x, number y def postfixOp(op, x, y) : x = float(x) y = float(y) ret = 0.0 if(op == "+") : ret = x + y elif(op == "-") : ret = x - y elif(op == "*") : ret = x * y elif(op == "/") : ret = x / y elif(op == "%") : ret = x % y else : print "Error, tried to parse operator not +-*/%" return ret # Function takes an operator and spits out a numeric precedence. def pres(c) : if c == '+' or c == '-' : p = 1 elif c == '(' : p = 9 else : p = 2 return p main()Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.