脚本专栏 
首页 > 脚本专栏 > 浏览文章

批处理数值排序问题

(编辑:jimmy 日期: 2024/11/15 浏览:3 次 )
a.txt中有20行随机产生的数列,如下:
2928326128601232462131283250710027308938740594716691200992050511576
5352129649530193383124730478244772348721985707222557212265817305
217141333532296179938475175265792931789219830308392472584606305
2371620291160322081050531817416284225477019123161801285941026814244
283929972304551060318886921731765136928849135391662294051194618754
1809165929787147057932949630411324311737224509104016550662932273
27396236084901303873154718299242931819623155304661177528921164510335
254221462410491137971033914630292752245114969186002809930190939425
1085287492160525651862932475207612387312368408826675135332406418337
2567810118246621010283281198810903279355871571118961177731143829148
23727111515524141721964179351992331180134926914198081871053303186
378579502856625703213542353218420835730692264021219729654278515442
30215186011014395001656818458819061824708536511543271701327524725
223702764213159156022932717903282522044350522584222768193271431422354
3079720530119542370417125702274761144023302102641160114921224469221
15642298214000242538839193816839550322381321993212316517861828002
13042178002978222022331319116624809338275899045263351248023569
11252165681825711849278422768716060438517976169102391532289954712000
105084292396529699311371735329685626410510259482788519645152723476
29674179062831103792824121564178225289202161443911094228581583531951
要求通过纯批处理按照数列的大小顺序,正序输出如下:
13042178002978222022331319116624809338275899045263351248023569
217141333532296179938475175265792931789219830308392472584606305
1809165929787147057932949630411324311737224509104016550662932273
5352129649530193383124730478244772348721985707222557212265817305
15642298214000242538839193816839550322381321993212316517861828002
23727111515524141721964179351992331180134926914198081871053303186
30215186011014395001656818458819061824708536511543271701327524725
105084292396529699311371735329685626410510259482788519645152723476
254221462410491137971033914630292752245114969186002809930190939425
283929972304551060318886921731765136928849135391662294051194618754
378579502856625703213542353218420835730692264021219729654278515442
1085287492160525651862932475207612387312368408826675135332406418337
2371620291160322081050531817416284225477019123161801285941026814244
2567810118246621010283281198810903279355871571118961177731143829148
2928326128601232462131283250710027308938740594716691200992050511576
3079720530119542370417125702274761144023302102641160114921224469221
11252165681825711849278422768716060438517976169102391532289954712000
27396236084901303873154718299242931819623155304661177528921164510335
29674179062831103792824121564178225289202161443911094228581583531951
223702764213159156022932717903282522044350522584222768193271431422354
 
每行的数值都远远超过了cmd所能计算的最大数值。
要求,正确输出,不生成临时文件,代码高效通用。完成题目,视思路加分。
batman:
先说明下思路:
  此题确实是出给我们的一大难题,为什么这要说呢?楼主要求的是文本内所有超大数值的排序,
所有的数值都远远超过了cmd所能运算的最大数值,用常规的比较法都是行不通的这是难点之一;
第二这些数值是随机生成的且字符总数不定,甚至可能达到数行和数十行之长,如采用逐字符判断
的方法来确实行最大字符数,效率将会是此类方案所无法逾越的障碍;第三楼主要求不生成临时文
件,这对用findstr /o来获取行最大字符的方案来讲,无疑是锁上了大门。
  综上所述总结如下:
  代码要通用就要获取行最大字符数,一种方法是逐字符法,用逐字符法效率上就存在很大问题;
第二种方法是使用findstr /o一次性获取每行字符偏移量,再通过处理获得行最大字符数,效率上比
逐字符法是高多了,但因为单用findstr /o是不能获得文本行未行字符偏移量的,必须要对未行强加
回车,在不破坏原文件的情况下就要用到临时文件。
  而我们写代码时一般遵循四条原则:高效率、通用、简洁、尽量不生成临时文件,其中首要的
一条就是高效,其次是通用,至于简洁和有无临时文件都不是主要考虑因素,所以依此主次关系我
给出以下两种方案:
一、通用性差一点(数值字符都在一行内是绝对没问题的),效率高,代码简洁,无临时文件:
@echo off&setlocal enabledelayedexpansion
for /l %%i in (1,1,80) do set "kong=!kong!#"
for /f %%i in (1.txt) do (
set "str=%%i%kong%"
set "a=!str:~,80!"
set "a=!a:%%i=!"
set "_!a!%%i=a"
)
for /f "delims==_" %%i in ('set _') do (
set "str=%%i"
echo !str:#=!
)
pause>nul
二、通用性极高,效率一般,生成临时文件,代码较复杂:
复制代码 代码如下:
@echo off&setlocal enabledelayedexpansion
set "max=0"&set "a=0"
for /f %%i in (1.txt) do echo %%i2.txt
echo.2.txt
for /f "tokens=1,2* delims=:" %%i in ('findstr /n /o .* 2.txt') do (
set /a n+=1,m=n-1
set "num=%%i"&set "_!n!=%%j"&set "#%%i=%%k"
if !m! gtr 0 set /a a=_!n!-_!m!-2
if !max! lss !a! set "max=!a!"
)
set /a num-=1
for /l %%i in (1,1,%max%) do set "kong=!kong!#"
for /l %%i in (1,1,%num%) do (
set "str=!#%%i!%kong%"
set "a=!str:~,%max%!"
call,set "a=%%a:!#%%i!=%%"
set ".!a!!#%%i!=a"
)
for /f "delims==." %%i in ('set .') do (
set "str=%%i"
echo !str:#=!
)
del /q 2.txt&pause>nul

 
pusofalse:
复制代码 代码如下:
@echo off&setlocal enabledelayedexpansion
set m=0
for /f "tokens=1,* delims=:" %%a in ('findstr/o .* 1.txt') do (
set/a n+=1,l=n-1,y+=1
set ..!n!=%%a
set ##!y!=%%b
if !n! geq 2 (
call,set/a s=%%..!n!%%-%%..!l!%%-2,line+=1
call,set "_!s!=%%_!s!%%%%##!line!%% "
if !s! geq !m! set m=!s!
)
)
for /f "skip=1 delims=:" %%a in ('^(echo !##%y%!^&echo.^)^|findstr/o .*') do set/a final=%%a-3
call,set "_%final%=%%_!final!%% !##%y%!"
if %final% geq !m! set m=%final%
for /l %%a in (1 1 %m%) do (
if defined _%%a (
for %%i in (!_%%a!) do set -%%i=faith
for /f "delims=-=" %%s in ('set -') do (
echo %%s
set "-%%s="
)
)
)
pause>nul
上一篇:文本最末列数值乘一个数的批处理实现
下一篇:批处理实现文本内容横列变纵列的代码
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap