Board logo

标题: 【练习-026】批处理将文本数据写入表格 [打印本页]

作者: pusofalse    时间: 2008-10-5 15:00     标题: 【练习-026】批处理将文本数据写入表格

出题目的:
  1、学习/巩固计算字符串长度技巧
  2、学习/巩固字符替换技巧
加分原则:
  1 思路新颖基分5分
  2 代码高效基分4分
  3 代码简洁基分2分
      4   代码通用基分3分
  5 无临时文件基分1分
  6 完美代码加15分
题目如下:
设a.txt中内容如下:
  1. Come with you, in the silence of darkness
  2. I want to show me secrets of life
  3. I will guide me where dream couldn't take me
  4. She seldom flew away in the night
  5. Me are the moonlight flower
  6. Me are the voice of the night
  7. When me call I'll follow
  8. We will leave on a trip of delight
复制代码
条件:a.txt中行数未知,每行的字符数未知,唯一知道的条件是不含全角(中文)字符。
要求:将文本内容写进表格,并把所有的you替换为me,所有的me替换为you。
但不包含其他含有字符me或you的单词,比如meet、your等。
如果me是在行首出现,则要替换为You(you同是)即第一个字母要大写。
尽量使用纯批方案。
输出如下:
  1. ┌────────────────────────┐
  2. │Come with me, in the silence of darkness        │
  3. ├────────────────────────┤
  4. │I want to show you secrets of life              │
  5. ├────────────────────────┤
  6. │I will guide you where dream couldn't take you  │
  7. ├────────────────────────┤
  8. │She seldom flew away in the night               │
  9. ├────────────────────────┤
  10. │You are the moonlight flower                    │
  11. ├────────────────────────┤
  12. │You are the voice of the night                  │
  13. ├────────────────────────┤
  14. │When you call I'll follow                       │
  15. ├────────────────────────┤
  16. │We will leave on a trip of delight              │
  17. └────────────────────────┘
复制代码

将以上内容,保存到文本,然后type一下,即可发现其输出格式。
给出其他文本,方便大家调试代码:
  1. I should have known all along
  2. there was something wrong
  3. I just never read between the lines
  4. Then I woke up one day and found me on your way
  5. Leaving nothing but my heart behind
  6. What can I do to make it up to me
  7. Promises don't come easy
  8. But tell me if there's a way to bring me back home to stay
  9. Well I'd promises anything to me
  10. I've been walkin' around with my head hanging down
复制代码
  1. I lie awake at night
  2. see thing in black and white
  3. I've only got me inside my mind
  4. me know me have made you blind
  5. I lie awake and pray
  6. That me will look my way
  7. I have all this longing in my heart
  8. I knew it right from the start
复制代码

作者: yslyxqysl    时间: 2008-10-5 15:34

  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. echo ┌──────────────────────────────┐
  4. for /f "delims=" %%i in (a.txt) do set i=%%i&&call :aaa
  5. echo └──────────────────────────────┘
  6. pause>nul&&exit
  7. :aaa
  8. if not "%tp2%"=="" echo ├──────────────────────────────┤
  9. :bbb
  10. if not "!i:~0,%tp%!"=="%i%" set /a tp+=1&&goto bbb
  11. set /a spacenum=60-%tp%
  12. if %spacenum% lss 0 set spacenum=0
  13. set tp=
  14. set space=
  15. for /l %%t in (1 1 %spacenum%) do set space= !space!
  16. echo │%i%%space%│
  17. set tp2=aaa
  18. goto :eof
复制代码
哈哈,先下手为强!!(仅做到输进表格)

[ 本帖最后由 yslyxqysl 于 2008-10-5 15:39 编辑 ]
作者: pusofalse    时间: 2008-10-5 15:51     标题: 回复 2楼 的帖子

如果a.txt中是这样呢?
  1. Come with you, in the silence of darkness
  2. I want to show me secrets of life
  3. I will guide me where dream couldn't taaaaaaaaaaaaaaaaaaaake me
  4. She seldom flew away in the night
  5. Me are the moonlight flower
  6. Me are the voice of the night
  7. When me call I'll follow
  8. We will leave on a trip of delight
复制代码

作者: yslyxqysl    时间: 2008-10-5 17:50

  1. @echo off&&setlocal ENABLEDELAYEDEXPANSION
  2. for /f "delims=" %%e in (a.txt) do set e=%%e&&call :abc
  3. set /a tp=longest%%2
  4. if %tp%==1 set /a longest+=1
  5. set /a tp=%longest%/2
  6. for /l %%m in (1 1 %tp%) do set heng=─!heng!
  7. echo ┌%heng%┐
  8. for /f "delims=" %%i in (a.txt) do (
  9. set i=%%i
  10. if not "!tp2!"=="" echo ├%heng%┤
  11. set tp=
  12. call :aaa
  13. )
  14. echo └%heng%┘&&pause>nul&&exit
  15. :abc
  16. if not "%tp%"=="" (if not "!e:~0,%tp%!"=="%e%" set /a tp+=1&&goto abc) else ^
  17. set /a tp+=1&&goto abc
  18. if "%longest%"=="" (set longest=%tp%) else if longest gtr %tp% set longest=%tp%
  19. goto :eof
  20. :aaa
  21. if not "!i:~0,%tp%!"=="%i%" set /a tp+=1&&goto aaa
  22. set /a spacenum=%longest%-%tp%
  23. if %spacenum% lss 0 set spacenum=0
  24. set space=&&set tp=
  25. for /l %%t in (1 1 %spacenum%) do set space= !space!
  26. echo │%i%%space%│&&set tp2=aaa&&goto :eof
复制代码

超级复杂版

[ 本帖最后由 yslyxqysl 于 2008-10-5 17:54 编辑 ]
作者: FOR    时间: 2008-10-5 22:08

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in (a.txt) do (
  3.   set "str= %%i "
  4.   set "str=!str: you = 临时 !"
  5.   set "str=!str: me = you !"
  6.   if "!str:~0,5!"==" you " set "str= You !str:~5!"
  7.   set "str=!str: 临时 = me !"
  8.   set num=!str:~1,-1!
  9.   set /a n=0,m+=1
  10.   set "var!m!=!num!"
  11.   call :loop "!num!"
  12.   if !n! gtr !h! set /a h=n
  13. )
  14. set /a n=h/2+1,h=n*2
  15. for /l %%i in (1 1 %n%) do set u=─!u!&set "l=  !l!"
  16. echo ┌!u!┐
  17.    for /l %%i in (1 1 %m%) do (
  18.    if %%i neq 1  echo ├!u!┤
  19.    set "c=!var%%i!!l!"
  20.    echo │!c:~0,%h%!│
  21. )
  22. echo └!u!┘
  23. pause
  24. exit
  25. :loop
  26. if defined num (set /a n+=1) else goto :eof
  27. set num=!num:~1!
  28. goto loop
复制代码

作者: FOR    时间: 2008-10-6 03:35

4楼代码貌似没有进行 替换 ?
作者: batman    时间: 2008-10-6 12:50     标题: 也来一个

将表格定为一行80个字符,不计算文本字符数
  1. @echo off&setlocal enabledelayedexpansion
  2. set "code=me#you you#me Me#You You#Me"
  3. for /l %%i in (1,1,80) do set "kong=!kong! "
  4. for /f "delims=" %%i in (a.txt) do set /a num+=1
  5. echo ┌──────────────────────────────────────┐
  6. for /f "delims=" %%a in (a.txt) do (
  7.     set /a n+=1
  8.     for %%b in (%%a) do (
  9.         if "!code:%%b=!" neq "%code%" (
  10.            for %%c in (%code%) do (
  11.                for /f "tokens=1,2 delims=#" %%d in ("%%c") do (
  12.                    if "%%b" equ "%%d" set "str=!str! %%e"
  13.                )
  14.            )
  15.            ) else (
  16.            set "str=!str! %%b"
  17.         )
  18.     )
  19.     set "str=!str!%kong%"
  20.     echo │!str:~,76!│&set "str="
  21.     if "!n!" neq "%num%" (
  22.        echo ├──────────────────────────────────────┤
  23.     ) else (
  24.     echo └──────────────────────────────────────┘
  25.     )
  26. )
  27. pause>nul
复制代码

作者: keen    时间: 2009-5-20 19:51

只是将文本内容放入表中,没有替换:
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=:" %%i in ('findstr /n .* a.txt') do set /a n=%%i-1
  3. echo ┌─────────────────────────────────────┐
  4. for /f "delims=" %%a in (a.txt) do (set str=%%a
  5.     for /l %%k in (1 1 80) do (set "str=!str! "
  6.         if "!str:~73!" equ " " echo │!str!│
  7. )
  8.     set /a m+=1
  9.     if !m! leq %n% call :lp
  10. )
  11. echo └─────────────────────────────────────┘
  12. pause>nul
  13. :lp
  14. echo ├─────────────────────────────────────┤
复制代码

作者: netbenton    时间: 2009-5-20 21:23

练习一下
  1. @echo off&setlocal enabledelayedexpansion
  2. set ech=─────────────────────────────────
  3. set spa=!ech:─=  !
  4. echo    ┌%ech%┐
  5. for /f "delims=" %%a in (a.txt) do (set "str=%%a "
  6. if defined var echo    ├%ech%┤
  7.     set str=!str:.= .!
  8.     set str=!str:,= ,!
  9.     set tou=!str:~,4!
  10.     set tou=!tou:me =临!
  11.     set tou=!tou:you =Me !
  12.     set tou=!tou:临=You !
  13.     set str=!str: me =临!
  14.     set str=!str: you = me !
  15.     set str=!str:临= you !
  16.     set str=!tou!!str:~4,-1!
  17.     set str=!str: .=.!
  18.     set str=!str: ,=,!
  19.         call :sub
  20. )
  21. echo    └%ech%┘
  22. pause>nul
  23. goto :eof
  24. :sub
  25. (if "!str:~66!" neq "" (
  26.         echo    │!str:~,66!│
  27. ) else (
  28.         set var=!str!!spa!
  29.         echo    │!var:~,66!│
  30. )
  31. set str=!str:~66!
  32. if defined str goto :sub)
复制代码





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