Ejemplo n.º 1
0
    def postfix_from(self, infix):
        infix = self.__prepare(infix)
        prec = {}
        prec["^"] = 4
        prec["*"] = 3
        prec["/"] = 3
        prec["+"] = 2
        prec["-"] = 2
        prec["("] = 1
        opStack = Stack()
        postfixList = []
        tokenList = infix.split()

        for token in tokenList:
            if self.__is_number(token) or token == "pi" or token == "e":
                postfixList.append(token)
            elif token == '(':
                opStack.push(token)
            elif token == ')':
                topToken = opStack.pop()
                while topToken != '(':
                    postfixList.append(topToken)
                    topToken = opStack.pop()
            else:
                while (not opStack.isEmpty()) and \
                   (prec[opStack.peek()] >= prec[token]):
                    postfixList.append(opStack.pop())
                opStack.push(token)

        while not opStack.isEmpty():
            postfixList.append(opStack.pop())
        return " ".join(postfixList)
Ejemplo n.º 2
0
def postfix_converter(infixstring):
    op_stack = Stack()
    output_list = []
    infix_list = infixstring.split(' ')
    operators = '*/+-'

    for char in infix_list:
        if char == '(':
            op_stack.push(char)
        elif char == ')':
            prior_op = op_stack.pop()
            while prior_op != '(':
                output_list.append(prior_op)
                prior_op = op_stack.pop()
        elif char in operators:
            if op_stack.contains_items() and op_stack.peek() in '*/':
                output_list.append(op_stack.pop())
                op_stack.push(char)
            else:
                op_stack.push(char)
        else:
            output_list.append(char)

    while op_stack.contains_items():
        output_list.append(op_stack.pop())

    return " ".join(output_list)
def infixxer(postfix):
    s = Stack()
    l = len(postfix)
    for i in range(l):
        if (isOperator(postfix[i])):
            temp = s.pop()
            temp2 = '(' + s.pop() + postfix[i] + temp + ')'
            s.push(temp2)
        else:
            s.push(postfix[i])

    return s.peek()
Ejemplo n.º 4
0
def postfixxer(prefix):
    s=Stack()
    l=len(prefix)
    for i in range(l-1,-1,-1):
        if(isOperator(prefix[i])):
            temp=s.pop()
            temp2=temp+s.pop()+prefix[i]
            s.push(temp2)
        else:
            s.push(prefix[i])

    return s.peek()       
Ejemplo n.º 5
0
def prefixxer(postfix):
    s = Stack()
    l = len(postfix)
    for i in range(l):
        if (isOperator(postfix[i])):
            temp = ''
            temp2 = ''
            temp = s.pop()
            temp2 = postfix[i] + s.pop() + temp
            s.push(temp2)
        else:
            s.push(postfix[i])

    return s.peek()
Ejemplo n.º 6
0
def postfix(infix):
    
    #op stack is where we store left parentheses, it will count
    #how much we need to pop when we hit a right parenthesis
    opstack=Stack()
    #output will be the return of all our values
    output=[]
    #splits up the infix notation equation
    input1=list(infix)
    #declare a dict
    prec={}
    #determine the level of precedence for operators
    prec['*']=3
    prec['/']=3
    prec['+']=2
    prec['-']=2
    prec['(']=1
    
    for i in input1:
        #append non operators/ parenthesises into output
        if i in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1023456789':
            output.append(i)
        #if is a left parenthesis, append it to the stack
        elif i =='(':
            opstack.push(i)
        #if it is a right parenthesis,
        #pop a left parenthesis out of the stack
        elif i==')':
            topToken=opstack.pop()
        #while the top of the stack is NOT a left parenthesis and
        #is an operator, keep on popping that shit into the output
            while topToken !="(":
                output.append(topToken)
                topToken=opstack.pop()
        #else if it is an operator
        else:
            #while the opstack is NOT empty and and the operator's precident in the stack
            #is GREATER than the operator currently being iterated on
            #pop that operator into the stack
            while (not opstack.isEmpty()) and (prec[opstack.peek()]) >=prec[i]:
                output.append(opstack.pop())
            #otherwise, just push the current operator into the stack
            opstack.push(i)
    #and this last while loop is to get out the remaining operators at the end
    while not opstack.isEmpty():
        output.append(opstack.pop())
    return " ".join(output)