def eval_rpn(rpn, variables): stack = Stack() for el in rpn: if el in operators: if stack.Length() < 2: raise Exception("Missing an operand") res = Evaluator.process_op(stack.pop(), stack.pop(), el) stack.push(res) else: if el in variables: stack.push(int(variables[el])) else: stack.push(el) if stack.Length() > 1: raise Exception("Missing an operation") return stack.pop()
def infix_to_postfix(infix): rpn = [] stack = Stack() for idx, token in enumerate(infix): if token in operators: while stack.Length() > 0 and Converter.opcmp(stack.peek(), token) >= 0: rpn.append(stack.pop()) stack.push(token) elif token == '(': stack.push(token) elif token == ')': while stack.peek() != '(': rpn.append(stack.pop()) stack.pop() else: rpn.append(token) while not stack.isEmpty(): rpn.append(stack.pop()) return rpn