突然想用批处理对数字进行排序. 以前我们是对几个数字分别进行比对, 按照大小排列出来, 但处理的个数很小, 数字超过10个代码就会很冗长, 并且效率比较低.
如果批处理有像高级语言中数组类型的概念就好了, 但很遗憾. 在批处理中人工构造一个数组, 这个问题就好解决了.
于是写了以下代码, 可以对N个数字进行大到小或小到大的排序. 只需要改变IF语句中的"lss"或"gtr".
@echo off
setlocal enabledelayedexpansion
set num=0
:input
set /a num+=1
cls
set /p num%num%=请输入一组数字, 终止直接回车 :
if not "!num%num%!"=="" goto input
set /a num-=1
set /a num_=num-1
cls
echo 排序former:
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 排序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
测试代码:
@echo off
setlocal enabledelayedexpansion
for /l %%l in (1,1,10) do (
set t=%%l
call :start
)
set /a num_=num-1
echo 排序former:
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 排序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 ]
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.
@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 ]