Simple Stack Machine

written by Teresa Carrigan


Run model in your browser


Top

THINGS TO NOTICE

Every time you Push an item, the stack gets one more item on top. For every time you Pop, Add, Sub, Mul, or Div, the stack loses one item.

What happens when you try to Pop and the stack is empty? What happens when you have a single item on the stack, and you try to Add? When you press Sub, how does the stack determine which item is subtracted?

Top

THINGS TO TRY

Convert an arithmetic expression to Reverse Polish Notation (also known as postfix). Enter this RPN expression as a series of stack commands. The final result popped will be the normal infix equivalent of the expression. For example,

C 4 + 3 A - *

would be entered as Push C, Push 4, Add, Push 3, Push A, Sub, Mul, Pop. What is popped should be (C + 4) * (3 – A).


Home

Applets on this website were written by Teresa Carrigan in 2004, for use in computer science courses at Blackburn College, with the exception of the Fireworks applet. The applets made with NetLogo require Java 1.4.1 or higher to run. The applets made with NetBeans require Java 1.4.2 or higher to run. Applets might not run on Windows 95 or Mac OS 8 or 9. You may obtain the latest Java plugin from Sun's Java site.