返回列表 发帖
原帖由 风行者 于 2008-7-26 20:03 发表
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1-3 delims= " %%a in (1.txt) do (
set /a n+=1
if !n!==1 echo %%a      %%b   %%c
if !n!==2 echo %%a         %%b  %%c
if !n!==3 echo %%a   ...

这种解法的确是太可爱了,100%的就题解题!
***共同提高***

TOP

使用tab键的都有哪些条件呢?11楼的测试文件不能成功,如果两行差距太大也不成功.

TOP

  默认情况下,一个tab键产生的空格相当于8个空格键产生的空格,如果字符串少于8个字符,则一个tab键就足够排版;如果多于8个字符,那么,一个tab键就没法排版了,需要增加tab键的个数,以保证tab键产生的空格个数多于字符串长度。
1

评分人数

    • 浅默: 不知最多能给几分PB + 4
尺有所短寸有所长,学好批处理没商量;
考虑问题复杂化,解决问题简洁化。

心在天山,身老沧州。

TOP

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2*" %%a in ('type temp.txt') do (
   set "mo=%%a"&set "niu=%%b"&set "m=0"&set "n=0"&set "very="
   call :ming0 %%a %%b %%c
)
echo.&pause&exit
:ming0
if not "!mo:~%m%,1!"=="" (set "momo=!mo:~%m%,1!"&set /a "m+=1"&goto :ming0)
:ming1
if not "!niu:~%n%,1!"=="" (set "niuniu=!niu:~%n%,1!"&set /a "n+=1"&goto :ming1)
set /a "ming=30-%m%-%n%"
for /l %%i in (1 1 %ming%) do set "very=!very! "
echo %1 %very% %2 %3COPY
2

评分人数

TOP

我也行!

@echo off
setlocal enabledelayedexpansion
set a=0
for /f "eol= tokens=1,2,3" %%a in ('type 1.txt') do (
set /a a+=1
set "lie1=%%a                                      "
set lie1=!lie1:~0,30!
set "lie2=%%b                                      "
set lie2=!lie2:~0,20!
echo,!lie1!!lie2:%%b=!%%b  %%c
)
pauseCOPY
1

评分人数

[url=][/url]

TOP

我稍微换一下思路,应该说是尽量的通用了……如果某文本太过畸形 那也是没办法的事了……
文本内容更新为:
111111111111111111111                                      98912 张三
222222222222222222    150020 李四四
  333333333333333333333    360000 王五
4444444444444444444321542365445432453243243423432   11111143242342332 赵六六
  555555555555555555    222 田七
666666666666666666666    999999                                   舞吧COPY
@echo off
mode con cols=100 lines=20
setlocal enabledelayedexpansion
for /l %%a in (1,1,80) do set kong= !kong!
for /f "eol= tokens=1,2,3" %%a in ('type 1.txt') do (
set lie1-2=%%a%%b%kong%
set lie1-2=!lie1-2:~0,80!
set lie1-2=%%a!lie1-2:%%a%%b=!%%b
echo,!lie1-2!   %%c
)
pauseCOPY
原理:例如文本内容为:
ab******1*d
abc*************2********e
用for获得前面的两列
直接组合 则免去了中间的空格:
ab1
abc2
直接加上80个空格
ab1***************************……
abc2***************************……
然后保存前80位。
ab1*********……***
abc2********……***
把空格前移
ab*********……***1
abc********……***2
这样前面的就基本对齐了。后面的第三列 就不用说了…
我觉得应该可以了……至少我自己用不上太通用的,这个代码自己也没有当工具用过




忽忽……加了中文 或者字符……就被淘汰了……

[ 本帖最后由 523066680 于 2008-8-12 15:49 编辑 ]
2

评分人数

[url=][/url]

TOP

我来个  补齐字符方法   效率不怎么样
@echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (1.txt) do call :lp %%i
for /l %%i in (1 1 %t%) do for /l %%j in (1 1 !%%il!) do set "%%iv=!%%iv! "
for /f "tokens=*" %%i in (1.txt) do call :lp0 %%i
pause&exit
:lp0
set/a n+=1
    if "%1"=="" set n=& echo %str1%&set str1=&goto :eof
    set "str=%1!%n%v!"
    call set "str1=%str1%%%str:~,!%n%l!%%"
shift
goto lp0
:lp
set/a n+=1
if not defined %n%l set %n%l=0
    if "%1"=="" set n=&goto :eof
    for /f "skip=1 delims=:" %%i in ('^(echo %1^&echo.^)^|findstr /o ".*"') do set m=%%i
    set t=%n%
    if %m% gtr !%n%l! set %n%l=%m%
shift
goto lpCOPY

TOP

不知此帖是否够通用?不过创建了临时文件。
http://bbs.bathome.net/viewthrea ... mp;highlight=%C5%C5
技术问题请到论坛发帖求助!

TOP

新接触批处理,很有意思,请各位大侠多多指教。在此送上我的代码,多多指教!既然各位大侠都说了就题解题,那不好意思了,嘿嘿……

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1-3" %%i in (test.txt) do (
set temp1=%%i$$$$$&set temp2=$$$$$%%j&set temp3=%%k$$$$$
set temp1=!temp1:~0,21!&set temp2=!temp2:~-6!&set temp3=!temp3:~0,3!
set tempstr=!temp1!      !temp2! !temp3!
set tempstr=!tempstr:$= !
echo !tempstr!
)COPY

[ 本帖最后由 weichenxiehou 于 2010-12-30 23:37 编辑 ]
看得多说得多,远比不上写得多。

TOP

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1-3" %%a in (1.txt) do (
set str=%%a%%b
call:count
set a=%%a
set /a num=32-!num!
set/p=!a!<nul
for /l %%i in (1 1 !num!) do (set /p= <nul)
echo %%b %%c
)
pause>nul
:count
if defined num set num=
:loop
if not "!str!"=="" (
set str=!str:~1!
set /a num+=1
goto loop
) COPY
only the strong survive!

TOP

下载了10楼附件,可是还是没效果啊,莫非win7不支持这样么...

TOP

@echo off  
setlocal EnableDelayedExpansion
set "space= "
for /f "tokens=1,2*" %%a in (1.txt) do (
    set "str1=%%a"& set /a n=0
    set /a s=1 &  call :1
    set "x=!str1!!str2!" & set str2=
    set "str1=%%b" & set /a n=0
    set /a s=2 & call :1
    set "y=!str2!!str1!" & set str2=
    set "str1=%%c" & set /a n=0
    set /a s=3 & call :1
    set "z=!str2!!str1!" & set str2=
    echo !x!!y!!z!
)
pause>nul
goto :eof
:1
if not "!str1:~%n%,1!"=="" set /a n+=1 & goto :1
if %s% equ 1 (set /a m=26-%n% & goto :2)
if %s% equ 2 (set /a m=10-%n% & goto :2)
set /a m=10
:2
for /l %%i in (1,1,!m!) do set "str2=!str2!%space%"
goto :eof COPY
效果见下图:

TOP

@Echo Off
SetLocal EnableDelayedExpansion
For /F "Tokens=1,2,3" %%i In (1.txt) Do (
Set L1=%%i                    
Set L2=                    %%j
Echo !L1:~0,25!!L2:~-15! %%k
)COPY

TOP

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims= " %%a in (a.txt) do (
set k=%%a
set k1=%%b
    for /l %%c in (1,1,25) do if "!k:~%%c,1!"=="" set "k=!k! "
    for /l %%d in (1,1,5) do if "!k1:~%%d,1!"=="" set "k1=!k1! "
echo;!k! !k1! %%c
)
pauseCOPY
心累~努力,为了美好的明天。

TOP

# python版实现文本左右对齐排版
# 题目来源: http://www.bathome.net/thread-1246-1-1.html
# 依山居 7:17 2015/11/4
# str.format 字符串格式化参考:
# http://www.crifan.com/python_string_format_fill_with_chars_and_set_alignment/
# just()字符串对齐 参考:
# http://blog.csdn.net/u012515223/article/details/20463231
'''
1.txt内容:
111111111111111111111    98912 张三
222222222222222222    150020 李四四
  333333333333333333333    360000 王五
444444444444444444    2332 赵六六
  555555555555555555    222 田七
666666666666666666666    999999 舞吧
'''
#说明我的代码没按题目要求全部使用空格填充是为了让代码用法看起来直观一些。
#方法一 format格式化对齐
def f1():   
    with open("1.txt","r") as f:
        for s in f:
            l=s.rsplit ()
            t='{0:-<25} {1: >7} {2}'.format(l[0],l[1],l[2])   
            print(str(t))
'''
输出:
111111111111111111111----   98912 张三
222222222222222222-------  150020 李四四
333333333333333333333----  360000 王五
444444444444444444-------    2332 赵六六
555555555555555555-------     222 田七
666666666666666666666----  999999 舞吧
'''
#方法2 使用just()对齐
r=''
def f2():
    f=open("1.txt","r")
    for s in f:
        l=s.rsplit()
        print(l[0].ljust(25," "),l[1].rjust(10,"^"),l[2])
'''
输出:
111111111111111111111     ^^^^^98912 张三
222222222222222222        ^^^^150020 李四四
333333333333333333333     ^^^^360000 王五
444444444444444444        ^^^^^^2332 赵六六
555555555555555555        ^^^^^^^222 田七
666666666666666666666     ^^^^999999 舞吧
'''
#方法3
'''
思路是rsplit拆分成三列,l得到如['111111111111111111111', '98912', '张三']
算出列1,列2的最长,以此算出需要填充的长度,
'''
def f3():
    f=open("1.txt","r")
    txt=f.readlines()
    maxa=0
    maxb=0
    for line in txt:
        line=line.rsplit()
        la=len(line[0])
        lb=len(line[1])
        if la >maxa:
            maxa=la
        if lb > maxb:
            maxb=lb
    for line in txt:      
        line=line.rsplit()
        la=len(line[0])
        lb=len(line[1])
        if la ==maxa:
            tla=line[0]
        elif la<maxa:
            tla=line[0]+">"*(maxa-la)
        if lb ==maxb:
            tlb=line[1]
        elif lb<maxb:
            tlb=" "*(maxb-lb)+line[1]
        print(tla,tlb,line[2])
'''
输出:
111111111111111111111  98912 张三
222222222222222222>>> 150020 李四四
333333333333333333333 360000 王五
444444444444444444>>>   2332 赵六六
555555555555555555>>>    222 田七
666666666666666666666 999999 舞吧
'''COPY

TOP

返回列表