【练习-012】批处理实现一行内容分行输出
a.txt:[code]aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九[/code]输出如下:[code]
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九[/code]唯一要求,老手略过,留给新手做练习,不能直接echo。
请先发现其中的规律。
其他,生成临时文件、代码超过一页都不重要,正确输出即可。
[[i] 本帖最后由 pusofalse 于 2008-8-12 20:27 编辑 [/i]] 这题确实值得新手们好好思考一下了,找中入手点是解此题的关键。
第一个想到的思路……
@echo offsetlocal enabledelayedexpansion
set a=-1
for /f "tokens=*" %%a in ('type 1.x') do set hang=%%a
:a
set /a a+=1
set /a b=%a%
:b
set hang%a%=!hang%a%!!hang:~%b%,1!
set /a b=%b%+4
if not "!hang:~%b%,1!"=="" goto :b
echo,!hang%a%!
if %a% leq 2 goto :a
pause
这个思路很直接,没啥特别之处,就是设
第一行为1,5,9,13,……位上的
第二行为2,6,10,14……位上的
第……
[[i] 本帖最后由 523066680 于 2008-8-12 21:20 编辑 [/i]] set/a的表达式。。。可以简写为:
set/a a+=1,b=a
... ...
set /a b+=4 支持一题多解,大家多动动脑筋了,呵呵
[[i] 本帖最后由 batman 于 2008-8-12 22:34 编辑 [/i]] 我唯一的要求被无视了啊。。
这题的要求为何如此之松。 凑个热闹
[color=white][code]@echo off&setlocal EnableDelayedExpansion
set "str=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
for /l %%a in (0 1 35) do (
set /a n+=1
call set .!n!=%%.!n!%%!str:~%%a,1!
set /a n=n%%4
)
for /f "tokens=2 delims==" %%a in ('set .') do echo %%a
pause>nul[/code][/color] 我也凑个闹热
[color=White][code]@echo off&setlocal EnableDelayedExpansion
set "str=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
for /l %%i in (0 1 3) do for /l %%j in (%%i 4 35) do set %%i=!%%i!!str:~%%j,1!
for /l %%i in (0 1 3) do echo !%%i!
pause>nul[/code][/color]
刷多两次……积分
没有独到之处……@echo off&setlocal enabledelayedexpansion
set /p hang=<1.x
set a=0
:a
set h%a%=!h%a%!!hang:~%a%,1!
if %a% leq 3 (set /a a+=1) else (set /a a=0 &set hang=%hang:~4%)
if defined hang goto :a
for /l %%a in (0,1,3) do echo !h%%a!
pause
---------------------------2-----------------------------------------------
@echo off
set/p hang=<1.x
set /a a=-1
:a
set /a a+=1 &call,set /a b=%%a%%+32
for /l %%a in (%a%,4,%b%) do (
call set h=%%h%%%%hang:~%%a,1%%)
echo,%h%
if %a% lss 3 set h=&goto :a
pause>nul 试写了一段代码,分享分享,请老手们多多指教。。。[code]@ echo off
setlocal ENABLEDELAYEDEXPANSION
set "x=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
set y=0
set s=0
: start
del /q /f q%s%.txt>nul 2>nul
: loop
set /a y+=4
if %y% ==36 set y=1 & call :dsp & call q.bat>dsp.txt & set s=1& goto start
if %y% ==37 set y=2 & call :dsp & call q.bat>>dsp.txt & set s=2& goto start
if %y% ==38 set y=3 & call :dsp & call q.bat>>dsp.txt & set s=3& goto start
if %y% ==39 set y=4 & call :dsp & call q.bat>>dsp.txt & set s=0& goto end
echo !x:~%y%,1!>>q%s%.txt
goto loop
:dsp
echo @echo off>q.bat
if %y%==1 echo echo a^^>>q.bat
if %y%==2 echo echo A^^>>q.bat
if %y%==3 echo echo 1^^>>q.bat
if %y%==4 echo echo 一^^>>q.bat
for /f "tokens=1 delims=" %%i in (q%s%.txt) do echo %%i^^>>q.bat
goto :eof
: end
type dsp.txt
pause>nul 2>nul[/code] 再次修改:
[code]
@echo off
set "str=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
:lp
call,set "var=%%str:~%n%,1%%"
call,set "str2=%%str2%%%var%"
set /a n+=4
if not defined var (echo %str2%) else goto lp
call,set "str=%%str:~1%%"&set n=0&set "str2="
set /a m+=1
if %m%==4 pause&exit
goto lp
[/code]
[[i] 本帖最后由 keen 于 2009-5-8 22:47 编辑 [/i]]
练练手
[code]@echo off&setlocal ENABLEDELAYEDEXPANSIONset "x=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
for /l %%a in (0,1,3) do ((for /l %%b in (%%a,4,35) do set/p=!x:~%%b,1!<nul)&echo.)
pause[/code]
[[i] 本帖最后由 netbenton 于 2009-5-3 23:32 编辑 [/i]] 刷变量方式[code]@echo off&setlocal enabledelayedexpansion
set str=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九
for /l %%i in (0,1,35) do (
set/a n+=1
call set st!n!=%%st!n!%%!str:~%%i,1!&if !n! equ 4 set n=0
)
for /l %%i in (1,1,4) do echo !st%%i!
pause[/code] [code]@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
set k=%%a
for /l %%b in (0,1,80) do (
set j=!k:~%%b,1!
for %%c in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if !j!==%%c set xiao=!xiao!%%c&set _!j!=1
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if !j!==%%d set da=!da!%%d
for /l %%e in (0,1,9) do if !j!==%%e set shu=!shu!%%e&set _!j!=1
if not defined _!j! set zhong=!zhong!!j!
)
)
echo;!xiao!&echo;!da!&echo;!shu!&echo;!zhong!
pause
[/code] [code]#python版一行内容分行输出
#依山居 18:14 2015/11/4
#题目来源 http://www.bathome.net/thread-1454-1-1.html
a="aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
"""
分行输出为:
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
"""
print("方法一:===============")
for r in range(0,4):
t=''
for s in range(0+r,len(a),4):
t=t+a[s]
print(t)
print("方法二:===============")
#=_=这个方法会不会看起来比较傻?
l=list(a)
ta=tb=tc=td=''
for r in range(0,9):
for s in range(0,4):
if s==0:
ta=ta+l.pop(0)
if s==1:
tb=tb+l.pop(0)
if s==2:
tc=tc+l.pop(0)
if s==3:
td=td+l.pop(0)
print(ta)
print(tb)
print(tc)
print(td)
print("方法3:回字有N种写法===============")
import string
ta=tb=tc=td=''
la=string.ascii_lowercase
ua=string.ascii_uppercase
nb=string.digits
ub="一二三四五六七八九"
for s in a:
if s in la:
ta=ta+s
if s in ua:
tb=tb+s
if s in nb:
tc=tc+s
if s in ub:
td=td+s
print(ta)
print(tb)
print(tc)
print(td)
print("方法4:回字有一种叫做正则的写法===============")
import re
#这正则写法感觉不科学,暂时没有好的想法
reg=["[a-z]","[A-Z]","\d","[^\da-zA-Z]"]
for s in reg:
rega=re.compile(s)
s=re.findall(rega,a)
print("".join(s))
"""
输出:
方法一:===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法二:===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法3:回字有N种写法===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法4:回字有一种叫做正则的写法===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
"""
[/code] [code]print("=======================论回字的第五种写法============")
l=list(a)
ta=tb=tc=td=''
for r in range(0,9):
ta+=l.pop(0)
tb+=l.pop(0)
tc+=l.pop(0)
td+=l.pop(0)
print(ta)
print(tb)
print(tc)
print(td)[/code] [code]print("==========试试回字的第六种写法好不好使==========")
lena=len(a)
t=''
lena=len(a)
for s in range(0,4):
t=''
for l in range(s,lena,4):
t+=a[l]
print(t)[/code] [code]
@echo off &setlocal enabledelayedexpansion
set var=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九&set /a a=0
:loop
for /l %%a in (%a%,4,36) do set/p=!var:~%%a,1!<nul
echo;&if !a! lss 3 set /a a+=1 & goto loop
pause&exit
[/code] [code]
@echo off & setlocal EnableDelayedExpansion
set strs=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九
:loop
set str=!strs:~0,4!
for /l %%i in (0,1,3) do ( set "str%%i=!str%%i!!str:~%%i,1!" )
set strs=!strs:~4!
if defined strs ( goto :loop )
for /l %%i in (0,1,3) do ( echo !str%%i! )
[/code] [code]@echo off
setlocal enabledelayedexpansion
set var=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九
:a
set /a n+=1
set /a m=(n-1)%%4
set str%m%=!str%m%!!var:~%m%,1!
if %m%==3 set var=%var:~4%
if not "%var%"=="" goto a
for /l %%a in (0,1,3) do echo,!str%%a!
pause[/code]
页:
[1]