Postfix Calculator

Stack Calculator Postfix Evaluation

Postfix Calculator

Implement a function that evaluates a postfix (Reverse Polish Notation) expression. The expression is given as a string where tokens (operands and operators) are space-separated.

The allowed operators are +, -, *, and /. Operands are integers. Use a stack data structure to evaluate the expression:

  1. Split the expression into tokens.
  2. Iterate over each token:
    • If the token is a number, push it onto the stack.
    • If the token is an operator, pop the top two numbers from the stack, apply the operator (note: the first popped element is the second operand), and push the result back onto the stack.
  3. At the end, the stack should contain a single element which is the result of the expression.

Assume that the expression is valid and well-formed.

Hint: As today is July 10, consider this a mid-summer challenge to practice your stack skills!