Skip to main content

Overview

The CalculatorManager class is the primary interface for interacting with the Windows Calculator calculation engine. It manages multiple calculator modes (Standard, Scientific, Programmer), memory operations, history, and display callbacks.

Constructor

displayCallback
ICalcDisplay*
required
Callback interface for display updates and notifications
resourceProvider
IResourceProvider*
required
Provider for localized strings and resources
CalculatorManager(
    _In_ ICalcDisplay* displayCallback,
    _In_ IResourceProvider* resourceProvider
);

Example

#include "CalculatorManager.h"

using namespace CalculationManager;

class MyDisplay : public ICalcDisplay {
    // Implement ICalcDisplay interface methods
};

MyDisplay* display = new MyDisplay();
IResourceProvider* resources = GetResourceProvider();

CalculatorManager* manager = new CalculatorManager(display, resources);
manager->SetStandardMode();

Calculator Modes

SetStandardMode()

Switches to Standard calculator mode with basic arithmetic operations.
void SetStandardMode();
Details:
  • Sets precision to 16 digits (StandardModePrecision)
  • Disables order of operations precedence
  • Resets to decimal radix
  • Uses standard history

SetScientificMode()

Switches to Scientific calculator mode with advanced mathematical functions.
void SetScientificMode();
Details:
  • Sets precision to 32 digits (ScientificModePrecision)
  • Enables order of operations precedence
  • Supports trigonometric, logarithmic, and exponential functions
  • Uses scientific history

SetProgrammerMode()

Switches to Programmer calculator mode with bitwise operations and multiple bases.
void SetProgrammerMode();
Details:
  • Sets precision to 64 bits (ProgrammerModePrecision)
  • Enables integer-only mode
  • Supports Hex, Decimal, Octal, and Binary radix
  • Disables history

Command Processing

SendCommand()

Sends a command to the calculator engine for processing.
command
Command
required
The command to execute (arithmetic operation, function, mode change, etc.)
void SendCommand(_In_ Command command);

Example

manager->SendCommand(Command::Command5);
manager->SendCommand(Command::CommandADD);
manager->SendCommand(Command::Command3);
manager->SendCommand(Command::CommandEQU);  // Result: 8

Memory Operations

MemorizeNumber()

Stores the current displayed value in memory.
void MemorizeNumber();
Behavior:
  • Inserts value at the beginning of the memory list
  • Maximum memory size is 100 items
  • Ignored if calculator is in error state

MemorizedNumberLoad()

Recalls a memorized number to the primary display.
indexOfMemory
unsigned int
required
Zero-based index of the memory item to recall
void MemorizedNumberLoad(_In_ unsigned int indexOfMemory);

MemorizedNumberAdd()

Adds the current display value to a memorized number.
indexOfMemory
unsigned int
required
Zero-based index of the memory item to modify
void MemorizedNumberAdd(_In_ unsigned int indexOfMemory);
Behavior:
  • If memory is empty, creates new memory item with current value
  • Notifies display callback of memory change

MemorizedNumberSubtract()

Subtracts the current display value from a memorized number.
indexOfMemory
unsigned int
required
Zero-based index of the memory item to modify
void MemorizedNumberSubtract(_In_ unsigned int indexOfMemory);

MemorizedNumberClear()

Removes a specific memory item.
indexOfMemory
unsigned int
required
Zero-based index of the memory item to remove
void MemorizedNumberClear(_In_ unsigned int indexOfMemory);

MemorizedNumberClearAll()

Clears all memorized numbers.
void MemorizedNumberClearAll();

History Management

GetHistoryItems()

Retrieves history items for the current mode.
return
std::vector<std::shared_ptr<HISTORYITEM>> const&
Reference to vector of history items
std::vector<std::shared_ptr<HISTORYITEM>> const& GetHistoryItems() const;

GetHistoryItems(mode)

Retrieves history items for a specific calculator mode.
mode
CalculatorMode
required
The calculator mode (Standard or Scientific)
std::vector<std::shared_ptr<HISTORYITEM>> const& GetHistoryItems(
    _In_ CalculatorMode mode
) const;

SetHistoryItems()

Restores history items (used for persistence).
historyItems
std::vector<std::shared_ptr<HISTORYITEM>> const&
required
Vector of history items to restore
void SetHistoryItems(
    _In_ std::vector<std::shared_ptr<HISTORYITEM>> const& historyItems
);

GetHistoryItem()

Retrieves a specific history item by index.
uIdx
unsigned int
required
Zero-based index of the history item
std::shared_ptr<HISTORYITEM> const& GetHistoryItem(_In_ unsigned int uIdx);

RemoveHistoryItem()

Removes a history item by index.
uIdx
unsigned int
required
Zero-based index of the history item to remove
return
bool
True if item was removed, false otherwise
bool RemoveHistoryItem(_In_ unsigned int uIdx);

ClearHistory()

Clears all history for the current mode.
void ClearHistory();

MaxHistorySize()

Returns the maximum number of history items (20).
size_t MaxHistorySize() const;

Utility Methods

Reset()

Resets the calculator to initial state.
clearMemory
bool
default:"true"
Whether to clear memorized numbers
void Reset(bool clearMemory = true);
Behavior:
  • Switches to Standard mode
  • Sets degree mode (if in Scientific)
  • Clears all entries
  • Disables exponential format
  • Optionally clears memory

IsEngineRecording()

Checks if the engine is recording input (building an expression).
bool IsEngineRecording();

IsInputEmpty()

Checks if the current input is empty.
bool IsInputEmpty();

SetRadix()

Sets the number base for Programmer mode.
iRadixType
RadixType
required
Radix type: Hex, Decimal, Octal, or Binary
void SetRadix(RadixType iRadixType);

GetResultForRadix()

Gets the current result formatted in a specific radix.
radix
uint32_t
required
The radix (2, 8, 10, or 16)
precision
int32_t
required
Number of digits of precision
groupDigitsPerRadix
bool
required
Whether to group digits with separators
std::wstring GetResultForRadix(
    uint32_t radix,
    int32_t precision,
    bool groupDigitsPerRadix
);

SetPrecision()

Changes the calculation precision.
precision
int32_t
required
Number of digits of precision
void SetPrecision(int32_t precision);

DecimalSeparator()

Gets the decimal separator character for the current locale.
wchar_t DecimalSeparator();

GetCurrentDegreeMode()

Returns the current angle mode for trigonometric functions.
return
Command
CommandDEG, CommandRAD, or CommandGRAD
Command GetCurrentDegreeMode();

GetDisplayCommandsSnapshot()

Gets a snapshot of the current expression commands for display.
std::vector<std::shared_ptr<IExpressionCommand>> GetDisplayCommandsSnapshot() const;

Enumerations

CalculatorMode

Defines the calculator modes.
enum class CalculatorMode {
    Standard = 0,
    Scientific
};

MemoryCommand

Commands for memory operations.
enum class MemoryCommand {
    MemorizeNumber = 330,
    MemorizedNumberLoad = 331,
    MemorizedNumberAdd = 332,
    MemorizedNumberSubtract = 333,
    MemorizedNumberClearAll = 334,
    MemorizedNumberClear = 335
};

CalculatorPrecision

Default precision values for each mode.
enum class CalculatorPrecision {
    StandardModePrecision = 16,
    ScientificModePrecision = 32,
    ProgrammerModePrecision = 64
};

ICalcDisplay Interface

Callbacks that must be implemented by the display:
class ICalcDisplay {
public:
    virtual void SetPrimaryDisplay(
        _In_ const std::wstring& displayString,
        _In_ bool isError
    ) = 0;

    virtual void SetIsInError(bool isError) = 0;

    virtual void SetExpressionDisplay(
        _Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
        _Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands
    ) = 0;

    virtual void SetMemorizedNumbers(
        _In_ const std::vector<std::wstring>& memorizedNumbers
    ) = 0;

    virtual void OnHistoryItemAdded(_In_ unsigned int addedItemIndex) = 0;
    virtual void SetParenthesisNumber(_In_ unsigned int parenthesisCount) = 0;
    virtual void OnNoRightParenAdded() = 0;
    virtual void MaxDigitsReached() = 0;
    virtual void BinaryOperatorReceived() = 0;
    virtual void MemoryItemChanged(unsigned int indexOfMemory) = 0;
    virtual void InputChanged() = 0;
};

See Also

Build docs developers (and LLMs) love