A04 – RPN Calculator

Create a reverse polish notation (RPN) calculator using LabVIEW. In RPN arithmetic the operators follow the arguments on which they they operate. For example to write the expression A sin (k x ωt) in RPN one could write

k x * ω t * – sin A *

Notice that in RPN no parenthesis are needed. To implement this type of arithmetic on a calculator one uses a stack.

Values are pushed onto the stack from the top and then removed from the top of the stack as needed. This would be a LIFO (Last In First Out) type stack. We will use an array to serve as the stack. In the above example, the value of k would be placed on the stack and then the value of x placed on the stack. The * operator would remove the k and the x and replace these two values with the single value equal to the product kx. Notice that some operators (like sin above for example) will only take one value from the stack and replace it with an altered value.

You can learn how a RPN Calculator should work using a sample calculator application (depicted below) which is available for Windows or Mac OS X as an attachment at the bottom of this page.

Your version is to implement at a minimum the following:

  • Ability to shut down the program by pressing the X in the top right of the application window.  A confirmation dialog should be presented to allow the user to confirm the exit.
  • Ability to enter a new value onto the stack by typing it into a register control. 
  • After a new value is placed on the stack the register should be cleared and the cursor should be placed back into the register control. To implement these features you will want to take a look at Property Nodes (especially Key Focus).
  • Be certain that the width of the Stack and the width of the Register are adequate to fully display both large and small numbers in scientific notation.
  • The stack operations buttons:
    • DROP – Removes the top value from the stack (if it exists).
    • DUP – Pushes a duplicate copy of the top value onto the stack.
    • CLEAR – Clears the entire stack contents.
    • SWAP – Swaps the two top values on the stack.
  • The unary (one argument) operators: SIN, COS, TAN, ASIN, ACOS, ATAN, SQRT, LN, Degrees to Radians, Radians to Degrees, 1/x, x^2, e^x and +/- (changes sign of top of stack).
  • The four binary (two argument) arithmetic operators (+, -, * and /). Others could be added like y^x for example.
  • Constants such as Pi or e.
  • The stack contents should be displayed on the front panel.
  • The application should behave as expected if there are not enough numbers on the stack to perform the requested operation.
  • The application should be written using an Event-Driven State Machine design pattern and you should make efforts to not repeat code unnecessarily (i.e. Attempt to group events that remove two elements from the stack and replace them with one together for example).
  • There should be a button that will toggle the context-sensitive help window to show descriptions for the behavior of the front panel buttons.
  • On shutdown the program should save the contents of the stack and the Angle Mode setting (if the extra credit is attempted) to a configuration file that is stored in the same directory as the application.
  • On startup, the program should load the contents of the stack and the Angle Mode setting (if the extra credit is attempted) from the configuration file.

Demonstration Videos

The following is a video demonstration of how the RPN Calculator application should work.

The next video gives an overview of how to get started setting up the block diagram for the main application in an Event Driven State Machine design pattern.

Finally, this video gives a behind the scenes look at how to effectively group some of the buttons together to make the code easier to write along with some suggestions for how to interact with the configuration file.

Extra Credit

Of course LabVIEW, like most programming environments, expects angles given as parameters to trig functions to be in radians.  As an extra credit opportunity, you can implement an Angle Mode setting that will allow the operator to change the mode between Degrees and Radians.  This control could be an Enum (as in my example below) or could be a radio button.  This setting will effect how the arguments to the trig functions SIN, COS, and TAN are treated and will effect the return values from the inverse trig functions ASIN, ACOS, and ATAN.  If you implement this option, then the setting of this control should be stored and recalled from the configuration file as described above.

Here is a quick video that describes an alternative control that could be used to select the angle mode for the calculator.


You can install a fully working version of the RPN Calculator application on your own computer so that you can test how different operations should work using the RPN logic.  The link below is a ZIP file that includes an installer that will install the RPN Calculator on your computer and will create a shortcut to it on your desktop.  Download this file and then unzip its contents.  Inside you will find a file named install.exe that will run the installer.

Print Friendly, PDF & Email