Ejemplo n.º 1
0
def postfixEval(postfixExpr):
    operandStack = Stack()
    tokenList = postfixExpr.split()

    for token in tokenList:

        if token in number(token):
            operandStack.push(token)
        else:
            try:
                operand2 = operandStack.pop()
                operand1 = operandStack.pop()
            except IndexError:
                print(postfixExpr + ": Bad Expression")
                return

            result = doMath(token, operand1, operand2)
            if result is not None:
                operandStack.push(result)
            else:
                print(postfixExpr + ": " + "Bad Expression")
                return

    return operandStack.pop()
Ejemplo n.º 2
0
def infixToPostfix(infixexpr):
    prec = {}
    prec["^"] = 3
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or is_float(token):
            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.º 3
0
def process(q, a, b):
    q_s = Stack()
    for i in q.items:
        q_s.push(i)

    a_s = Stack()
    b_s = Stack()

    while not q_s.isEmpty():
        tmp_op = q_s.pop()
        tmp_name = q_s.pop()
        if tmp_op == "A":
            a_s.push(tmp_name)
        elif tmp_op == "B":
            b_s.push(tmp_name)
        elif tmp_op == "X":
            if a_s.size() < b_s.size():
                a_s.push(tmp_name)
            elif a_s.size() > b_s.size():
                b_s.push(tmp_name)
            elif a_s.size() == b_s.size():
                tmp_name = None
                tmp_op = None

    for x in a_s.items:
        a.enqueue(x)

    for y in b_s.items:
        b.enqueue(y)

    for j in q_s.items:
        q.enqueue(j)

    return q.items, a.items, b.items
Ejemplo n.º 4
0
def merge(a, b):
    sa = Stack()
    sb = Stack()

    #   Inserir os items de cada queue em stacks independentes
    for i in a.items:
        sa.push(i)

    for j in b.items:
        sb.push(j)

    #   Comparar os 2 stacks
    sc = Stack()
    while not sa.isEmpty() and not sb.isEmpty():
        if sa.peek() < sb.peek():
            tmp = sa.pop()
            sc.push(tmp)
            tmp = sb.pop()
            sc.push(tmp)
        elif sb.peek() < sa.peek():
            tmp = sb.pop()
            sc.push(tmp)
            tmp = sa.pop()
            sc.push(tmp)

    #   Virar o stack ao contrario
    sd = Stack()
    while not sc.isEmpty():
        tmp = sc.pop()
        sd.push(tmp)

    #   Inserir o stack temporario na queue final
    c = Queue()
    for y in sd.items:
        c.enqueue(y)

    return c
Ejemplo n.º 5
0
def infixToPostfix(infixexpr):
    prec = {}
    prec["^"] = 3
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or is_float(token):
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                try:
                    topToken = opStack.pop()
                except IndexError:
                    return "Expressão Inválida: " + infixexpr
        else:
            try:
                while (not opStack.isEmpty()) and (prec[opStack.peek()] >=
                                                   prec[token]):
                    postfixList.append(opStack.pop())
                opStack.push(token)
            except KeyError:
                return "Operador Inválido: " + "'" + token + "'" + " de " + infixexpr

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList)
Ejemplo n.º 6
0
 def __init__(self):
     self.input = Stack()
     self.output = Stack()