This sample consists of a command line reverse Polish notation (RPN) calculator.
Note: A RPN calculator works slightly differently from standard calculators. It consists of a stack of numbers. Operations are applied to the two numbers on top of the stack. The result is then put on top of the stack so that it can be used in the next operation. This sample refers to the top of the stack as Accumulator.
After you launch the sample, the following text appears in a console:
********************************* Calculator in reverse Polish form ********************************* Allowable operations are: '/': Divide top two numbers on the stack. '0': Empty the stack. 'a': Enter operand onto stack. '?': Help. '*': Multiply top two numbers on the stack. '+': Add top two numbers on the stack 'q': Quit. '-': Subtract top two numbers on the stack. Enter a number, followed by:
Enter the first number to be put onto the stack, for example 3.
Note: Failing to enter a number at this stage will cause the sample to stop. This sample was designed to showcase the use of EiffelBase data structures and is not protected against unexpected entries.
You may then add another number on the stack by entering the character a:
********************************* Calculator in reverse Polish form ********************************* Allowable operations are: '/': Divide top two numbers on the stack. '0': Empty the stack. 'a': Enter operand onto stack. '?': Help. '*': Multiply top two numbers on the stack. '+': Add top two numbers on the stack 'q': Quit. '-': Subtract top two numbers on the stack. Enter a number, followed by: 3 Accumulator = 3 Next operation? a Enter a number, followed by :
Enter a second number, for example 2. You can then apply any operation to the two operands such as minus:
... Next operation? a Enter a number, followed by: 2 Accumulator = 2 Next operation? - Accumulator = 1 Next operation?
You may use the operation 0 to clear the stack at any time. You may use q to quit the program.
Tip: You can use the command ? to display the list of available operations.
This sample shows how to leverage EiffelBase data structures in a simple Eiffel system. The root class CALCULATOR first instantiates all state objects, each corresponding to one possible operation. The state classes all inherit from STATE. They are: