Given a numeric expression, evaluate the expression and give the result. For instance: 4-3*12+300
Anonymous
Assuming no parentheses, you need two stacks: an operand stack, and an operator stack. Alternate parsing operands and operators. Whenever you encounter an operator that has precedence less than or equal to the operator at the current top of the stack, pop the operator and the topmost two operands, evaluate them, and push the result on the operand stack. You can treat the "end of string" as through it were an operator of the lowest precedence. Handling parentheses is a bit trickier. When you encounter the left paren, you always push it onto the operator stack. Then you treat it as though it were an operator of the lowest precedence; this forces you to stay within that paren group until you encounter the corresponding right paren.
Check out your Company Bowl for anonymous work chats.