Board logo

标题: [代码合集] [已解决]批处理实现两文本同行交替输出怎样解释? [打印本页]

作者: mstsc    时间: 2011-9-5 22:56     标题: [已解决]批处理实现两文本同行交替输出怎样解释?

BATHOME 练习汇总~~1——50


--------------------------------------------------------------------------------

【练习-001】批处理实现两文本同行交替输出

有两个文本如下(实际中并不知道两文本各有多少行):

文本1.txt

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ccccccccccccccccccccccccccccccccccccccc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ggggggggggggggggggggggggggggggggggggggg
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

文本2.txt

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

要求用批处理输出如下(两文本交替输出):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
ccccccccccccccccccccccccccccccccccccccc
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
ggggggggggggggggggggggggggggggggggggggg
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

要求:
1 尽量不生成临时文件
2 代码要高效
3 代码尽量简洁
目的:
唯一的目的在于共同提高!
 


--------------------------------------------------------------------------------

显示答案 关闭答案

--------------------------------------------------------------------------------

more:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%a in ('type 11.txt') do (
  4.    set /a m+=1
  5.    set "home!m!=%%a"
  6. )
  7. for /f "delims=" %%b in ('type 12.txt') do (
  8.    set /a n+=1
  9.    set "bat!n!=%%b"
  10. )
  11. if %m% gtr %n% (
  12.    for /l %%c in (1 1 %m%) do (
  13.       if not "!home%%c!"=="" echo !home%%c!
  14.       if not "!bat%%c!"=="" echo !bat%%c!
  15.    )
  16. ) else (
  17.    for /l %%d in (1 1 %n%) do (
  18.       if not "!home%%d!"=="" echo !home%%d!
  19.       if not "!bat%%d!"=="" echo !bat%%d!
  20.    )
  21. )
  22. pause
复制代码
 

batman:
  1. @echo off
  2. for /f "delims=" %%i in (1.txt) do set /a n+=1&call,set "_%%n%%=%%i"
  3. for /f "delims=" %%i in (2.txt) do set /a m+=1&call,set ".%%m%%=%%i"
  4. if %n% gtr %m% (set "num=%n%") else (set "num=%m%")
  5. for /l %%i in (1,1,%num%) do (
  6.      if defined _%%i call,echo %%_%%i%%
  7.      if defined .%%i call,echo %%.%%i%%
  8. )
  9. pause>nul
复制代码
 

pusofalse:
  1. @echo off&setlocal enabledelayedexpansion&set n=-1
  2. for /f "delims=" %%a in (1.txt) do (
  3. set/a n+=1
  4. set flag=
  5. call :lp %%a
  6. if not defined flag echo %%a
  7. )
  8. :lp
  9. if "%1" equ "" set/a n+=1
  10. set m=skip=%n%
  11. if "%m%" equ "skip=0" set "m="
  12. for /f "%m% delims=" %%a in (2.txt) do if "%1" neq "" (echo %1&echo %%a&set flag=a&goto :eof) else echo %%a
  13. if "%1" equ "" pause
复制代码
 

 

batman:
  1. @echo off
  2. set "n=-1"&set "flag=a"
  3. :begin
  4. set /a n+=1
  5. if %n% equ 0 (set "m=") else (set "m=skip=%n%")
  6. for /f "%m% delims=" %%i in (1.txt) do echo %%i&if defined flag goto next
  7. set no=a
  8. :next
  9. for /f "%m% delims=" %%i in (2.txt) do echo %%i&goto begin
  10. if not defined no set "flag="&goto begin
  11. pause>nul
复制代码
 

batman:
  1. @echo off&setlocal enabledelayedexpansion
  2. set "num=0"
  3. for %%a in (1.txt 2.txt) do (
  4.     for /f "delims=" %%i in (%%a) do (
  5.         set /a n+=1
  6.         if !n! gtr !num! set "num=!n!"
  7.         set /p=%%i <nul>>temp.txt
  8.     )
  9.     echo.>>temp.txt&set "n=0"
  10. )
  11. :lp
  12. set /a n+=1
  13. for /f "tokens=%n%" %%i in (temp.txt) do echo %%i
  14. if %n% neq %num% goto lp
  15. del /q temp.txt&pause>nul
复制代码
 


--------------------------------------------------------------------------------

made by 基拉freedom 2009.8.13

请问  这里显示的答案那种适合新手学习呢?

还有  就是这里能不能给新手解释一下每段代码是什么作用呢?


我还想特别问一下,下面每段代码的意思:
  1. @echo off&setlocal enabledelayedexpansion
  2. set "num=0"
  3. for %%a in (1.txt 2.txt) do (
  4.     for /f "delims=" %%i in (%%a) do (
  5.         set /a n+=1
  6.         if !n! gtr !num! set "num=!n!"
  7.         set /p=%%i <nul>>temp.txt
  8.     )
  9.     echo.>>temp.txt&set "n=0"
  10. )
  11. :lp
  12. set /a n+=1
  13. for /f "tokens=%n%" %%i in (temp.txt) do echo %%i
  14. if %n% neq %num% goto lp
  15. del /q temp.txt&pause>nul
复制代码
谢谢
作者: Batcher    时间: 2011-9-6 09:14

每段代码的意思?每段代码都是为了实现题目的要求:两文本同行交替输出
你具体哪里看不懂,直接问。
作者: mstsc    时间: 2011-9-6 22:26

我的意思是:
    希望能看懂每段代码的主用,这样在不懂得地方就能了解。不像现在都不知道代码是怎么实现目的的。


我想‘这样久而久之,就能看懂代码的意思,从而把学到的知识来解决自己的问题’




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2