Suddenly I want to use batch processing to sort numbers. Previously, we compared several numbers respectively and arranged them in order, but when the number of numbers is small, if the number exceeds 10, the code will be very lengthy and the efficiency is relatively low.
If batch processing had the concept of array types like high-level languages, it would be great, but unfortunately. In batch processing, manually constructing an array can solve this problem.
So I wrote the following code, which can sort N numbers in descending or ascending order. Just change "lss" or "gtr" in the IF statement.
Test code:
[ Last edited by pengfei on 2006-11-15 at 06:54 AM ]
If batch processing had the concept of array types like high-level languages, it would be great, but unfortunately. In batch processing, manually constructing an array can solve this problem.
So I wrote the following code, which can sort N numbers in descending or ascending order. Just change "lss" or "gtr" in the IF statement.
@echo off
setlocal enabledelayedexpansion
set num=0
:input
set /a num+=1
cls
set /p num%num%=Please enter a group of numbers, terminate by directly pressing Enter :
if not "!num%num%!"=="" goto input
set /a num-=1
set /a num_=num-1
cls
echo Former sort:
for /l %%k in (1,1,%num%) do (
set /p ii=!num%%k! <nul
)
for /l %%i in (1,1,%num_%) do (
set /a n=num-%%i
for /l %%j in (1,1,!n!) do (
set x=%%j
set /a y=x+1
call :go
)
)
echo.
echo.
echo Sorted after:
for /l %%k in (1,1,%num%) do (
set /p ii=!num%%k! <nul
)
echo.
pause >nul
goto :eof
:go
if !num%x%! gtr !num%y%! (
set i=!num%x%!
set num%x%=!num%y%!
set num%y%=!i!
)
goto :eof
Test code:
@echo off
setlocal enabledelayedexpansion
for /l %%l in (1,1,10) do (
set t=%%l
call :start
)
set /a num_=num-1
echo Former sort:
for /l %%k in (1,1,%num%) do (
set /p ii=!num%%k! <nul
)
for /l %%i in (1,1,%num_%) do (
set /a n=num-%%i
for /l %%j in (1,1,!n!) do (
set x=%%j
set /a y=x+1
call :go
)
)
echo.
echo.
echo Sorted after:
for /l %%k in (1,1,%num%) do (
set /p ii=!num%%k! <nul
)
echo.
pause >nul
goto :eof
:go
if !num%x%! lss !num%y%! (
set i=!num%x%!
set num%x%=!num%y%!
set num%y%=!i!
)
goto :eof
:start
set j=!RANDOM:~0,1!
set num%t%=!RANDOM:~0,%j%!
set /a num+=1
goto :eof
[ Last edited by pengfei on 2006-11-15 at 06:54 AM ]
Recent Ratings for This Post
( 1 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| redtek | +2 | 2006-11-15 00:38 |
