From a plain-language description to a Finite-State Machine model of a calculator’s behavior.

A calculator — functional description

Let’s assume we’re holding the following pocket calculator.

pocket calculator

Citizen LC-110NR pocket calculator (photo: MichalR, CC BY-SA 3.0, Wikimedia Commons)

Let’s describe a simplified way this calculator works: buttons, calculation rules, display, and error handling.

Buttons

  • Enter numbers using 0–9 and .
  • Perform calculations with +, , ×, ÷
  • Press = to show the result
  • Press = again to repeat the last calculation using the last operator and operand
  • % converts the current value to a percentage
  • +/- changes the sign of the current value
  • SQRT returns the square root of the current value (negative -> 0)
  • CE Clears the current Entry (the number being entered)
  • C Clears the entire calculation and resets the calculator
  • M+ (Memory add) adds the current display to the stored value
  • M- (Memory subtract) subtracts the current display from the stored value
  • MRC (Memory Recall/Clear) shows the stored value; press it twice to Clear the memory

Calculation rules

  • Calculations are done step by step and can be chained (e.g. 7 + 3 + 2 = 12)
  • Unary actions (%, +/-) always affect the number currently shown
  • Memory operations (M+, M-) always use the number currently shown

Display behavior

  • The screen shows the current input or the last result
  • Selecting a binary operator clears the display until the next digit or unary action
  • Typing a number after a result starts a new calculation
  • Recalling memory displays the stored memory value without clearing the calculation state
  • Only one decimal point is allowed per number

Error handling

  • Invalid actions are ignored (e.g. CE when no entry is active, operators in the cleared state)
  • Division by zero is handled safely
  • The calculator stays responsive in all cases

Finite State Machine concepts applied to the calculator

We’ll start by introducing basic concepts and end with complete Finite State Machine (FSM) and Deterministic Finite Automaton (DFA), also known as a Deterministic Finite-State Machine (DFSM) definitions.

Action

An operation performed by the calculator.

Example: update the display, evaluate an expression.

Event / Input

A key press received by the calculator.

Example: digit, +, =.

State

The calculator’s status while it is waiting for the next input, that is, while it is waiting to execute a transition.

Example: The calculator is displaying 7 and is waiting for the next key press.

Initial state

The state before any input is received by the calculator.

Example: The calculator has just been turned on or cleared and shows an empty screen.

Transition

The change from one calculator state to another triggered by an input;
actions may occur as part of the transition.

Example: Pressing C transitions the calculator to the cleared state, clearing the display.

Finite State Machine (FSM)

A calculator model defined by a finite set of calculator states, calculator inputs, and calculator transitions.

Example: The calculator FSM consists of states such as cleared, entering number, and result, with key presses (digit, +, =) triggering transitions between them.

Deterministic Finite-State Machine (DFSM)

Deterministic Finite-State Machine (DFSM) is a calculator’s Finite State Machine (FSM) in which each key press, in each state, leads to exactly one next state. It is also called a Deterministic Finite Automaton (DFA); this post uses DFSM.

Example: Pressing C in any state transitions the calculator to the cleared state.

State machine diagram

State machine diagram

State machine diagram: calculator states, inputs, and transitions (source)

Diagram symbols:

  • boxes = states (Q)
  • arrowed lines = transitions (δ)
  • labels = inputs (Σ)
  • start marker = black dot/arrow to initial state (q₀ = CLEARED)
  • accepting state = RES (F), valid end of calculation

This diagram represents the calculator’s Finite State Machine, showing its states, inputs, and transitions. It is a Deterministic Finite State Machine because each transition is unique: each state/input pair has exactly one next state.

Note that DFSM diagrams generally omit outputs/actions; those would be shown in a Mealy machine or a Moore machine. Memory buttons are also omitted here on purpose; see below for why.

Let’s read the diagram by tracing 2 + 2 =.

  1. Start at the initial state: the black dot/arrow points to CLEARED (q₀).
  2. Press a digit (Σ): transition to ARG1_INT (action: update display with the digit).
  3. Press +: transition to OP (action: clear the display until the next digit).
  4. Press a digit: transition to ARG2_INT (action: update display with the digit).
  5. Press =: transition to RES (action: show the result).

State transition table

The state-transition table below corresponds to the state machine diagram. Columns list inputs (Σ), rows list states (Q) — see the diagram legend for state names. Each cell shows the next state.

Q \ ΣDigitDOTBinaryOprUnaryOpr=CCE
CLEAREDARG1_INTARG1_DECCLEAREDCLEAREDCLEAREDCLEAREDCLEARED
ARG1_INTARG1_INTARG1_DECOPARG1_INTRESCLEAREDCLEARED
ARG1_DECARG1_DECARG1_DECOPARG1_DECRESCLEAREDCLEARED
OPARG2_INTARG2_DECOPOPRESCLEAREDOP
ARG2_INTARG2_INTARG2_DECOPARG2_INTRESCLEAREDOP
ARG2_DECARG2_DECARG2_DECOPARG2_DECRESCLEAREDOP
RESARG1_INTARG1_DECOPRESRESCLEAREDRES

This table defines the transition function δ. To read a function value, pick a row and column; the cell is the result. Example: the CLEARED row and Digit column gives δ(CLEARED, Digit) = ARG1_INT.

Formal DFSM definition

The calculator’s DFSM is defined by a tuple C = (Q, Σ, δ, q₀, F) (formal definition), where:

  • Q = {CLEARED, ARG1_INT, ARG1_DEC, OP, ARG2_INT, ARG2_DEC, RES}
  • Σ = {Digit, DOT, BinaryOpr, UnaryOpr, =, C, CE}
  • q₀ = CLEARED
  • F = {RES}
  • δ is given by the transition table above

Why memory buttons were omitted

The memory of a Finite State Machine is limited by the number of states it has. Those states describe what you’re doing (e.g., ARG1_INT), not which number you have (e.g., 7 or 3.14).

Memory buttons (M+, M-, MRC) need the calculator to remember an exact number.

That would mean the DFSM should have states like MEM=0, MEM=1, MEM=2, and so on for every possible value, which is impractical.

DFSM memory buttons fragment

DFSM fragment: memory states grow impractically large (source)

To model it cleanly, we would need an extended FSM (EFSM) with variables.

Summary

First, we described the pocket calculator’s behavior in plain language. Then we defined Finite State Machine (FSM) and Deterministic Finite State Machine (DFSM) concepts in terms of the calculator. After that, we drew the state machine diagram with the calculator’s states, inputs, and transitions. We noted that the model is a Deterministic Finite State Machine because each state/input pair leads to one next state.

The state transition table presents all transitions in an elegant and precise way. This lets us write the calculator’s DFSM formally (formal definition). We omitted memory buttons to stick to a Deterministic Finite State Machine.

Note: The idea for this post came from an old academic Java assignment to implement an MVC calculator using a DFSM. I reworked it, simplified the number of states, and kept the model as clear and understandable as possible. This post and the implementation code were written and improved with the help of OpenAI Codex.