[[Property:title|EiffelBase Sample: Calculator]]
[[Property:weight|0]]
[[Property:uuid|12c3409d-165b-b1df-e26a-b05d49969661]]
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''.This sample consists of a command line reverse Polish notation (RPN) calculator. }}

==Compiling==

To compile the example: 
# Launch EiffelStudio.
# Click '''Add project'''
# Browse to ''$ISE_EIFFEL\examples\base\calculator\''
# Choose ''calculator.ecf''
# Choose the location where the project will be compiled, by default the same directory containing the configuration file.
# Click '''Open'''.

==Running==

After you launch the sample, the following text appears in a console: 
<code>
*********************************
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 :
					</code>

Enter the first number to be put onto the stack, for example <code>3</code>. 

{{caution|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 <code>a</code>: 
<code>
*********************************
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 :
						</code>

Enter a second number, for example <code>2</code>. You can then apply any operation to the two operands such as minus: 
<code>
...
Next operation? a
Enter a number, followed by : 2

Accumulator = 2

Next operation? -

Accumulator = 1

Next operation?
							</code>

You may use the operation <code>0</code> to clear the stack at any time. You may use <code>q</code> to quit the program. 

{{tip|You can use the command <code>?</code> to display the list of available operations. }}

==Under the Hood==

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: 
* PLUS: Add stack's two top numbers. 
* MINUS: Substract stack's two top numbers. 
* MULTIPLY: Multiply stack's two top numbers. 
* DIVIDE: Divide stack's two top numbers. 
* EMPTY: Empty stack. 
* HELP: Prints available commands. 
* QUESTION: Get number from user. 
* QUIT: Close application. 
Each of these classes implement the feature do_one_state from STATE which performs the operation associated with the state. The initial state is QUESTION which asks for the initial number to put in the ''accumulator''. Following states depend on the user input. <br/>
Every descendant of STATE implement the feature operation which performs the corresponding stack transformation. 

See the reference for the class interfaces. 






