Monday, March 21, 2005

MS-DOS Calculator

Have you ever needed to perform a quick calculation at the command prompt, well your prays have been answered. Below is the source code for an MS-DOS calculator that I created. It utilizes the MS-DOS command prompt's ability to perform basic math functions.

To create the CALC.BAT batch file, follow the instructions below:
- Open the Notepad, by typing "NOTEPAD.EXE" and press the Enter key in the Run... command.
- Select the text below and Copy it (Ctrl-C) from your browser window, then switch to the Notepad application and paste (Ctrl-V) the text into it.
- Save the file (Ctrl-S) as 'C:\WINDOWS\CALC.BAT'. (This allows you to run the command from anywhere).
- Then close the Notepad application.



@echo off
:: ----------------------------
:: Name: MS-DOS Calculator
:: Description: Perform quick calculations from the MS-DOS prompt.
:: Author: Jason Savitt
:: Version: 1.01
:: Web Site: http://windowstipoftheday.blogspot.com/ also visit
:: http://ubergeek316.blogspot.com/
:: Notes: - Requires Windows XP, 2003 or higher, although it may run
:: on Windows NT 4.0 and 2000 (not tested).
:: - Place the file in the %WinDir% (i.e.: C:\Windows)
:: to make it available from any where on your system.
:: ----------------------------

rem Creates the equation from the passed parameter from the command line.
if "%1" == "" goto DispHelp
set Equation=%1
:BuildEqu
shift
if "%1" == "" goto DispEqu
set Equation=%Equation%%1
goto BuildEqu
rem If the equation is empty, then it displays help

:DispEqu
echo.
rem Performs the equation and displays the results
set /a Results=%Equation%
echo %Equation% = %Results%
goto ExitProg

:DispHelp
rem Display help screen
echo.
echo The command line calculator supports the following operators, in
echo decreasing order of precedence:
echo.
echo ^(^) - grouping
echo ^! ^~ ^- - unary operators
echo ^* ^/ %% - arithmetic operators
echo ^+ ^- - arithmetic operators
echo , - expression separator
echo.
echo For example:
echo - calc 5+5
echo - calc (5-5)*10
echo - calc 20/5
goto ExitProg

:ExitProg
exit /b


To use the calculator, open the MS-DOS prompt then type "calc 5 * 5" (or whatever else you want) and press the Enter key. To get calculator Help, just type "calc" and press key Enter key.


The command line calculator supports the following operators, in
decreasing order of precedence:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
, - expression separator

For example:
- calc 5 + 5
- calc (5 - 5) * 10
- calc 20/5


Note: This batch has only been tested on Windows XP, although it should work on Windows NT4 or 2000. It will not work on Windows 95/98/ME, the MS-DOS prompt doesn't support the necessary functions need in order for it to work.

7 comments:

somebot said...

MS-DOS prompt doesn't support the necessary functions need in order for MS-DOS Calculator to work.

UberGeek316 said...

What version of Windows are you running?

Mikkel GP said...

don't you think its quite a lot of code/work just to exchange "set /a 5*5" with "calc 5*5"?

easy recipes said...

talking about codes... code workss..
i love teaching code works.. i miss the stuff... huhuhu

eric aldinger said...

SET /A does not seem to support arithmetic operations on decimals.

E.G. C:\>set /A 88.64+2497.63
Missing operator.

Anonymous said...

I was looking for a command from DOS 6 that would work and I also had previously seen the "set /a" functionality in cmd.exe using "help set" on Windows XP. I have seen some kind of calculating routine in DOS previous to Windows NT but I forgot where it is. Off to microsoft.com for the answer. As for decimals, the period character separates some expressions similar to "bash" in linux but I think additional processing is needed than just using "set /A" for those expressions, like using a different command to process numbers and then using multiple set commands to perform arithmetic. I am not absolutely sure of about one quarter of the content in this post. The robot key in this page was postulated by me, programmed by others. Sorry, my fault. But now it really annoys me that some robots can read the data better than we can see the letters. The whole security mediaplex has me thinking about eliminating all pronouns from all my files and communications.

Anonymous said...

Hello
Need little supplement, so will work...
Copy the finished calc.bat file into Windows\system32 directory (win. order bat com exe )so

calc - (MsDos)calc prg
calc.exe - (Win)Calc prg
(Oh, old Commodore-time, when promt was calculating)