Instructables

How to create calculator in batch

Picture of how to create calculator in batch
images.jpg
sahil.jpg
Before creating a calculator lets go through a simple code tutorial..
<====================================================>
1>(echo) code "echo" are used to write a text for e.g " echo Welcome to Instructables".
2>(color 30)code "color" are used to give color to background and font.
3>(set /p =)code "set /p" is used to create console in program.
4>(title)code "title" is used to give title to program
5>(start)code "start" is used to start an external program.
6>(pause)code "pause" is used to pause a program to avoid, crash.
7>(%username%)code "%username%" just adds the name of the profile name of respective OS.
<=====================================================>
"WIP" (Work-in-progress).

@echo off
title calculator
color 0A
:main
echo.
echo ---------------------------
echo.
echo WELCOME %USERNAME% to
echo.
echo Cal'C Ver 0.2
echo.
echo ---------------------------
echo.
echo /////////////////////////////////////
echo Your previous Calulated number was %sum%
echo ////////////////////////////////////
echo.
echo Enter the specified alphabet to continue..
echo.
echo a)Addition
echo.
echo b)Subtraction
echo.
echo c)Divison
echo.
echo d)Multipication
echo.
echo e)Square, Cube or any power (by Prof.Pickle)
echo.
set /p do=Value.
if %do%== a goto add
if %do%== A goto add
if %do%== b goto sub
if %do%== B goto sub
if %do%== c goto div
if %do%== C goto div
if %do%== d goto mul
if %do%== D goto mul
if %do%== e goto power1
if %do%== E goto power1
echo.
cls
echo Invalid value = %do%
echo.
pause
cls
goto main

:add
cls
echo ADDITON
echo.
set /p no1="num1. "
echo       +
set /p no2="num2. "
set /a sum=no1+no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:sub
cls
echo SUBTACTION
echo.
set /p no1="num1. "
echo       -
set /p no2="num2. "
set /a sum=no1-no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:div
cls
echo DIVISON
echo.
set /p no1="num1. "
echo       /
set /p no2="num2. "
set /a sum=no1/no2
echo ------------
echo %sum%
echo.
pause
cls
goto main


:mul
cls
echo MULTIPLICATION
echo.
set /p no1="num1. "
echo       *
set /p no2="num2. "
set /a sum=no1*no2
echo ------------
echo %sum%
echo.
pause
cls
goto main

:power1
cls
echo Square, Cube or any power (by Prof.Pickle)
echo.
echo Select the number
set /p num=
cls
echo Select the power
set /p pow=
set /a pow=%pow%+1
set ans=%num%
cls
:power2
set /a pow=%pow%-1
if %pow% LSS 1 goto next
set /a ans=%ans%*%num%
goto power2
:next
echo Your answer is %ans%
echo.
echo To open Prof.Pickle's instrutable's profile type 'I'. Type 'M' to goto main
set /p open=
if %open%== I start http://www.instructables.com/member/Prof.+Pickle/
if %open%== i start http://www.instructables.com/member/Prof.+Pickle/
if %open%== m goto main
if %open%== M goto main
goto main
skore11 year ago
Gud work..! Try adding more mathematical functionality like square, cube, average, percentage, etc.. Nice job for a 14 year old.. Keep it up..!
code source (author)  skore11 year ago
Thanks, I will try for adding more mathematical functionality.
In a reply to the suggested functions that skore1 suggested, the square, cube and other powers would be possible, likewise with average. The percentage is possible but involves ingenuity. Square root and the like would be impossible, in my opinion. Other functions would be easy to add in (such as pi).

In case you are wondering why I am making a new comment (opposed to replying), the "Reply" button has disappeared on select comments. No matter, there are always ways around...
code source (author)  Prof. Pickle1 year ago
Thanks..
Still doubt, How we can code for square, cube, etc?
Try this: (I skipped "@echo off" and similar codes"

:power1
echo Select the number
set /p num=
cls
echo Select the power
set /p pow=
set /a pow=%pow%+1
set ans=%num%
cls
:power2
set /a pow=%pow%-1
if %pow% LSS 1 goto next
set /a ans=%ans%*%num%
goto power2
:next
echo Your answer is %ans%
pause

If there are any errors in that then I am sorry, I hadn't really thought about making the code earlier; I just knew it was possible.

Remember that if you use this, mention my name.
code source (author)  Prof. Pickle1 year ago
This code works, I will add to this instructable.
Just one more question, What type of mathematical functionality is this??
This will allow you to square, cube, etc (pretty much any power you want), to any number you want.
code source (author)  Prof. Pickle1 year ago
cool, I will add to Cal'C programme..
I just figured I'd make a shorter code that accomplishes the same thing:

:order
set /p base=What is the base number? 
set /p order=What is the order? 
for /l %%i in (0,1,%order%) do set ans=%ans% + (%base%*%order%)
echo %ans%



I haven't tested the code but it should work (I know I should but I'm tired).
code source (author)  Prof. Pickle1 year ago
The code is not working..
I figured it wouldn't. I was tired at the time and hadn't tested it. I'll just be a moment... (Oh, and keep in mind this is a script snippet, not an actual program. I left out basic batch file codes like "@echo off", "title", "color" and most importantly the pause command at the end.)

Here we go, a working script:

@echo off
set ans=0
set /p base=What is the base number?
set /p order=What is the order?
set /a add=%base%*%base%
set /a ans+=add
set /a order-=2
for /l %%i in (1,1,%order%) do set /a ans*=base
echo %ans%
pause



Note: There is a bug with this. The maximum value the order can be for the base of 2 is 30. With the base of 3 it is 29, with 4 it is 28, etc...

I don't know why this happens, but atleast it works.
code source (author)  Prof. Pickle1 year ago
Awesome! I will add to new version of Cal'c...
Its simple and also effective..
I tried to make it as simple as possible but I know it can be done shorter. Oh well, it'll do for now.
Excellent tutorial; I am sorry I didn't comment on it sooner. As always, let's point out the flaws before rewarding your skill:
Firstly, the version type should always be displayed with an uppercase "V". Also, stating that this is version 0.1 is suggesting that this is still in BETA form. Either change or state somewhere in this instructable that it is a "WIP" (Work-in-progress).
Secondly, on the fifteenth line of your code, there is a grammatical error: The abbreviation "No." is displayed in your code as "no". Please fix this up before someone notices it that doesn't understand.
Thirdly, in your explanation of the code "start", I have spotted a grammatical error. I suggest remodeling it in this fashion: "5>(start)code "start" is used to start an external program.
In fact, you might want to revise most of your explanations as almost all of them contain errors.

Now that my rant is done, shall we get to the pleasantries?

Your calculator is different then others, but the idea is still unoriginal. It is laid out well and easy to understand, and, as always, your tutorial at the start is much appreciated. Just as skore1 said: "Nice job for a 14 year old...".
I know that was probably just constructive criticism, but... harsh, much?
I hate to sound all stuck up myself, but for the sake of simplicity, here's a metaphor - The parents of bird's push out their younglings in order for them to fly and experience the world. I agree, this is harsh in ways, but can you also see the good in it?
code source (author)  Prof. Pickle1 year ago
Thanks....
I have edited the mistakes...
(removed by author or community request)
code source (author)  Prof. Pickle1 year ago
ok..
skore21 year ago
great tutorial..