# Build a Batch Calculator + Advanced Calculator V0.8

3,951

9

4

## Introduction: Build a Batch Calculator + Advanced Calculator V0.8

Hello guys.
When I began learnig batch, one of the first things I did was buid a small calculator, and thoguh it was very simple, it was a greaet start to learning how to utilise variables.

So I decided to make this instructable that will show you how to:
- Use some basic batch commands
- how to build a simple caclulator

I've also included a small project of mine, which is currently at v0.8.
I might take it to v1.0, and if I do so i'll update it to this instructable.

NB : The Calculator file at the end of every step is my advanced one

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Basic Batch

OK, lets start with some commands I'll guarantee you'll use when making a user interactive batch file.
Read this if your new to batch, and even if your not skim it for some usefull things you may not know.

- @echo off
> Suppresses the console from displaying what commands you type into the batch file and only displays the output data

- Echo [text]
> Displays the text on the console
____________________________________________________

- Set "var=val"
> Creates a variable named "var" with the value of "val"

- Set /p "var=Prompt"
> Prompts the user with "Prompt" and what ever they input becomes te value of "var"

- Set /a "var=Equation"
> Solves "Equation" as a maths equation and sets the value to "var"

- %var%
> Refers to the variable amed "var"

- %var:~[Refer to values within "var"]%
> To learn how to use the above type "set /?" in cmd.
____________________________________________________

- if "%var%" equ "val" [command]
- :tag
> Creates a tag called "tag" which can be later referenced
- goto tag
> Goes to the tag labelled "tag"
____________________________________________________

- Color [hex][hex]

- Rem
> A line starting with Rem will be completly ignored. It's used to organsise the file or help someone else understand it.

- Title Program Name
> Sets the console's title to "Program Name"

- Pause
> Displays "Press any key to continue . . ." and continues upon user input

- Exit
> Immeadiately exits the batch file

And thats your crash course to batch!

Of coourse, type help to learn more. Lastly this is nothing compared to some of the operatable programs built in to windows. And nothing when compared to building you own operable programs!

But this is just your first little step...

### Attachments

Ok, heres the step to build your calculator. I'm not going to explain it, so when you realises how it works (from the last step) you'll be extra proud of yourself, and you'll have a better understanding (Cuz you actually thought about it!).

@echo off
Title My Calculator!
color 0a

:loop
set /p "input=Calculate: "
Echo.
Echo.
goto loop

Rem This Line is a remark and will be ignored. So will the below line. Its just good practice to End all Batch Files like this
Exit /b 0

And thats it
NB: Echo. Leaves a blank line

Then Ctrl+S to save and save as "Calculator.bat" with the option "allfiles" selected (note ".txt - Text Document") Some versions of Windows will not except the latter.

Finally find and run it and have FUN!!! Modify it to make it Mind blowing!

## Step 3: Try My Calculator

Here's my calculator, in Version 0.8:

If I do update it I'll put ip up here again.
If you want me to make more steps (or a new instructable) Explaining how to build one like mine, Comment.

Note, when using it type "?" so you know how to use it.

Don't dedicate your life to finding cases where it breaks. Dedicate your life to:

conveying potential aspects of belief or opinon which may or may not have didactic influence over others who amy ever wish to accept so of the great potential and power of a simple automated series of tasks which take user input and resolves it to the humans standard of maths, with the awkward fact that the functions require the operator to be placed in fromt of the operand???

I'll go neck my self about. . . . . . . . . . . . Pause . . . . . . . . . . . . . Now!

34 3.3K
22 2.7K
153 9.2K

## 4 Discussions

Hi Monacraft,

I made a simple calculator that cannot calculate exponents, etc.

Can you put all the commands together so that I would have an advanced calculator? So that I would be able to calculate exponents, Stuff like 3^4= ... and and so on...

I am not that good in batch scripting and I don't know how to add those stuff :(

Here is my code:

@echo off

:start

set /p MATH=Equation?

set /a RESULT=%MATH%

echo %RESULT%

pause

cls

goto start

Hi Monacraft,

I made a simple calculator that cannot calculate exponents, etc.

Can you put all the commands together so that I would have an advanced calculator? So that I would be able to calculate exponents, Stuff like 3^4= ... and and so on...

I am not that good in batch scripting and I don't know how to add those stuff :(

Here is my code:

@echo off

:start

set /p MATH=Equation?

set /a RESULT=%MATH%

echo %RESULT%

pause

cls

goto start

Hello Governor,

Exponents aren't too hard in batch, all they require is a simple for-loop. Here is a sample code which returns x^a or in this case 2^4=16.

================================

set /a x=2

set /a a=4

for /l %%a in (2, 1, %a%) do (

set /a x*=%x%

)

Echo %x%

pause

================================

If you run that, it should print out 16. Hopefully that shouldn't be too hard to incorporate into your program. Sorry for taking so long to respond to your question (I've been having internet problems :/ ). If you have anyother questions, comment or email me and I should reply in about a week :D.