This is a simple calculator:
#ECHO OFF
TITLE Simple Calculator
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
if %selection%== "1" goto add
if %selection%== "2" goto sub
if %selection%== "3" goto multiply
if %selection%== "4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
ECHO.
SET /A Ansadd=%Numadd1%+%Numadd2%
ECHO The result is: %Ansadd%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
ECHO.
SET /A Ansub=%Numsub1%-%Numsub2%
ECHO The result is: %Ansub%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
ECHO.
SET /A Ans=%Nummul1%+%Nummul2%
ECHO The result is: %Ansmul%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
ECHO.
SET /A Ansdiv=%Numdiv1%+%Numdiv2%
ECHO The result is: %Ansdiv%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
May I know what is the problem with this and the correct code?
There were too many issues to mention in the comment section so please take a look at this example against yours to see if you can identify them:
#ECHO OFF
TITLE Simple Calculator
SET /P "Selection=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF "%Selection%"=="+" GOTO Add
IF "%Selection%"=="-" GOTO Subtract
IF "%Selection%"=="*" GOTO Multiply
IF "%Selection%"=="/" GOTO Divide
GOTO :EOF
:Add
SET /P "Numadd1=Enter an addend: "
SET /P "Numadd2=Enter another addend: "
SET /A Ansadd=Numadd1+Numadd2
ECHO.
ECHO The sum is: %Ansadd%
ECHO.
PAUSE
GOTO :EOF
:Subtract
SET /P "Numsub1=Enter the minuend: "
SET /P "Numsub2=Enter the subtrahend: "
SET /A Ansub=Numsub1-Numsub2
ECHO.
ECHO The difference is: %Ansub%
ECHO.
PAUSE
GOTO :EOF
:Multiply
SET /P "Nummul1=Enter a factor:"
SET /P "Nummul2=Enter another factor: "
SET /A Ansmul=Nummul1*Nummul2
ECHO.
ECHO The product is: %Ansmul%
ECHO.
PAUSE
GOTO :EOF
:Divide
SET /P "Numdiv1=Enter the dividend: "
SET /P "Numdiv2=Enter the divisor: "
SET /A Ansdiv=Numdiv1/Numdiv2
ECHO.
ECHO The quotient is: %Ansdiv%
ECHO.
PAUSE
Ideally you should make sure that none of your variables have existing values at the start of the script and check that the entered values are compatible/validated before using them too!
Edit
You could probably make the code shorter too:
#ECHO OFF
TITLE Simple Calculator
FOR %%A IN (Operator Integer1 Integer2 Result) DO SET "%%A="
SET /P "Operator=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF DEFINED Operator (SET "Operator=%Operator:~,1%") ELSE GOTO :EOF
ECHO(%Operator%|FINDSTR /L "+ - * /">NUL||GOTO :EOF
SET /P "Integer1=Enter your starting integer: "
SET /P "Integer2=Now the other integer: "
SET /A Result=Integer1 %Operator% Integer2
ECHO(
ECHO The Answer is: %Result%
ECHO(
PAUSE
These two lines
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
should be:
set /p selection=if you want to add press 1,subtract 2,multiply 3 and divide 4:
There are a few errors in your code, including the math. Try and take a look at my code and compare it with yours.
#ECHO OFF
TITLE Simple Calculator
echo If you want to add press 1, subtract 2, multiply 3 and divide 4
set /p selection=
if "%selection%"=="1" goto add
if "%selection%"=="2" goto sub
if "%selection%"=="3" goto multiply
if "%selection%"=="4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
SET /A Ans=%Numadd1%+%Numadd2%
goto end
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
SET /A Ans=%Numsub1%-%Numsub2%
goto end
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
SET /A Ans=%Nummul1%*%Nummul2%
goto end
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
SET /A Ans=%Numdiv1%/%Numdiv2%
:end
ECHO.
ECHO The result is: %Ans%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
Related
Im trying to create a batch that will calculate a certain equation with Input variables.
equation is
(%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c%
im currently using code i found here but it doesn't seem to work properly
SETLOCAL EnableExtensions EnableDelayedExpansion
#echo off
echo Enter BaseCC
set /P b=
echo Enter Riven ( if none enter 1 )
set /P r=
echo Enter Combo Multiplier
set /P c=
call :calc_ 2 (%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c%
echo !calc_v!
pause
goto :EOF
:calc_
set scale_=1
set calc_v=
for /l %%i in (1,1,%1) do set /a scale_*=10
set /a "calc_v=!scale_!*%2"
set /a calc_v1=!calc_v!/!scale_!
set /a calc_v2=!calc_v!-!calc_v1!*!scale_!
set calc_v=!calc_v1!.!calc_v2!
goto :EOF
if b is 20; r is 114; c is 3
result should be 280.2
The result i get is 180.0
#ECHO OFF
SETLOCAL
rem equation is (%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c%
echo Enter BaseCC
set /P b=
echo Enter Riven ( if none enter 1 )
set /P r=
echo Enter Combo Multiplier
set /P c=
:: Calculate (%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c%
:: write this in FP terms : (b+0.88b+1.65b+(br/100))*c
:: Resolve to (3.53b+(br/100))*c = (353b+br)*c/100
:: = (353+r)*b*c /100
SET /a result_by_100=(353+r)*b*c
ECHO result=%result_by_100:~0,-2%.%result_by_100:~-2%
GOTO :EOF
The problem is that the term (%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c% is resolved using integer-maths, so each term which implements a division is resolved to int(term). The result is `(%b%+(%b%*INT(88/100))+(%b%*INT(165/100))+(%b%INT(%r%/100)))%c%
The simple solution is to resolve the formula as shown.
Found the solution thanks to the suggestions of Stephan.
it works correctly now.
Didn't know i can implement vbs.
#echo off
set /p b=BaseCC=
set /p r=Riven=
set /p c=Combo=
echo WSH.Echo Eval(Wscript.Arguments(0))>>Q.vbs
cscript //nologo Q.vbs ((%b%+(%b%*(88/100))+(%b%*(165/100))+(%b%*(%r%/100)))*%c%)
Del "Q.vbs"
Pause>nul
I have a project for square root calculation to get the larger side of in a right triangle using Pythagorean theorem.
Here's what I've tried :
#echo off
set /p a=Pleas Enter Value(1) =
echo.
set /p b=Pleas Enter Value(2) =
set /a c=%a%*%a%
set /a d=%b%*%b%
set /a F=%d%+%c%
echo.
:calculation Value(3)
rem root Number
√%F%
echo.
pause>nul
Batch
This is the native batch method(not accurate):
set divider=0
:loop
set /a divider=divider+1
set /a sqrt=F / divider
if %divider% equ %sqrt% goto break
if %divider% gtr %sqrt% goto break
goto loop
:break
echo %sqrt%
It will floor the number if the square root is floating point number.
Again, will not support floating point arithmetic, but this code works:
#echo off
Title SquareRoot
:StartSquareRoot
cls
echo Number:
set /p number=
call :SquareRoot %number%
echo Square: %number%
echo Root: %answer%
pause
goto StartSquareRoot
:SquareRoot
set root=1
set /a sqr=%root%*%root%
:Loop
if %sqr% LSS %number% (
set /a root=root+1
set /a sqr=root*root
goto Loop
)
(EndLocal && set answer=%root% && exit /B)
I know I can write an echo > C:/Folder/name.txt
But I have a generator to make 2 number for Cord and can't figure out a quick way to make a one line echo > with all the points
for shortness I'll simplify my code because the current code is not the issue
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else(
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
if %Number% GEQ %Max% (goto :fin) Else(
set /a Number=%Number%+1 & set R=2 & Goto :loop)
This Is what Im trying to Optimize
:fin
echo (%PX1%,%PY1%),(%PX2%,%PY2%),(%PX3%,%PY3%) ... Ect >C:Folder/File.txt
Is there any way to make all the generated numbers on 1 line
Credit to Squashman for the Answer - Thank you!
Correct Code:
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
set Line=Test
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else (
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
set line=%line%(!PX%number%!,!PY%number%!),
if %Number% GEQ %Max% (goto :fin) Else (
set /a Number=%Number%+1 & set R=2 & Goto :loop)
:fin
echo %Line:~0,-1% >C:Folder/File.txt
Credit to Squashman for the Answer - Thank you!
The logic is so much simpler if you get the max point count before the loop. And the FOR /L loop can auto increment your counter and eliminate the need for GOTO.
#echo off
setlocal enableDelayedExpansion
set /p "cnt=How many points? "
set "ln="
for /l %%N in (1 1 %cnt%) do (
echo(
set /p "PX%%N=What is PointX%%N? "
set /p "PY%%N=What is PointY%%N? "
set "ln=!ln!(!PX%%N!,!PY%%N!),"
)
echo(
echo !ln:~0,-1!
I am making a small script that would assign roles as an app sort of thing. It asks for the number of people, then it runs random to see what role you are.
Like this:
:mrole
set/a player1=%random% * 4 / 32768 + 1
if %player1%==1 set 1role=Murderer
But what if I only want a certain ratio of people to get a certain role? Say 1:4 are murderers. If I do something like this for up to 8 players what if 6 of them are murderers? What if I want to run through again for a Detective role if there are 8 people? The current one I have for mrole is 29 lines! Is there a quicker way than checking if there are too many EVERY time? And the Detective check is going to be very long too!
What I have so far is below and here's a link: http://pastebin.com/g2GfN7v9
#echo off
:new
set test=2
:main
cls
echo Trouble in Murder Town
echo --------------------------
echo.
echo 1. Start
echo 2. Tester Options
echo 3. Quit
echo.
set/p m=
if %m%==1 goto start
if %m%==2 goto testrat
if %m%==3 exit
goto main
:testrat
cls
echo Chance to have a tester
echo -----------------------
echo Currently a 1 in %test% chance to have a tester in a game
echo.
echo 1. Always
echo 2. Never
echo 3. 1/2 (default)
echo 4. 1/3
echo 5. 1/4
echo 6. 1/5
echo 7. Back
echo.
set/p t=
if %t%==1 set test=1
if %t%==2 set test=0
if %t%==3 set test=2
if %t%==4 set test=3
if %t%==5 set test=4
if %t%==6 set test=5
if %t%==7 goto options
goto testrat
:start
cls
echo How many players not including the one at the computer?
echo.
echo 1. 3
echo 2. 4
echo 3. 5
echo 4. 6
echo 5. 7
echo 6. 8
echo 7. Quit
echo.
set/p t=
if %t%==1 set players=3
if %t%==2 set players=4
if %t%==3 set players=5
if %t%==4 set players=6
if %t%==5 set players=7
if %t%==6 set players=8
if %t%==1 goto names
if %t%==2 goto names
if %t%==3 goto names
if %t%==4 goto names
if %t%==5 goto names
if %t%==6 goto names
if %t%==7 goto main
goto start
:names
cls
echo Please enter their names.
echo.
set/p player1=Player 1:
echo.
set/p player2=Player 2:
echo.
set/p player3=Player 3:
echo.
if %players%==8 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
echo.
set/p player7=Player 7:
echo.
set/p player8=Player 8:
goto role
) else if %players%==7 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
echo.
set/p player7=Player 7:
goto role
) else if %players%==6 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
goto role
) else if %players%==5 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
goto role
) else if %players%==4 (
set/p player4=Player 4:
goto role
) else goto role
:role
set 1role=0
set 2role=0
set 3role=0
set 4role=0
set 5role=0
set 6role=0
set 7role=0
set 8role=0
set murderers=0
set detectives=0
goto mrole
:mrole
set/a player1=%random% * 4 / 32768 + 1
if %player1%==1 set 1role=Murderer
if %player1%==1 set/a murderers=%murderers% + 1
if %player1%==1 set mcheck=y
if %player1% neq 1 set mcheck=n
if %mcheck%==y if %players% lss 8 goto drole
set/a player2=%random% * 4 / 32768 + 1
if %murderers%==1 if %players%==8 if %player2%==1 set 2role=Murderer
if %mcheck%==y if %players% lss 8 goto drole
set/a player3=%random% * 4 / 32768 + 1
if %murderers%==1 if %players%==8 if %player3%==1 set 3role=Murderer
if %mcheck%==y if %players% lss 8 goto drole
set/a player4=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player4%==1 set 4role=Murderer
set/a player5=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player5%==1 set 5role=Murderer
set/a player6=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player6%==1 set 6role=Murderer
set/a player7=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player7%==1 set 7role=Murderer
set/a player8=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player8%==1 set 8role=Murderer
if %murderers% lss 1 goto mrole
if %players%==8 if %murderers% lss 2 goto mrole
:drole
if %players%==8 (
) else if %players lss 8 goto irole
:irole
if 1role==0 set 1role=Innocent
if 2role==0 set 2role=Innocent
if 3role==0 set 3role=Innocent
if 4role==0 set 4role=Innocent
if 5role==0 set 5role=Innocent
if 6role==0 set 6role=Innocent
if 7role==0 set 7role=Innocent
if 8role==0 set 8role=Innocent
The algorithm you're looking for is a shuffle. Think of your roles as a deck
of cards, with one role per card. If you were playing a game with people, you'd
shuffle the deck of roles and hand one card to each person.
Study the following code that declares a deck of 4 cards and then shuffles them.
I've named the deck in the style of an array from more traditional programming
languages, but it's really just a convention. I could have named it in the
form of DECK.1, DECK.2, ... or whatever.
I've also isolated parts of the program into subroutines. There's a subroutine
to generate a random number, print an "array", shuffle an array, and swap 2
items in an array. When working with array-style indirection in batch, you'll
hit a wall if you don't break things into subroutines.
#echo off
setlocal EnableDelayedExpansion
set "DECK[0]=1H"
set "DECK[1]=2S"
set "DECK[2]=3C"
set "DECK[3]=4D"
set "DECK_SIZE=4"
call :print_array "DECK" %DECK_SIZE%
call :shuffle "DECK" %DECK_SIZE%
echo -----
call :print_array "DECK" %DECK_SIZE%
goto :eof
REM Generate random number from %1 min through %2 max.
:rand
set /a RAND_NUM=%RANDOM% * (%~2 - %~1 + 1) / 32768 + %~1
goto :eof
REM %1 Name of array to print
REM %2 Size of the array
:print_array
set /a LAST_INDEX=%~2-1
for /L %%a in (0, 1, %LAST_INDEX%) do (
echo %~1[%%a]: !%~1[%%a]!
)
goto :eof
rem To shuffle an array a of n elements (indices 0..n-1):
rem for i from n − 1 downto 1 do
rem j ← random integer with 0 ≤ j ≤ i
rem exchange a[j] and a[i]
REM %1 Name of array to shuffle
REM %2 Size of the array
:shuffle
set /a LAST_INDEX=%~2-1
for /L %%a in (0, 1, %LAST_INDEX%) do (
call :rand %%a %LAST_INDEX%
call :swap "%~1[%%a]" "%~1[!RAND_NUM!]"
)
goto :eof
REM %1 One of the variables to swap.
REM %2 The other variable to swap.
:swap
set "__tmp=!%~1!"
set "%~1=!%~2!"
set "%~2=!__tmp!"
goto :eof
So to modify this code for your program, you'd want an array of players and an
array of roles. The player array remains fixed, and you shuffle the role array.
E.g.,
set "PLAYER[0]=Howard"
set "PLAYER[1]=Fred"
set "PLAYER[2]=Robin"
set "PLAYER[3]=Gary"
set PLAYER_SIZE=4
set "ROLE[0]=murderer"
set "ROLE[1]=innocent"
set "ROLE[2]=innocent"
set "ROLE[3]=detective"
set "ROLE_SIZE=4"
call :shuffle "ROLE" %ROLE_SIZE%
set /a ROLE_LAST_INDEX=%ROLE_SIZE%-1
for /L %%a in (0, 1, %ROLE_LAST_INDEX%) do (
echo Player !PLAYER[%%a]! is !ROLE[%%a]!.
)
One sample output from this code would give you:
Player Howard is detective.
Player Fred is innocent.
Player Robin is murderer.
Player Gary is innocent.
#ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET "roles=1234"
SET players=4
:setplayrole
SET /a player=1+(%RANDOM% %% %players%)
IF DEFINED $%player% GOTO setplayrole
SET $%player%=%roles:~-1%&SET "roles=%roles:~0,-1%"
IF DEFINED roles GOTO setplayrole
SET $
GOTO :EOF
Here's a snippet that might achieve your aim.
But first, a little commercial - you have tis line in your posting:
if %player1%==1 set 1role=Murderer
Um - it's possible to do this, but a really, really bad idea because of batch's syntax. Never start a variablename with a numeric. It has huge consequences, and none of the are good.
--- back to our - er, program...
I'm not clear whether your difficulty is assigning the ratios of roles to the players, but it seems that you want a proportion in each role; guaranteed there are no 'no-one playing this role'.
To me, what you'd do is assign a set of roles - I've just use 1..4 with an even distribution. You may want to have 10 players, one each in roles 1..4 and the others random, where you could build the roles string by appending the random roles to a seed of required roles.
Having established the set of roles for the game, you then assign each role in the string to a player, and remove the role as it is assigned.
I use a standard clear out all variables with names starting "$"' routine; then assign the required roles, (append any extras as you wish) then the loop is simple - select a random player; if that player already has an assigned role, select again. If not, simply assign the last role in the list and remove the last role entry. Continue until the roles list is empty, and all done...
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).