def infix_to_postfix(expression):
    operator_stack = Stack()
    output = []
    token_list = expression.split()
    precedence = {"^": 4, "*": 3, "/": 3, "+": 2, "-": 2, "(": 1}

    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    for token in token_list:
        try:
            int(token)
            number = True
        except ValueError:
            number = False
        if token in chars or number:
            output.append(token)
        elif token == "(":
            operator_stack.add(token)
        elif token == ")":
            stack_token = operator_stack.pop()
            while stack_token != "(":
                output.append(stack_token)
                stack_token = operator_stack.pop()
        else:
            while (not operator_stack.is_empty()) and \
                    (precedence[operator_stack.peek()] >= precedence[token]):
                output.append(operator_stack.pop())
            operator_stack.add(token)

    while not operator_stack.is_empty():
        output.append(operator_stack.pop())

    return " ".join(output)
Esempio n. 2
0
class StackQueue:
    def __init__(self):
        self._stack1 = Stack()
        self._stack2 = Stack()
        self._loaded = True

    def enqueue(self, value):
        if not self._loaded:
            self._reload()
        self._stack1.push(value)

    def dequeue(self):
        if not self._loaded:
            return self._stack2.pop()
        self._unload()
        return self._stack1.pop()

    def peek(self):
        self._unload()
        value = self._stack1.peek()
        self._reload()
        return value

    def count(self):
        return self._stack1.count if self._loaded else self._stack2.count

    def _unload(self):
        while self._stack1.count > 1:
            self._stack2.push(self._stack1.pop())
        self._loaded = False

    def _reload(self):
        while self._stack2.count > 0:
            self._stack1.push(self._stack2.pop())
        self._loaded = True
Esempio n. 3
0
def infixToPostfix(tokenList):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    prec[">"] = 0
    prec["<"] = 0
    prec["="] = 0
    prec[">="] = 0
    prec["<="] = 0

    opStack = Stack()
    postfixList = []
    for token in tokenList:
        if token.isnumeric() or token not in [
                '+', '-', '*', '/', '(', ')', '>', '<', '=', '<=', '>='
        ]:
            if not token.isnumeric():
                token = tabela_sintatica.tabela[token]['value']
                if not token.isnumeric():
                    print("erro! variável sem valor atribuído." + token)
                    exit()
            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 postfixList