标题: 【练习-006】批处理实现文本内容居中显示 [打印本页]
作者: pusofalse 时间: 2008-7-31 11:43 标题: 【练习-006】批处理实现文本内容居中显示
a.txt-
- [Promise don't come easy]
- 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 you on your way.
- Leaving nothing but my heart behind.
- What can I do to make it up to you.
- Promises don't come easy.
- But tell me if there's a way to bring you back home to stay.
- Well I'd promises anything to you.
- I've been walkin' around with my head hanging down.
- Wondrin' what I'm gonna do.
- 'Cause when you walked out that door.
复制代码
要求,CMD原始窗口中,不调节窗口大小,不生成临时文件,居中显示文本内容。输出如下:-
-
- [Promise don't come easy]
- 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 you on your way.
- Leaving nothing but my heart behind.
- What can I do to make it up to you.
- Promises don't come easy.
- But tell me if there's a way to bring you back home to stay.
- Well I'd promises anything to you.
- I've been walkin' around with my head hanging down.
- Wondrin' what I'm gonna do.
- 'Cause when you walked out that door.
复制代码
-----------------------------------------------------------------------------------
至此,已有比较完美的答案,见2楼sed版,与6楼findstr版。
[ 本帖最后由 pusofalse 于 2008-7-31 21:17 编辑 ]
作者: Batcher 时间: 2008-7-31 11:50
- sed -e :a -e "s/^.\{1,77\}$/ & /;ta" a.txt
复制代码
sed.exe
http://rtngslin.moe.hm/cndos-up/img/561.zip
作者: pusofalse 时间: 2008-7-31 12:09
高~ 竟然一句搞定。^_^
[ 本帖最后由 pusofalse 于 2008-7-31 12:11 编辑 ]
作者: batman 时间: 2008-7-31 12:41
我还是喜欢用纯批处理,代码先不发了,先看看别人的吧。。。
作者: more 时间: 2008-7-31 13:08 标题: 逐字判断,效率低下
- @echo off
- setlocal enabledelayedexpansion
- for /f "delims=" %%a in ('type 1.txt') do (
- set "str=%%a"&set "n=0"
- call :count
- )
- echo.&pause&exit
- :count
- set "var=!str:~%n%,1!"
- set /a "n+=1"
- if not "!str:~%n%,1!"=="" (goto :count)
- set /a "m=%n%/2"
- set /a "mm=40-%m%"
- for /l %%c in (1 1 %mm%) do (call set "kong=%%kong%% ")
- echo %kong%%str%
- set "kong="
复制代码
作者: batman 时间: 2008-7-31 16:23
思路说明:
以下代码是充分findstr命令的示例,使用findstr /n来取得行号同时兼容空行,
并将行号进行变量赋值;使用findstr /o来取得第行的字符偏移量,但要通过
上、下行的字符偏移差,因为字符偏移值是类计递加的,同时每行回车增加
两个字符偏移,所以不可避免要对最后一行强加回车,在不破坏原文件的情
况下只好生成临时文件;利用findstr /o能够一次性获得文本所有行的字符偏
移量也就是字符总数,效率和逐字符判断相比肯定是高得多了。
---------------------------------
- @echo off
- copy /y 1.txt 2.txt>nul
- echo.>>2.txt&echo end>>2.txt
- for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
- set "_%%i=%%j"&set ".%%i=%%k"
- if %%i geq 2 call :lp %%i
- )
- del /q 2.txt&pause>nul&goto :eof
- :lp
- set /a n=%1-1
- set /a num=_%1-_%n%-2
- set /a kl=(80-num)/2
- for /l %%i in (1,1,%kl%) do set /p= <NUL
- call,set /p=%%.%n%%%<NUL&ECHO.
-
复制代码
作者: more 时间: 2008-7-31 17:12
哈哈,看了版主的代码终于知道findstr /o的用法了,收获不小啊,非常感谢!!!
作者: more 时间: 2008-7-31 17:17
- for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
- ...
复制代码
好像delims=:就行了吧,空格没有必要吧.
作者: batman 时间: 2008-8-2 09:15
再来一个更简单的(no findstr no 逐字符 no tempfile no 变量延迟):
- @echo off
- for /l %%i in (1,1,80) do call,set "kong=%%kong%% "
- for /f "delims=" %%i in (1.txt) do (
- set "str=%%i%kong%"
- call,set "str=%%str:~,80%%"
- call,set "str=%%str:%%i=%%"
- call,set "str=%%str: = %%"
- call,echo %%str%%%%i
- )
- pause>nul
复制代码
[ 本帖最后由 batman 于 2008-8-2 12:04 编辑 ]
作者: keen 时间: 2009-4-26 00:13
- @echo off
- for /f "delims=" %%i in (a.txt) do (
- set v=%%i
- set str=%%i
- set n=0
- call :lp
- )
- pause&exit/b
- :lp
- set /a n+=1
- set v=%v:~1%
- if not "%v%"=="" (goto lp) else (goto next)
- :next
- set /a m=(77-%n%)/2
- for /l %%j in (1 1 %m%) do set /p= <nul
- echo %str%
- goto :eof
复制代码
作者: batman 时间: 2009-4-26 00:50
楼上的没考虑空行,同时逐字符效率是大问题,还有最后的goto :eof是多余的。
作者: keen 时间: 2009-4-27 11:30 标题: 回复 11楼 的帖子
没办法,水平有限,没考虑空行和效率,看了你的9楼的代码才知道还可以整体考虑,学习了。
goto :eof 我的理解是它可以让程序执行call的下一行
作者: netbenton 时间: 2009-4-28 13:19
偶也来个纯批的,没有外部命令
每循环一次在两边各增加一个空格,直到超过79个字符
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in (a.txt) do (set "str=%%a"
- for /l %%b in (1,1,40) do (if "!str:~78!" equ "" set "str= !str! ")
- echo,!str:~,79!
- )
- pause
复制代码
[ 本帖最后由 netbenton 于 2009-4-28 13:22 编辑 ]
作者: biluncloud 时间: 2011-12-22 14:23
再来一个更简单的(no findstr no 逐字符 no tempfile no 变量延迟):
[ 本帖最后由 batman 于 2008-8 ...
batman 发表于 2008-8-2 09:15
看练习题的时候看到这道题,然后看到版主竟然使用如此精妙的方法来处理,激动之余冲上来顶一下,太强大了
作者: 喃喃努努 时间: 2012-5-20 23:06
不懂。。。
作者: designTech 时间: 2015-12-29 12:14
回复 13# netbenton
如果行尾有tab或者空格时,程序就不行了
作者: codegay 时间: 2016-5-2 04:58
- """
- python文本内容居中显示.py
- http://bbs.bathome.net/thread-1289-1-1.html
- 2016年5月2日 04:51:33 codegay
- """
-
- with open("a.txt") as f:
- txt=[r.strip() for r in f]
- w=max([len(r) for r in txt])+10
- [print(r.center(w)) for r in txt]
复制代码
作者: taofan712 时间: 2017-3-1 11:03
本帖最后由 taofan712 于 2017-3-1 11:06 编辑
- @echo off
- setlocal enabledelayedexpansion
- set "kong= "
- for /f "delims=" %%a in (a.txt) do (
- setlocal
- for %%b in (%%a) do set /a n+=1
- set /a num=80-n*4
- set /a num=num/8
- for /l %%c in (1,1,!num!) do set/p=%kong%<nul
- set/p=%%a<nul
- for /l %%c in (1,1,!num!) do set/p=%kong%<nul
- echo;
- endlocal
- )
- pause
复制代码
取近似值做的,平均每个单词4个字母。效果不错。
重要的是我想借此问一下,我第八行set /a num=num/8为什么不能跟第七行写一起set /a num=(80-n*4)/8 会报错。
作者: qixiaobin0715 时间: 2021-3-20 10:41
- @echo off
- setlocal enabledelayedexpansion
- for /l %%i in (1,1,40) do set "space=!space! "
- for /f "tokens=1* delims=:" %%a in ('findstr /o .* 1.txt') do (
- if defined num2 (
- set /a "num1=(%%a-!num2!)/2"
- call echo,%%str:~!num1!%%
- )
- set num2=%%a
- set str=!space!%%b
- )
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |