i neeed to Write a bat file that accepts three parameters. Of these three parameters, find the smallest one, then display all the numbers from 1 to AND the smallest parameter on the screen. i tried this but it does not work
@ECHO OFF
set /a c=%1
set /a b=%2
set /a a=%3
set Smallest=%d%
if %c% lss %Smallest% set Smallest=%c%
if %b% lss %Smallest% set Smallest=%b%
if %a% lss %Smallest% set Smallest=%a%
Echo Smallest number is %Smallest%
pause>nul
CodePudding user response:
Here is an example code for this homework which outputs the smallest number and all numbers from 1
to the smallest number if the smallest number is not less than 1
.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Smallest=%~1"
if not defined Smallest goto OutputHelp
if "%~2" == "" goto OutputHelp
:CompareLoop
shift
if "%~1" == "" goto OutputResult
if %~1 LSS %Smallest% set "Smallest=%~1"
goto CompareLoop
:OutputResult
echo The smallest number is: %Smallest%
if %Smallest% LSS 1 goto EndBatch
echo/
for /L %%I in (1,1,%Smallest%) do echo %%I
goto EndBatch
:OutputHelp
echo %~nx0 must be run with at least two integer numbers as arguments.
echo/
echo Example: %~nx0 489 0x2AF 0715
echo/
echo An integer number beginning with 0x is interpreted hexadecimal.
echo An integer number beginning with 0 is interpreted as octal number
echo which cannot contain the digits 8 and 9 to be valid. An invalid
echo octal number is interpreted with value 0.
echo/
echo There is not checked if an argument string is a valid string for
echo a decimal, hexadecimal or octal integer number in the value
echo range -2147483648 to 2147483647 (32 bit signed integer).
:EndBatch
echo/
pause
endlocal
The output of this batch file on running it with the three numbers of the example without the output of all numbers from 1
to smallest number is:
The smallest number is: 0715
The octal number 0715
is decimal 461
and the hexadecimal number 0x2AF
is decimal 687
which makes the octal number 0715
the smallest number.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
setlocal /?
shift /?
I recommend to read also:
- Safe number comparison in Windows batch file
- Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files
- Why is no string output with 'echo %var%' after using 'set var = text' on command line?
- DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
CodePudding user response:
This is the way I would do it:
@echo off
setlocal
set /A c=%1, b=%2, a=%3
set /A "comp=((a-b)>>31) 1,one=!comp*a comp*b,comp=((one-c)>>31) 1,Smallest=!comp*one comp*c"
echo Smallest: %Smallest%
for /L %%i in (1,1,%Smallest%) do echo %%i
That is, if a > b then (a-b)
is positive or zero (else, is negative), and (a-b)>>31
is zero (else, is -1 because >>
is a signed shift of 31 bits) so finally ((a-b)>>31) 1
is one if a > b or is zero if a < b. In this way, we use this value comp
to get b, or not this value !comp!
to get a; and store the lesser of both in one
variable.
The same method is used to get the lesser of one
and c
.
At end, we show all values from 1 to the lesser...