How to Implement a Simple Calculator (Expression Evaluator) in Batch Script
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 /b
The script checks the user's operator input against a list of allowed operators using a for loop. The loop variable %%O iterates through + - * / %%, and if the user's input matches any of these, the valid flag is set to 1. This prevents the script from attempting to evaluate expressions with invalid operators like = or ^.
% CharacterIn Batch, % is a special character used for variable expansion. To include a literal % in a for loop list, you must escape it by writing %%. That is why the operator list appears as (+, -, *, /, %%): the first % escapes the second, resulting in a single % character for the modulo operator.
Method 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_loop
The expression evaluator supports parentheses and other operators that set /a supports, but the user must be careful with Batch's special characters. For example, ^ (bitwise XOR) and | (bitwise OR) are also command-line redirection operators and must be escaped as ^^ and ^| when typed directly. Alternatively, the script could pre‑process the input to escape these characters automatically.
Why 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 |
set /a follows standard mathematical precedence: parentheses first, then multiplication/division/modulo, then addition/subtraction, then bitwise shifts, then bitwise AND, XOR, and OR. Use parentheses to force a specific evaluation order when needed.
Important Limitations
All calculations are performed with integer arithmetic. The expression 10 / 3 yields 3, not 3.33. Any fractional part is truncated (not rounded).
Results cannot exceed 2,147,483,647 (positive) or go below -2,147,483,648 (negative). Attempting to calculate beyond these bounds causes silent overflow.
The expression evaluator can handle spaces in the input (e.g., 5 + 3 * 2). The set /a command automatically ignores spaces around operators.
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.