Skip to the content of the web site.

Project R.2: Stack calculator

Create a stack calculator. A stack-oriented calculator (one that uses reverse Polish notation, allows the user to perform mathematical operations, but unlike the common in-fix notation (such as $3.2 + 5.7$), stack-oriented calculators have the user enter the operands first, and then the operator.

You will start with an empty array with some pre-determined capacity.

If the user enters a number, that number is put into the next available entry in the array. If the user enters an operator, the last two numbers are taken out of the array, the operation is performed on these two numbers, and the result is placed in place of the first. When an operation is performed, the result is displayed to the screen.

3.5
7.9
1.8
+
    -> 9.7
*
    -> 33.95

This is the approach a number of HP calculators use, and it has a clear advantage over traditional calculators: it is never necessary to use brackets, as the order of operations is not ambiguous.

To review the ambiguity of infix notation, the expression $3 + 5 \times 7$ could be interpreted as $(3 + 5) \times 7 = 8 \times 7 = 56$ or $3 + (5 \times 7) = 3 + 35 = 38$. By convention, we assume multiplication is performed first.

If we were using our stack-oriented calculator, these two operations would be

3
5
+
    -> 8
7
*
    -> 56

and

5
7
*
    -> 35
3
+
    -> 38

You may approach this project in one of three ways:

  • Have a default fixed-size static array.
  • Query the user for the size of the array, and then allocate memory for an array of that size.
  • Create an array with an initial size 16, and if more than 16 objects get put onto the array, create a new array of double the capacity, copy over the 16 entries, delete the old array, and continue working with the new array. If the array becomes full again, when the next number is placed into the array, create a new array of double the previous capacity, copy the entries in the old array over, delete the old array, and continue working with the new array.

Non-commutative operators

If you get an operator such as -, / and ^, there is a question of whether or not you meant to, for example, subtract the last entry added to the array from the second-last, or vice versa. We will use the convention that if you enter two numbers and then a non-commutative operator, you expected to perform them in order:

5
7
-
    -> -2
4
^
    -> 16
5
/
    -> 3.2

Unary operators and functions

You can continue with this project as follows:

  1. If the user enters !, replace the entry at the end of the array with the factorial of that value.
  2. If the enters sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, exp, log, log10, log2, sqrt, apply the replace the entry at the end of the array with the function applied to that value.
5
!
    -> 120
sin
0.5806111842123142892824
2
^
0.3371093472324260
120
cos
0.8141809705265618
2
^
0.6628906527675741
+
    -> 120

Constants

Instead of only looking for numeric values versus operators, you could also allow the user to enter symbols representing specific values:

SymbolValue
pi3.141592653589793
e2.718281828459045
phi1.618033988749895
gamma0.5772156649015329
inffloating-point infinity
nannot-a-number

If any of these constants are added, the result is placed on the stack.

Clearing the array

If the user enters clear, reset the array and thereby emptying it.

+
Invalid operation: no entries in the array
5
7
+
    -> 12
clear
4
+
Invalid operation: only one entry in the array