Building an interactive Calculator is a great way to combine user input, menu systems, and arithmetic into a single, polished tool. In Batch, the set /a command already supports the basic operators (+, -, *, /, %% for modulo). By wrapping this in an interactive loop, you can create a persistent calculator that accepts repeated inputs.
In this guide, we will demonstrate how to build an interactive calculator with a menu and a direct expression evaluator.
Method 1: Menu-Based Calculator
@echo off
setlocal enabledelayedexpansion
:calc_menu
cls
echo ==============================
echo BATCH CALCULATOR
echo ==============================
echo.
:: Get user input
set /p "a=Enter first number: "
set /p "op=Enter operator (+, -, *, /, %%): "
set /p "b=Enter second number: "
:: Validate operator
if NOT "%op%"=="+" if NOT "%op%"=="-" if NOT "%op%"=="*" if NOT "%op%"=="/" if NOT "%op%"=="%%" (
echo [ERROR] Invalid operator.
pause
goto :calc_menu
)
:: Validate division by zero
if "%op%"=="/" if "%b%"=="0" (
echo [ERROR] Cannot divide by zero!
pause
goto :calc_menu
)
:: Perform calculation
if "%op%"=="+" set /a result=%a% + %b%
if "%op%"=="-" set /a result=%a% - %b%
if "%op%"=="*" set /a result=%a% * %b%
if "%op%"=="/" set /a result=%a% / %b%
if "%op%"=="%%" set /a result=%a% %% %b%
echo.
echo ==========================================
echo %a% %op% %b% = %result%
echo ==========================================
echo.
set /p "again=Continue? (Y/N): "
if /i "%again%"=="Y" goto :calc_menu
exit /bMethod 2: Direct Expression Evaluator
The set /a command can evaluate complete mathematical expressions directly, making a one-line evaluator possible:
@echo off
setlocal enabledelayedexpansion
:eval_loop
cls
echo ==============================
echo EXPRESSION EVALUATOR
echo ==============================
echo Type a math expression (e.g., 5+3*2^)
echo Type EXIT to quit.
echo.
set /p "expr=>> "
if /i "!expr!"=="EXIT" exit /b
:: Evaluate expression
set /a "result=!expr!" 2>nul
if !errorlevel! NEQ 0 (
echo [ERROR] Invalid expression.
) else (
echo = !result!
)
pause
goto :eval_loopWhy Build a Calculator in Batch?
- Quick Math: Performing rapid calculations without opening the Windows Calculator app or Excel.
- Script Integration: Embedding a calculator function inside a larger tool so users can compute values during a setup wizard.
- Educational Tool: Teaching basic programming concepts by building something immediately useful and interactive.
Supported Operators in set /a
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 10 - 4 = 6 |
* |
Multiplication | 6 * 7 = 42 |
/ |
Integer Division | 10 / 3 = 3 |
%% |
Modulo (Remainder) | 10 %% 3 = 1 |
<< |
Left Bit Shift | 1 << 3 = 8 |
>> |
Right Bit Shift | 8 >> 2 = 2 |
& |
Bitwise AND | 5 & 3 = 1 |
^ |
Bitwise XOR (escaped as ^^) |
5 ^^ 3 = 6 |
| |
Bitwise OR (escaped as ^|) |
5 | 3 = 7 |
Important Limitations
Conclusion
Building an interactive calculator demonstrates the full power of Batch's set /a command. Whether you use a structured menu or a free-form expression evaluator, this tool turns the command prompt into a powerful math workstation. The ability to evaluate arithmetic expressions on the fly is an essential utility for system administrators, developers, and power users operating in the Windows terminal environment.