## Operators, Functions, and Properties – part 22

In this series, we’ve lately been modeling a four-function calculator as a state machine.  A state machine moves from state to state based on operators that are invoked through button presses.  Operators  take their input, do something with it, and then produce an output.  All the information needed to produce a unique output is present on the inputs (that’s what makes it an operator).

In the previous post, we designed an operator that’s useful for implementing the “=” key of the calculator.  We’ll repeat the diagram here, but for a description you will need to refer to the previous post.

I think it is high time that I present a full design of the simple four-function calculator, even though that will make this blog post very long.  At least it will be mostly diagrams; we will have opportunity in later posts to analyze this design and to note its limitations.  In a sense, this design will bring together a lot of material that was developed earlier in this series; this includes some notational conventions – by no means is this post meant to stand alone.

Remember that we’ve been looking at a very inexpensive four-function calculator, and even so have chosen to ignore many of its keys:

Our approach has been to design a state machine: a machine with state (memory) where the state gets modified under the influence of pressing keys.  Each key invokes an operator that uses the state and modifies it as appropriate:

We’ll show the following operators in detail: the plus-key operator, the clear-key operator, the clear-entry-key operator, the equals-key operator, and the 2-key operator.  The minus-key operator, multiply-key operator, divide-key operator are essentially the same as the plus-key operator; the 0-9 key operators are essentially the same as the 2-key operator.  We start with the equals-key operator:

In this diagram, the blue lines represent the entire state, with its four named components.  Compared to the diagram above it, there is one extra component to the state – we saw the need for it here.  The black arrows represent simple values, often numbers (e.g. 0), but sometimes text (e.g. “none”) – in our presentation, numbers are distinguished from text by having all text in quotes.

Here is the clear-key operator:

In contrast with this clear-key operator, the clear entry operator will clear only some components of the state, and leave other components of the state alone:

Below is the operator for the plus key:

It may be surprise how similar this operator is to the equals key operator.  The “+” key really doesn’t do any adding, it just marks that adding is to be done in the future.  What it does do is handle any pent-up operations: if the user had pressed these keys in sequence:  C 3 × 5 +, then the pressing of the + forces the multiplication to be done, so that the calculator is left in the same state as if C 15 + had been pressed.

The digit key operators, here represented by the 2-key operator, can in some sense be the most surprising.  As we saw in earlier posts, they cause a multiplication:

To complete our presentation of the design, we show the “screen driver”: the logic that determines what is shown on the calculator’s screen.

This is a completion of sorts: we now have a design.  Soon we will find its limitations, and some of those limitations we’ll have an opportunity to fix.  For now – there’s our calculator as a finite state machine.

This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.