标题: 【练习-026】批处理将文本数据写入表格 [打印本页]
作者: pusofalse 时间: 2008-10-5 15:00 标题: 【练习-026】批处理将文本数据写入表格
出题目的:
1、学习/巩固计算字符串长度技巧
2、学习/巩固字符替换技巧
加分原则:
1 思路新颖基分5分
2 代码高效基分4分
3 代码简洁基分2分
4 代码通用基分3分
5 无临时文件基分1分
6 完美代码加15分
题目如下:
设a.txt中内容如下:- Come with you, in the silence of darkness
- I want to show me secrets of life
- I will guide me where dream couldn't take me
- She seldom flew away in the night
- Me are the moonlight flower
- Me are the voice of the night
- When me call I'll follow
- We will leave on a trip of delight
复制代码
条件:a.txt中行数未知,每行的字符数未知,唯一知道的条件是不含全角(中文)字符。
要求:将文本内容写进表格,并把所有的you替换为me,所有的me替换为you。
但不包含其他含有字符me或you的单词,比如meet、your等。
如果me是在行首出现,则要替换为You(you同是)即第一个字母要大写。
尽量使用纯批方案。
输出如下:- ┌────────────────────────┐
- │Come with me, in the silence of darkness │
- ├────────────────────────┤
- │I want to show you secrets of life │
- ├────────────────────────┤
- │I will guide you where dream couldn't take you │
- ├────────────────────────┤
- │She seldom flew away in the night │
- ├────────────────────────┤
- │You are the moonlight flower │
- ├────────────────────────┤
- │You are the voice of the night │
- ├────────────────────────┤
- │When you call I'll follow │
- ├────────────────────────┤
- │We will leave on a trip of delight │
- └────────────────────────┘
复制代码
将以上内容,保存到文本,然后type一下,即可发现其输出格式。
给出其他文本,方便大家调试代码:- I should have known all along
- there was something wrong
- I just never read between the lines
- Then I woke up one day and found me on your way
- Leaving nothing but my heart behind
- What can I do to make it up to me
- Promises don't come easy
- But tell me if there's a way to bring me back home to stay
- Well I'd promises anything to me
- I've been walkin' around with my head hanging down
复制代码
- I lie awake at night
- see thing in black and white
- I've only got me inside my mind
- me know me have made you blind
- I lie awake and pray
- That me will look my way
- I have all this longing in my heart
- I knew it right from the start
复制代码
作者: yslyxqysl 时间: 2008-10-5 15:34
- @echo off
- setlocal ENABLEDELAYEDEXPANSION
- echo ┌──────────────────────────────┐
- for /f "delims=" %%i in (a.txt) do set i=%%i&&call :aaa
- echo └──────────────────────────────┘
- pause>nul&&exit
- :aaa
- if not "%tp2%"=="" echo ├──────────────────────────────┤
- :bbb
- if not "!i:~0,%tp%!"=="%i%" set /a tp+=1&&goto bbb
- set /a spacenum=60-%tp%
- if %spacenum% lss 0 set spacenum=0
- set tp=
- set space=
- for /l %%t in (1 1 %spacenum%) do set space= !space!
- echo │%i%%space%│
- set tp2=aaa
- goto :eof
复制代码
哈哈,先下手为强!!(仅做到输进表格)
[ 本帖最后由 yslyxqysl 于 2008-10-5 15:39 编辑 ]
作者: pusofalse 时间: 2008-10-5 15:51 标题: 回复 2楼 的帖子
如果a.txt中是这样呢?- Come with you, in the silence of darkness
- I want to show me secrets of life
- I will guide me where dream couldn't taaaaaaaaaaaaaaaaaaaake me
- She seldom flew away in the night
- Me are the moonlight flower
- Me are the voice of the night
- When me call I'll follow
- We will leave on a trip of delight
复制代码
作者: yslyxqysl 时间: 2008-10-5 17:50
- @echo off&&setlocal ENABLEDELAYEDEXPANSION
- for /f "delims=" %%e in (a.txt) do set e=%%e&&call :abc
- set /a tp=longest%%2
- if %tp%==1 set /a longest+=1
- set /a tp=%longest%/2
- for /l %%m in (1 1 %tp%) do set heng=─!heng!
- echo ┌%heng%┐
- for /f "delims=" %%i in (a.txt) do (
- set i=%%i
- if not "!tp2!"=="" echo ├%heng%┤
- set tp=
- call :aaa
- )
- echo └%heng%┘&&pause>nul&&exit
- :abc
- if not "%tp%"=="" (if not "!e:~0,%tp%!"=="%e%" set /a tp+=1&&goto abc) else ^
- set /a tp+=1&&goto abc
- if "%longest%"=="" (set longest=%tp%) else if longest gtr %tp% set longest=%tp%
- goto :eof
- :aaa
- if not "!i:~0,%tp%!"=="%i%" set /a tp+=1&&goto aaa
- set /a spacenum=%longest%-%tp%
- if %spacenum% lss 0 set spacenum=0
- set space=&&set tp=
- for /l %%t in (1 1 %spacenum%) do set space= !space!
- echo │%i%%space%│&&set tp2=aaa&&goto :eof
复制代码
超级复杂版
[ 本帖最后由 yslyxqysl 于 2008-10-5 17:54 编辑 ]
作者: FOR 时间: 2008-10-5 22:08
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%i in (a.txt) do (
- set "str= %%i "
- set "str=!str: you = 临时 !"
- set "str=!str: me = you !"
- if "!str:~0,5!"==" you " set "str= You !str:~5!"
- set "str=!str: 临时 = me !"
- set num=!str:~1,-1!
- set /a n=0,m+=1
- set "var!m!=!num!"
- call :loop "!num!"
- if !n! gtr !h! set /a h=n
- )
- set /a n=h/2+1,h=n*2
- for /l %%i in (1 1 %n%) do set u=─!u!&set "l= !l!"
- echo ┌!u!┐
- for /l %%i in (1 1 %m%) do (
- if %%i neq 1 echo ├!u!┤
- set "c=!var%%i!!l!"
- echo │!c:~0,%h%!│
- )
- echo └!u!┘
- pause
- exit
-
- :loop
- if defined num (set /a n+=1) else goto :eof
- set num=!num:~1!
- goto loop
复制代码
作者: FOR 时间: 2008-10-6 03:35
4楼代码貌似没有进行 替换 ?
作者: batman 时间: 2008-10-6 12:50 标题: 也来一个
将表格定为一行80个字符,不计算文本字符数- @echo off&setlocal enabledelayedexpansion
- set "code=me#you you#me Me#You You#Me"
- for /l %%i in (1,1,80) do set "kong=!kong! "
- for /f "delims=" %%i in (a.txt) do set /a num+=1
- echo ┌──────────────────────────────────────┐
- for /f "delims=" %%a in (a.txt) do (
- set /a n+=1
- for %%b in (%%a) do (
- if "!code:%%b=!" neq "%code%" (
- for %%c in (%code%) do (
- for /f "tokens=1,2 delims=#" %%d in ("%%c") do (
- if "%%b" equ "%%d" set "str=!str! %%e"
- )
- )
- ) else (
- set "str=!str! %%b"
- )
- )
- set "str=!str!%kong%"
- echo │!str:~,76!│&set "str="
- if "!n!" neq "%num%" (
- echo ├──────────────────────────────────────┤
- ) else (
-
- echo └──────────────────────────────────────┘
- )
- )
- pause>nul
复制代码
作者: keen 时间: 2009-5-20 19:51
只是将文本内容放入表中,没有替换:- @echo off&setlocal enabledelayedexpansion
- for /f "delims=:" %%i in ('findstr /n .* a.txt') do set /a n=%%i-1
- echo ┌─────────────────────────────────────┐
- for /f "delims=" %%a in (a.txt) do (set str=%%a
- for /l %%k in (1 1 80) do (set "str=!str! "
- if "!str:~73!" equ " " echo │!str!│
- )
- set /a m+=1
- if !m! leq %n% call :lp
- )
- echo └─────────────────────────────────────┘
- pause>nul
- :lp
- echo ├─────────────────────────────────────┤
复制代码
作者: netbenton 时间: 2009-5-20 21:23
练习一下
- @echo off&setlocal enabledelayedexpansion
- set ech=─────────────────────────────────
- set spa=!ech:─= !
- echo ┌%ech%┐
- for /f "delims=" %%a in (a.txt) do (set "str=%%a "
- if defined var echo ├%ech%┤
-
- set str=!str:.= .!
- set str=!str:,= ,!
-
- set tou=!str:~,4!
- set tou=!tou:me =临!
- set tou=!tou:you =Me !
- set tou=!tou:临=You !
-
- set str=!str: me =临!
- set str=!str: you = me !
- set str=!str:临= you !
-
- set str=!tou!!str:~4,-1!
- set str=!str: .=.!
- set str=!str: ,=,!
- call :sub
- )
- echo └%ech%┘
-
- pause>nul
- goto :eof
-
- :sub
- (if "!str:~66!" neq "" (
- echo │!str:~,66!│
- ) else (
- set var=!str!!spa!
- echo │!var:~,66!│
- )
- set str=!str:~66!
- if defined str goto :sub)
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |