批处理之家's Archiver

pusofalse 发表于 2008-7-31 11:43

【练习-006】批处理实现文本内容居中显示

a.txt[code]
[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.[/code]要求,CMD原始窗口中,不调节窗口大小,不生成临时文件,居中显示文本内容。输出如下:[code]

                          [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.[/code]
-----------------------------------------------------------------------------------
[color=red]至此,已有比较完美的答案,见2楼sed版,与6楼findstr版。[/color]

[[i] 本帖最后由 pusofalse 于 2008-7-31 21:17 编辑 [/i]]

Batcher 发表于 2008-7-31 11:50

[code]sed -e :a -e "s/^.\{1,77\}$/ & /;ta" a.txt[/code]sed.exe
[url]http://rtngslin.moe.hm/cndos-up/img/561.zip[/url]

pusofalse 发表于 2008-7-31 12:09

高~ 竟然一句搞定。^_^

[[i] 本帖最后由 pusofalse 于 2008-7-31 12:11 编辑 [/i]]

batman 发表于 2008-7-31 12:41

我还是喜欢用纯批处理,代码先不发了,先看看别人的吧。。。

more 发表于 2008-7-31 13:08

逐字判断,效率低下

[code]@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="[/code]

batman 发表于 2008-7-31 16:23

思路说明:
以下代码是充分findstr命令的示例,使用findstr /n来取得行号同时兼容空行,
并将行号进行变量赋值;使用findstr /o来取得第行的字符偏移量,但要通过
上、下行的字符偏移差,因为字符偏移值是类计递加的,同时每行回车增加
两个字符偏移,所以不可避免要对最后一行强加回车,在不破坏原文件的情
况下只好生成临时文件;利用findstr /o能够一次性获得文本所有行的字符偏
移量也就是字符总数,效率和逐字符判断相比肯定是高得多了。
---------------------------------
[code]@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.
[/code]

more 发表于 2008-7-31 17:12

哈哈,看了版主的代码终于知道findstr /o的用法了,收获不小啊,非常感谢!!!

more 发表于 2008-7-31 17:17

[code]for /f "tokens=1,2* delims=: " %%i in ('findstr /n /o .* 2.txt') do (
...[/code]好像delims=:就行了吧,空格没有必要吧.

batman 发表于 2008-8-2 09:15

再来一个更简单的(no findstr  no 逐字符 no tempfile no 变量延迟):
[code]
@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
[/code]

[[i] 本帖最后由 batman 于 2008-8-2 12:04 编辑 [/i]]

keen 发表于 2009-4-26 00:13

[code]@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[/code]

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个字符
[code]@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[/code]

[[i] 本帖最后由 netbenton 于 2009-4-28 13:22 编辑 [/i]]

biluncloud 发表于 2011-12-22 14:23

[quote]再来一个更简单的(no findstr  no 逐字符 no tempfile no 变量延迟):


[ 本帖最后由 batman 于 2008-8 ...
[size=2][color=#999999]batman 发表于 2008-8-2 09:15[/color] [url=http://www.bathome.net/redirect.php?goto=findpost&pid=6329&ptid=1289][img]http://www.bathome.net/images/common/back.gif[/img][/url][/size][/quote]


    看练习题的时候看到这道题,然后看到版主竟然使用如此精妙的方法来处理,激动之余冲上来顶一下,太强大了

喃喃努努 发表于 2012-5-20 23:06

不懂。。。

designTech 发表于 2015-12-29 12:14

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=27966&ptid=1289]13#[/url] [i]netbenton[/i] [/b]


    如果行尾有tab或者空格时,程序就不行了

codegay 发表于 2016-5-2 04:58

[code]"""
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][/code]

taofan712 发表于 2017-3-1 11:03

[i=s] 本帖最后由 taofan712 于 2017-3-1 11:06 编辑 [/i]

[code]
@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[/code]取近似值做的,平均每个单词4个字母。效果不错。
重要的是我想借此问一下,我第八行set /a num=num/8为什么不能跟第七行写一起set /a num=(80-n*4)/8 会报错。

qixiaobin0715 发表于 2021-3-20 10:41

[code]@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[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.