Apply an operator to two pops
Updated
A binary operator pops the right operand first, then the left: r = pop(); l = pop(); push(l op r). Reverse that and you compute 3-2 as 2-3.
What to focus on
After the push, the result is the new top and may be an operand for a later operator.
Worked examples
add
a = [2, 3], op +- r=3 l=2
- push 5
Result: [5]
sub
a = [5, 2], op -- r=2 l=5
- 5-2=3 not 2-5
Result: [3]
mul
a = [4, 3], op *- r=3 l=4
- push 12
Result: [12]
Try it
Write applyOp(a, op). a is the operand stack, written [bottom, ..., top]. op is +, -, or *. Pop right, then left. Push (left op right). Subtract uses left minus right. On [2, 3] apply + → [5]. On [5, 2] apply - → [3], not -3. On [4, 3] apply * → [12].
The stack has at least two integers before the operator. Operators are + − *.