<p>Let's go over the expression <span class="fixed">10 4 3 + 2 * -</span> together! First we push <span class="fixed">10</span> on to the stack and the stack is now <span class="fixed">10</span>. The next item is <span class="fixed">4</span>, so we push it to the stack as well. The stack is now <span class="fixed">10, 4</span>. We do the same with <span class="fixed">3</span> and the stack is now <span class="fixed">10, 4, 3</span>. And now, we encounter an operator, namely <span class="fixed">+</span>! We pop the two top numbers from the stack (so now the stack is just <span class="fixed">10</span>), add those numbers together and push that result to the stack. The stack is now <span class="fixed">10, 7</span>. We push <span class="fixed">2</span> to the stack, the stack for now is <span class="fixed">10, 7, 2</span>. We've encountered an operator again, so let's pop <span class="fixed">7</span> and <span class="fixed">2</span> off the stack, multiply them and push that result to the stack. Multiplying <span class="fixed">7</span> and <span class="fixed">2</span> produces a <span class="fixed">14</span>, so the stack we have now is <span class="fixed">10, 14</span>. Finally, there's a <span class="fixed">-</span>. We pop <span class="fixed">10</span> and <span class="fixed">14</span> from the stack, subtract <span class="fixed">14</span> from <span class="fixed">10</span> and push that back. The number on the stack is now <span class="fixed">-4</span> and because there are no more numbers or operators in our expression, that's our result!</p>
0 commit comments