def infix_to_postfix(infix_expr): # converts infix expression to a postfix expression """ Method to convert infix expressions into postix Infix expression should have operations and operands seperated by spaces postfix expression will be formatted that way""" expression = infix_expr.split(" ") # operands = StackArray(len(expression)//2) operands = StackArray(30) to_process = [] for part in expression: # part = expression[i] if not check_num(part): if (part == ')'): while (operands.peek() != '('): to_process.append(str(operands.pop())) operands.pop() # print("Operand: " + part); else: while (not operandOnTop(part, operands)): to_process.append(str(operands.pop())) operands.push(part); # print("Operand: " + part); else: if check_float(part): to_process.append(float(part)) else: to_process.append(int(part)) while (not operands.is_empty()): to_process.append(str(operands.pop())) postfix_expr = " ".join(str(thing) for thing in to_process) # print("Postfix Expression Ready: " + postfix_expr) return postfix_expr
def infix_to_postfix(infixexpr): """Converts an infix expression to an equivalent postfix expression """ prec = {'^':4, '*': 3, '/': 3, '+': 2, '-': 2, '(': 1} opstack = StackArray(30) postfixList = [] tokenList = infixexpr.split(' ')#returns an array with elements split for tok in tokenList: if tok not in "^*()-/+": #if token is a number put in the list postfixList.append(tok) elif tok == '(': #if token is open parenthesis put it in list output opstack.push(tok) elif tok == ')': #if token is close parenthesis toptoken = opstack.pop() #take the top token of stack while toptoken != '(': #pop through the stack and append to output list until you get to the open parenthesis postfixList.append(toptoken) toptoken = opstack.pop() else: while (not opstack.is_empty()) and (prec[opstack.peek()] >= prec[tok]):#handle precedence if opstack.peek() == '^' and tok == "^": break#dont append postfixList.append(opstack.pop()) opstack.push(tok) while not opstack.is_empty():#if empty append everything else postfixList.append(opstack.pop()) return " ".join(postfixList)
def infix_to_postfix(infixexpr): """Converts an infix expression to an equivalent postfix expression """ """Signature: a string containing an infix expression where tokens are space separated is the single input parameter and returns a string containing a postfix expression where tokens are space separated""" operandStack = StackArray(30) postfixList = [] tokenList = infixexpr.split() precedence = {"^": 4, "*": 3, "/": 3, "+": 2, "-": 2, "(": 1} for token in tokenList: if is_valid(token): postfixList.append(token) else: if token == '(': operandStack.push(token) elif token == ')': top = operandStack.pop() while top != '(': postfixList.append(top) top = operandStack.pop() else: while (operandStack.is_empty() == False) and ( precedence[operandStack.peek()] >= precedence[token] and token != "^"): postfixList.append(operandStack.pop()) operandStack.push(token) while operandStack.is_empty() == False: postfixList.append(operandStack.pop()) return " ".join(postfixList)
def postfix_eval(postfixExpr): """ Takes in a valid Postfix Expression and evaluates using the helper function doMath(op,op1,op2)""" newStack = StackArray(30) postfix = postfixExpr.split() for x in postfix: if x not in ('*','-','/','+','^'): newStack.push(x) elif x in ('*','-','/','+','^'): newStack.push(doMath(x,float(newStack.pop()),float(newStack.pop()))) return newStack.pop()
def postfix_eval(postfixExpr): """Evaluate the postfix expression using Stacks, and return final answer """ opStack = StackArray(30) tList = postfixExpr.split() for t in tList: if t in "0123456789": opStack.push(int(t)) else: op2 = opStack.pop() op1 = opStack.pop() result = doMath(t, op1, op2) opStack.push(result) return opStack.pop()
def postfix_eval(postfix_expr): # evaluates a postfix expression """ Evaluates a postfix expression and returns the answer """ to_process = postfix_expr.split(" ") num_nums = postfix_valid(postfix_expr) if not num_nums: raise ValueError nums = StackArray(num_nums) while to_process: if (check_num(to_process[0])): nums.push(float(to_process[0])) del to_process[0] # print("Number: " + str(nums.peek())); else: # print("Operation: "+ to_process[0]); if to_process[0] in ["+", "-", "*", "/", "^"]: nums.push(do_math(nums.pop(), nums.pop(), to_process[0])) del to_process[0] else: del to_process[0] # print("Calculation complete: " + str(nums.peek())) return nums.pop()
def postfix_eval(postfixExpr): """ Purpose """ operandStack = StackArray(30) listexp = postfixExpr.split(' ') for token in listexp: if token not in "^*-+/": operandStack.push(float(token)) else: op2 = operandStack.pop() op1 = operandStack.pop() if token == '^': operandStack.push(doMath(token, float(op2), float(op1))) else: operandStack.push(doMath(token, float(op1), float(op2))) return operandStack.pop()
def infix_to_postfix(infixexpr): """Converts an infix expression to an equivalent postfix expression """ """Signature: a string containing an infix expression where tokens are space separated is the single input parameter and returns a string containing a postfix expression where tokens are space separated""" newStack = StackArray(30) postfixList= [] tokenList = infixexpr.split() x = 0 par = 0 while x < len(tokenList): if tokenList[x] is ('('): #When encountering paranthesis in the input newStack.push(tokenList[x]) par = x+1 while par < len(tokenList) and tokenList[par] not in (')'): if tokenList[par] not in ('*','-','/','+','^','('): postfixList.append(tokenList[par]) elif tokenList[par] in ('('): newStack.push(tokenList[x]) elif tokenList[par] in ('^'): if newStack.peek() in ('('): newStack.push(tokenList[par]) elif newStack.peek() in '^': newStack.push(tokenList[par]) elif newStack.peek() in ('*','/'): newStack.push(tokenList[par]) elif newStack.peek() in ('+','-'): newStack.push(tokenList[par]) elif tokenList[par] in ('*','/'): if newStack.peek() in ('('): newStack.push(tokenList[par]) elif newStack.peek() in ('^'): postfixList.append(newStack.pop()) newStack.push(tokenList[par]) elif newStack.peek() in ('+','-'): newStack.push(tokenList[par]) elif newStack.peek() in ('*','/'): postfixList.append(newStack.pop()) newStack.push(tokenList[par]) elif tokenList[par] in ('+','-'): if newStack.peek() in ('('): newStack.push(tokenList[par]) elif newStack.peek() in ('^','*','/'): while newStack.peek() in ('^','*','/') and newStack.peek() not in ('(') : postfixList.append(newStack.pop()) newStack.push(tokenList[par]) elif newStack.peek() in ('+','-'): while newStack.peek() in ('+','-'): postfixList.append(newStack.pop()) newStack.push(tokenList[par]) par+=1 condition = True while condition: #Popping and appending remaining symbols to list while newStack.peek() not in ('('): postfixList.append(newStack.pop()) newStack.pop() #Removing the ( that was pushed onto the Stack iterate = 0 if newStack.size() == 0: condition = False while iterate < newStack.size(): if newStack.items[iterate] == None: iterate+=1 continue elif newStack.items[iterate] in ('('): condition = True break else: iterate+=1 condition = False x = par elif tokenList[x] not in ('*','-','/','+','^','(',')'): #Encountering a non paranthesis postfixList.append(tokenList[x]) elif tokenList[x] in ('^'): if newStack.is_empty(): newStack.push(tokenList[x]) elif newStack.peek() in '^': newStack.push(tokenList[x]) elif newStack.peek() in ('*','/'): newStack.push(tokenList[x]) elif newStack.peek() in ('+','-'): newStack.push(tokenList[x]) elif tokenList[x] in ('*','/'): if newStack.is_empty(): newStack.push(tokenList[x]) elif newStack.peek() in ('^'): postfixList.append(newStack.pop()) newStack.push(tokenList[x]) elif newStack.peek() in ('+','-'): newStack.push(tokenList[x]) elif newStack.peek() in ('*','/'): postfixList.append(newStack.pop()) newStack.push(tokenList[x]) elif tokenList[x] in ('+','-'): if newStack.is_empty(): newStack.push(tokenList[x]) elif newStack.peek() in ('^','*','/'): while newStack.peek() in ('^','*','/') or not newStack.is_empty(): postfixList.append(newStack.pop()) newStack.push(tokenList[x]) elif newStack.peek() in ('+','-'): while newStack.peek() in ('+','-'): postfixList.append(newStack.pop()) newStack.push(tokenList[x]) x+=1 while not newStack.is_empty(): #Popping and appending remaining symbols to list postfixList.append(newStack.pop()) return " ".join(postfixList) #Creates the Postfix Expression by joining the list