在批处理之家论坛下载的一份批处理练习题集合,新手练手用的。只完成了前面19题,后面没精力去做了....
第1题
题目:批处理实现两文本同行交替输出
有两个文本如下(实际中并不知道两文本各有多少行):
文本1.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ccccccccccccccccccccccccccccccccccccccc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ggggggggggggggggggggggggggggggggggggggg
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
文本2.txt
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
要求用批处理输出如下(两文本交替输出):
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
ccccccccccccccccccccccccccccccccccccccc
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
ggggggggggggggggggggggggggggggggggggggg
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
要求:
1 尽量不生成临时文件
2 代码要高效
3 代码尽量简洁
目的:
唯一的目的在于共同提高!
答案:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type 1.txt') do set/a n1+=1&set 1_!n1!=%%a
for /f "delims=" %%b in ('type 2.txt') do set/a n2+=1&set 2_!n2!=%%b
if "%n1%" gtr "%n2%" (set n=%n1%) else (set n=%n2%)
for /l %%i in (1,1,%n%) do (
if defined 1_%%i echo !1_%%i!
if defined 2_%%i echo !2_%%i!
)
第2题
题目:批处理查找字符数最多的文本行
有文本a.txt如下:
aaaaaaa aaaa bbbbbb ccccccccccc dddd
aa aaaaaaa bbbbbbbb cccccccccc ddddddddddddd eeeeeee
aaaaaaaaaaaa bbbbbbbbbbb cccccccccccccccccccc
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aa
aaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbb
aaaaaaaaa ccccccccc bbbbbbbbbbbbb ddddddddddddddddd
通过批处理查找出文本中字符数(不含空格)最多的行并输出,很明显就是:
aaaaaaaaa ccccccccc bbbbbbbbbbbbb ddddddddddddddddd
要求:
1 不生成临时文件
2 代码简洁,高效,通用性好
3 加分仍以思路为重
答案:
@echo off&setlocal enabledelayedexpansion
set m=0
for /f "delims=" %%a in ('type a.txt') do (
set s0=%%a
set s1=!s0: =!
set n=0
call :1
)
echo %maxstr%
pause&exit
:1
set s1=%s1:~0,-1%
set/a n+=1
if defined s1 goto :1
if "%n%" gtr "%m%" set maxstr=%s0%
goto :eof
第3题
题目:文本左右对齐排版
1.txt内容如下:
111111111111111111111 98912 张三
222222222222222222 150020 李四四
333333333333333333333 360000 王五
444444444444444444 2332 赵六六
555555555555555555 222 田七
666666666666666666666 999999 舞吧
通过批处理输出为:
111111111111111111111 98912 张三
222222222222222222 150020 李四四
333333333333333333333 360000 王五
444444444444444444 2332 赵六六
555555555555555555 222 田七
666666666666666666666 999999 舞吧
要求:
不生成任何临时文件。
代码简洁,视思路与代码加分!
本题只是给出一个简单的文本通过批处理排版,而要求代码的通用,正如batman版主所说,会是一个繁琐的过程,于是就题解题,不必考虑代码的通用。
答案1:
@echo off&setlocal enabledelayedexpansion
set kong40=
set kong10=
for /f "tokens=1-3 delims= " %%a in ('type 1.txt') do (
set str1=%%a%kong40%a
set str2=%kong10%%%b
echo !str1:~0,20!!str2:~-13,13! %%c
)
答案2:@523066680
@echo off&setlocal enabledelayedexpansion
for /l %%a in (0,1,39) do set kong= !kong!
for /f "tokens=1-3" %%i in ('type 1.txt') do (
set str=%%i%%j%kong%
set str=!str:~0,40!
set str=!str:%%i%%j=!
echo %%i!str!%%j %%k
)
第4题
题目:批处理重命名特殊文件名
文件夹下有三个由字母、数字、汉字、特殊字符组成文件名的文件同时批处理自身也在其中:
一b!a!1235@_ s%#8i 三 c.rar
h^e12 0~%l%!p!@. s321-的文件.txt
%o%%% !O!ffi9~ ce$- Wor8d29 文档.docx
重命名.bat
要求运行批处理将这三个文件文件名中的数字、汉字、特殊字符全部(含空格)去掉,而重命名为只含字母的文件,但后缀名不能更改,同时批处理自身名字不能更改,重命名后:
basic.rar
helps.txt
oOfficeWor.docx
重命名.bat
要求:代码简洁、通用、效率不至于太低。
答案:
@echo off
for %%a in (*.*) do (
if "%%a" neq "1.bat" (
set "str=%%~na"
set "oldname=%%~nxa"
call :lp %%~xa
)
)
pause&exit
:lp
if "%str:~0,1%" geq "a" (
if "%str:~0,1%" leq "Z" (
set newname=%newname%%str:~0,1%
)
)
set str=%str:~1%
if defined str goto:lp
ren "%oldname%" "%newname%%1"
set newname=
goto :eof
第5题
题目:批处理解约瑟夫环应用题
有二十九个女生(分别用1-29号来称呼)围成一圈玩报数游戏,规则是这样的:从1开始数数,当数到3的这个人就退出游戏,而她后面的人接着从1数。。。如此一直到最后剩下一个人,现在知道最初是从13号女生开始的游戏,问最后剩下的会是第几号女生?
要求:
1 用批处理解答
2 代码简洁高效
3 代码通用且不生成临时文件
加分原则:
以思路为重(如思路独特,请简要说明)
完全符合要求的加10分
--------------------------------------------------
已有两套解决方案,见3楼more和6楼ieutk版主的代码,但个人认为这两套方案均不完美(见本人的跟贴评述),期
待完美方案的出现,大家加油了!!!
注:约瑟夫问题
约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉。例如N=6,M=5,被杀掉的人的序号为5,4,6,2,3。最后剩下1号。
答案1(@batman):
@echo off&setlocal enabledelayedexpansion
for /l %%a in (13,1,29) do set "str=!str! #%%a#"
for /l %%a in (1,1,12) do set "str=!str! #%%a#"
:lp
for %%a in (!str!) do (
set /a n+=1
if !n! equ 3 set "str=!str: %%a=!"&set /a n=0
)
for /f "tokens=2" %%a in ("%str%") do if "%%a" neq "" goto lp
echo 最后剩下的是%str:#=%号&pause>nul
答案2:(@more)
@echo off
set "str=13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 1 2 3 4 5 6 7 8 9 10 11 12"
:again
for /f "tokens=1,2,4*" %%a in ("%str%") do (
if not "%%c"=="" (set "str=%%c %%d %%a %%b"&goto :again) else (echo %%b&pause&exit)
)
第6题
题目:批处理实现文本内容居中显示
[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.
答案:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=2" %%i in ('mode^|find "列"') do (set col=%%i)
for /f "delims=" %%a in ('type a.txt') do (set "str=%%a"&call :1 "%%a")
pause>nul&exit
:1
set str=%str:~0,-1%
set/a n+=1
if defined str goto:1
set/a m=%col%-%n%
set/a m=%m%/2
set n=0
for /l %%i in (1,1,%m%) do (set kong= !kong!)
echo %kong%%~1
set kong=
第7题
题目:批处理去除指定字符前所有的0
如文本1.txt:
0000acb0h
0b0c00a000s
0000h00ga00
a0000000
通过批处理将文本所有字符串字符a前的0去除输出如下:
acb0h
bca000s
hga00
a0000000
要求:代码简洁、高效、通用,不生成临时文件
答案:
@echo off&setlocal enabledelayedexpansion
cd /d d:\test
for /f "tokens=1,2 delims=a" %%a in (a.txt) do (
if not "%%b"=="" (
set "str=%%a"
set str=!str:0=!
echo !str!a%%b
) else (
echo a%%a
)
)
第8题
题目:批处理实现文本内容横列变纵列
a.txt 每行的字符数都一样,但未知多少个字符。
注意,文本第三行r之后有一个空格。第四行x之后有一个空格。
ab cde f
ghi j k l
m n op qr
st uv wx
输出为:
a ms
bg t
hn
i
ou
c pv
dj
e
kqw
rx
fl
即横列变纵列,并保留原来空格。
要求,高效,不生成临时文件。
答案:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
set str=%%a
for /l %%i in (0,1,10) do (set l_%%i=!l_%%i!!str:~%%i,1!)
)
for /l %%i in (0,1,10) do (echo !l_%%i!)
pause
第9题
题目:数值排序问题
a.txt中有20行随机产生的数列,如下:
2928326128601232462131283250710027308938740594716691200992050511576
5352129649530193383124730478244772348721985707222557212265817305
217141333532296179938475175265792931789219830308392472584606305
2371620291160322081050531817416284225477019123161801285941026814244
283929972304551060318886921731765136928849135391662294051194618754
1809165929787147057932949630411324311737224509104016550662932273
27396236084901303873154718299242931819623155304661177528921164510335
254221462410491137971033914630292752245114969186002809930190939425
1085287492160525651862932475207612387312368408826675135332406418337
2567810118246621010283281198810903279355871571118961177731143829148
23727111515524141721964179351992331180134926914198081871053303186
378579502856625703213542353218420835730692264021219729654278515442
30215186011014395001656818458819061824708536511543271701327524725
223702764213159156022932717903282522044350522584222768193271431422354
3079720530119542370417125702274761144023302102641160114921224469221
15642298214000242538839193816839550322381321993212316517861828002
13042178002978222022331319116624809338275899045263351248023569
11252165681825711849278422768716060438517976169102391532289954712000
105084292396529699311371735329685626410510259482788519645152723476
29674179062831103792824121564178225289202161443911094228581583531951
要求通过纯批处理按照数列的大小顺序,正序输出如下:
13042178002978222022331319116624809338275899045263351248023569
217141333532296179938475175265792931789219830308392472584606305
1809165929787147057932949630411324311737224509104016550662932273
5352129649530193383124730478244772348721985707222557212265817305
15642298214000242538839193816839550322381321993212316517861828002
23727111515524141721964179351992331180134926914198081871053303186
30215186011014395001656818458819061824708536511543271701327524725
105084292396529699311371735329685626410510259482788519645152723476
254221462410491137971033914630292752245114969186002809930190939425
283929972304551060318886921731765136928849135391662294051194618754
378579502856625703213542353218420835730692264021219729654278515442
1085287492160525651862932475207612387312368408826675135332406418337
2371620291160322081050531817416284225477019123161801285941026814244
2567810118246621010283281198810903279355871571118961177731143829148
2928326128601232462131283250710027308938740594716691200992050511576
3079720530119542370417125702274761144023302102641160114921224469221
11252165681825711849278422768716060438517976169102391532289954712000
27396236084901303873154718299242931819623155304661177528921164510335
29674179062831103792824121564178225289202161443911094228581583531951
223702764213159156022932717903282522044350522584222768193271431422354
每行的数值都远远超过了cmd所能计算的最大数值。
要求,正确输出,不生成临时文件,代码高效通用。完成题目,视思路加分。
答案:(@batman)
@echo off&setlocal enabledelayedexpansion
for /l %%i in (1,1,80) do set "kong=!kong!#"
for /f %%i in (1.txt) do (
set "str=%%i%kong%"
set "a=!str:~,80!"
set "a=!a:%%i=!"
set "_!a!%%i=a"
)
for /f "delims==_" %%i in ('set _') do (
set "str=%%i"
echo !str:#=!
)
pause>nul
PS:这个想不出来怎么做,看了答案,挑了一个大概能明白意思的答案。
第10题
题目:文本最末列数值乘一个数
a.txt
1##4072.0###52.19### #72## #
2#38705.8# # # 51.73## #70# #
372###389.0##51.80##19.77# # 72#####
4##59.71#3715.45##71######
以#和空格作为分隔符,最后一列的数值乘以7,其他不变。已知的条件,数字是随机的,最末列的数值是整数,即使乘以7,仍在CMD所能计算范围内。#与空格与“.”的位置和数量与每行的列数都是随机的。
输出如下:
1##4072.0###52.19### #504## #
2#38705.8# # # 51.73## #490# #
372###389.0##51.80##19.77# # 504#####
4##59.71#3715.45##497######
只是对最末列的数值进行运算操作,其他不变。
要求通用,高效。
答案1:(@batman)
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in (a.txt) do (
set str=%%i
set kaishi=yes
call :1
echo !str!!nums!!var!
set nums=
set var=
)
pause
:1
if "%str:~-1%" geq "0" (
if "%str:~-1%" leq "9" (
set shuzi=yes
set kaishi=no
)
)
if "%shuzi%" == "yes" (
set nums=%str:~-1%!nums!
set shuzi=no
) else (
if "%kaishi%" == "yes" (
set var=%str:~-1%!var!
) else (
set /a nums=nums*7
goto :eof
)
)
set str=%str:~0,-1%
goto :1
看了答案,还是@batman的比较容易读懂,然后按照思路,自己写成我比较容易读懂答案。答题思路是:设置一个变量kaishi,如果循环找到的是非数字,就设置一个字符变量var。如果循环找到数字后,改变kaishi的值,并设置变量nums记录数字的值,并继续下一次循环,直到找到非数字后,计算nums×7再回到call的地方显示结果。。有点绕,想了好久才明白这个逻辑。后面的题目感觉越来越难了。
第11题
题目:批处理按要求将字符串分段输出
一、要求用批处理随机输出200个字符到1.txt中的一行,内容类同如下:
xh45q3ma+remgofm54sevhrna4g5r8pl9cjardezqjwj3m8itamh0a4itzd6jz8cmrfmibmhr0wmccyb8qnp2qh4rvlwff6yr2ez4eo063u6viy7ppw+nzxaxe8vrm190eei_0dwx5e2kxbds-ae6e96c9_i9glw8mz+uf6uienx9od8bktfnjlwlqu-e1rcj33_+4bz
二、要求用批处理将1.txt中的内容按10个字符为一段输出如下面两种形式:
在一行内输出:
xh45q3ma+r emgofm54se vhrna4g5r8 pl9cjardez qjwj3m8ita mh0a4itzd6 jz8cmrfmib mhr0wmccyb 8qnp2qh4rv lwff6yr2ez 4eo063u6vi y7ppw+nzxa xe8vrm190e ei_0dwx5e2 kxbds-ae6e 96c9_i9glw 8mz+uf6uie nx9od8bktf njlwlqu-e1 rcj33_+4bz
分行输出:
xh45q3ma+r
emgofm54se
vhrna4g5r8
pl9cjardez
qjwj3m8ita
mh0a4itzd6
jz8cmrfmib
mhr0wmccyb
8qnp2qh4rv
lwff6yr2ez
4eo063u6vi
y7ppw+nzxa
xe8vrm190e
ei_0dwx5e2
kxbds-ae6e
96c9_i9glw
8mz+uf6uie
nx9od8bktf
njlwlqu-e1
rcj33_+4bz
答案:
@echo off&setlocal enabledelayedexpansion
set zifu=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%a in (0,1,199) do (
set/a aa=!RANDOM!%%35
call set str=%%str%%%%zifu:~!aa!,1%%
)
echo 分行输出:
:1
echo %str:~0,10%
set str1=%str1%%str:~0,10%
set str=%str:~10%
if defined str goto:1
echo;&echo 同行输出:
echo %str1%
第12题
题目:批处理实现一行内容分行输出
a.txt:
aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九
输出如下:
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
唯一要求,老手略过,留给新手做练习,不能直接echo。
请先发现其中的规律。
其他,生成临时文件、代码超过一页都不重要,正确输出即可。
答案(@netbenton)
@echo off&setlocal enabledelayedexpansion
set zifu=aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九
for /l %%a in (0,1,3) do (
for /l %%i in (%%a,4,35) do (
set /p=!zifu:~%%i,1!<nul
)
echo;
)
一开始写了一段,能实现需求,但后来看了@netbenton的答案,觉得这个才是最佳的方法。
第13题
题目:将字符串小写转大写并延时输出的批处理
题目如下
有一字符串i like the bathome,because here is the batch of the world.
要求用批处理将字符串的所有字母转成大写并在屏幕上以打字的效果延时输出。
答案:
@echo off&setlocal enabledelayedexpansion
set "str=i like the bathome,because here is the batch of the world."
set "daxie=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"
for %%a in (%daxie%) do (set str=!str:%%a=%%a!)
:1
set /p p="%str:~0,1%"<nul
set str=%str:~1%
for /l %%i in (1,1,800) do echo>nul
if defined str goto :1
思路是利用字符替换不区分大小写的方法。
第14题
题目:出给新手的数值排序加分题
给出任意一组数列,正序输出,如:
3 2008 11 19 777 23 2014 453 789 51
输出为:3 11 19 23 51 453 777 789 2008 2014
说明:
不必考虑特大数,所给的数列中都是整数值。
答案:
@echo off&setlocal enabledelayedexpansion
set "str=3 2008 11 19 777 23 2014 453 789 51"
for %%a in (%str%) do (
set m=10
for %%i in (%str%) do (
if %%a lss %%i set /a m-=1
)
set s_!m!=%%a
)
for /l %%r in (1,1,10) do set /p=!s_%%r! <nul
思路是通过逐一比较,得出当前数字在字符串中的排序位置。
第15题
题目:批处理随机取值练习
题目如下:
已知有1-26与a-z的一一对应,如1对应a 10对应j 26对应z,要求用批处理从1-26,a-z这52个值中(全视为值)随机抽取10个互不相同的值并显示出来,注意如同1和a 24和x 26和z这样的取值均视为相同不合要求。
解题限制:
暂无限制。
答案:
@echo off&setlocal enabledelayedexpansion
set zimu=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
set/a n=26,m=0
for /l %%a in (1,1,26) do (set str_%%a=%%a)
for %%a in (%zimu%) do (set/a n+=1&set str_!n!=%%a)
:lp
set/a suiji=%random%%%52+1
set/a ss=%suiji%-26
if defined _!ss! goto :lp
if "%suiji%" gtr "26" (
set /p=!str_%suiji%! <nul
set _!ss!=1
) else (
set /p=%suiji% <nul
set _!ss!=1
)
set/a m+=1
if "%m%" equ "10" pause&exit
goto :lp
思路:建立52个变量并一一对应各自的值,然后通过随机1-52并读取出里面的值。
第16题
题目:批处理判断字符串长度
习题:
1.1 字符串长度不超过80。不含特殊字符
例 i like the bathome,because here is the batch of the world.
1.2 字符串长度小于255,含有特殊字符,试求其长度。
例
%%a%%!ver!&^^^ ^*~ are you o k ?" : \ / ` ` verfdxcweippo opj x ds gw !%%
一般的,先求1.1的题目。看谁的代码灵活、简洁、思路清晰。然后再做1.2(汗。中文不用考虑……)
其实我也是想征集一个比较好的办法……估计如果有中文的话。或许可以重定向到文本然后再判断字节吧。我猜可以的。
最后:一题多解,可别只用 重定向文本 那个方法哈。
答案@shqf
1.1略过,重点看了1.2。可是1.2太难了,不会。但是看到一个答案很牛,虽然能看懂,但是没明白原理。
::%%a%%!ver!&^^^ ^*~ are you o k ?" : \ / ` ` verfdxcweippo opj x ds gw !%%
@echo off&setlocal enabledelayedexpansion
set /p str=<%0
for /l %%a in (1,1,255) do (
set str=!str:~1!
if "!str!"=="" (
set /a totle=%%a-2
echo !totle!
goto :end
)
)
:end
pause
实在没搞懂,为什么这样能处理特殊字符。
我自己的思路是,将特殊字符写入文本。然后在末尾添加空行计算出字符偏移量,从而得出字符个数。
最后经@bat-pcl_test的指导明白了<并不对文件内容做检测,只管输入,所以可以处理特殊符号。
第16题
题目:批处理根据输入值进行数值计算
通过批处理实现如下操作(要求适应于1-9的输入数)
请输入要操作的数(1-9):4
34的平方:16
34的立方:64
从1到34的和:10
从1到34的积:24
从1到34的平方和:34
从1到34的立方和:100
4行4列的数列:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
4行4列的螺纹数列:
答案:
@echo off&setlocal enabledelayedexpansion
set /p "p=请输入要操作的数(1-9):"
set j=1
for /l %%a in (1,1,%p%) do (
set /a p2=%%a*%%a
set /a p3=%%a*%%a*%%a
set /a h=!h!+%%a
set /a j=%%a*!j!
set /a ph=!ph!+!p2!
set /a lh=!lh!+!p3!
)
echo %p%的平方:%p2%
echo %p%的立方:%p3%
echo 从1到%p%的和:%h%
echo 从1到%p%的积:%j%
echo 从1到%p%的平方和:%ph%
echo 从1到%p%的立方和:%lh%
数列:
@echo off&setlocal enabledelayedexpansion
set /p "p=请输入要操作的数(1-9):"
for /l %%a in (1,1,%p%) do (
for /l %%i in (1,1,%p%) do (
set /a n+=1
set /p=!n! <nul
)
echo;
)
螺纹:
@echo off&setlocal enabledelayedexpansion
set /p p=输入:
set /a x=1,num=%p%,sum=%p%*%p%
set yuns=--
for /l %%y in (1,1,%num%) do (set/a n+=1,y+=1&set _%x%_%%y=!n!)
:lp
set /a num-=1
for /l %%a in (1,1,%num%) do (set/a n+=1,x=!x!%yuns%1&set _!x!_!y!=!n!)
set yuns=-%yuns%
for /l %%a in (1,1,%num%) do (set/a n+=1,y=!y!%yuns%1,ys+=1&set _!x!_!y!=!n!)
if not %n% equ %sum% goto :lp
for /l %%a in (1,1,%p%) do (
for /l %%b in (1,1,%p%) do (
set /p=!_%%a_%%b! <nul
)
echo;
)
pause
第18题
题目:按要求整理并改写文本
有文本a.txt
Network vertically: In the research phase of your job hunt,talk to people who are on a level above you in your desired industry. They'll have some insights that people at your own level won't have,and will be in a good position to hire you or recommend you to be hired. Keep your spirits up. Looking for a job is one of the toughest things you will ever have to do. Maintain your confidence,stay persistent,and think positively,and eventually you will get a job that suits you.
要求:
- 通过批处理将文本中的you全替换成me并输出,但your不能变
- 通过批处理将文本整理成如下格式(遇:号和.号换行并在行首空两格),并生成文本b.txt:
- 将文本b.txt中的Looking for a job is one of the toughest things me will ever have to do.这句改写成bathomework is a nice job,其它的不变。
Network vertically:
In the research phase of your job hunt,talk to people who are on a level above me in your desired industry.
They'll have some insights that people at your own level won't have,and will be in a good position to hire me or recommend me to be hired.
Keep your spirits up.
Looking for a job is one of the toughest things me will ever have to do.
Maintain your confidence,stay persistent,and think positively,and eventually me will get a job that suits me.
补充两点:
- a.txt中只有一行
- 解题中不使用第三方工具(并不是提倡不使用第三方)
答案:
@echo off&setlocal enabledelayedexpansion
set /p str=<a.txt
set str=%str: you= me%
set str=%str: mer= your%
for /f "tokens=1* delims=:" %%a in ("%str%") do (echo %%a:>>c.txt&set str2=%%b)
:2
for /f "tokens=1* delims=." %%a in ("%str2%") do (
set aaa=%%a
set aaa=!aaa:Looking for a job is one of the toughest things me will ever have to do=bathomework is a nice job!
if not "!aaa:~1,1!" == "" echo; !aaa!.>>c.txt
set str2=%%b
goto :2
)
pause
看了答案后,发现还有一个更牛叉的替换方法,学了一招。
@随风
@echo off&setlocal enabledelayedexpansion
set /p str=<a.txt
set str=%str: you= me%
set str=%str: mer= your%
set "str=%str::=:&echo; %"
set "str=%str:.=.&echo; %"
set "str=%str:Looking for a job is one of the toughest things me will ever have to do=bathomework is a nice job%"
echo %str%
pause
暂时做到这里吧。后面的题目越来越难了!
网友评论