标题: 【练习-018】按要求整理并改写文本 [打印本页]
作者: batman 时间: 2008-8-30 10:08 标题: 【练习-018】按要求整理并改写文本
出题目的:
掌握字符截取技巧
掌握学符替换技巧
掌握改写文本技巧
解题要求:
代码通用、高效
尽量简洁
不生成临时文件
加分规则:
思路独特基准分5分
代码高效、通用基准分4分
技巧高超基准分3分
代码简洁基准分2分
完美代码加分15分
题目如下:
有文本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.
复制代码
1 要求通过批处理将文本中的you全替换成me并输出,但your不能变
2 要求通过批处理将文本整理成如下格式(遇:号和.号换行并在行首空两格),并生成文本b.txt:- Network vertically:
- In the research phase of your job hunt,talk to people who are on a level abov
- e 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 eventuall
- y me will get a job that suits me.
复制代码
3 要求将文本b.txt中的Looking for a job is one of the toughest things me will ever have to do.
这句改写成bathomework is a nice job,其它的不变。
解题限制:
高手请暂做旁观,请新手老手解答。
补充两点:
1 a.txt中只有一行
2 解题中不使用第三方工具(并不是提倡不使用第三方)
[ 本帖最后由 batman 于 2008-8-30 13:00 编辑 ]
作者: yslyxqysl 时间: 2008-8-30 10:42
- @echo off
- setlocal ENABLEDELAYEDEXPANSION
- for /f "tokens=*" %%i in (a.txt) do (
- set i=%%i
- set i=!i: you = me !
- set i=!i: you.= me.!
- set i=!i: you,= me,!
- set i=!i: you?= me?!
- set i=!i: you^^^!= me^^^!!
- echo !i!>>a.txt
- )
复制代码
[ 本帖最后由 yslyxqysl 于 2008-9-19 20:33 编辑 ]
作者: batman 时间: 2008-8-30 10:51
楼上的出现了书写错误吧:@echo,off
作者: wangwei4106 时间: 2008-8-30 11:04
2楼代码有误,至少最后一个you.就替换不了!
这样应该就可以了!
- @echo off&setlocal enabledelayedexpansion
- for /f "tokens=*" %%i in (a.txt) do (
- set aa=%%i
- set aa=!aa:you=me!
- set aa=!aa:mer=your!
- echo !aa!
- )
- pause>nul
复制代码
呵呵,谢谢batman
为什么我老是出错呢!
[ 本帖最后由 wangwei4106 于 2008-8-30 15:15 编辑 ]
作者: yslyxqysl 时间: 2008-8-30 11:04
加个逗号是为了测试,嘻嘻。
作者: yslyxqysl 时间: 2008-8-30 11:08
当文本出现young的时候,4楼的代码有些麻烦
作者: wangwei4106 时间: 2008-8-30 11:09
郁闷!
呵呵
我也没有想更多了!
谢谢提醒啊,我再去看看!
作者: Batcher 时间: 2008-8-30 11:30
第一题:复制代码
Unix for Windows 工具集
http://bbs.bathome.net/thread-1114-1-1.html
作者: Batcher 时间: 2008-8-30 11:42
第二题:- @echo off
- copy a.txt b.txt
- change b.txt /from ":" /to ":&H0D0A "
- change b.txt /from "." /to ".&H0D0A "
复制代码
文本修改工具change.exe
http://bbs.bathome.net/viewthread.php?tid=1685
作者: wangwei4106 时间: 2008-8-30 11:53
不知道你们搞这个工具出来,是不是有点离题了……
其实我还不会用这些个工具!^_^
作者: batman 时间: 2008-8-30 12:57
原帖由 wangwei4106 于 2008-8-30 11:04 发表
2楼代码有误,至少最后一个you.就替换不了!
这样应该就可以了!
@echo off&setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%i in (a.txt) do (
set aa=%%i
set aa=!aa:you=me!
set aa=!aa:mer ...
为什么要tokens=* delims= 呢?
作者: terse 时间: 2008-8-30 13:06
写出了 晚上来贴
现在就Looking for a job is one of the toughest things me will ever have to do.
这句里will ever的空格有不?因为我这里替换没成功 去了空格就可以
[ 本帖最后由 terse 于 2008-8-30 13:08 编辑 ]
作者: more 时间: 2008-8-30 14:44
帖上两个(都不怎么通用),不知哪个会得到版主的认可,或都不合???- @echo off
- setlocal enabledelayedexpansion
- for /f "delims=" %%a in (test.txt) do (
- call set "str=%%str%%%%a"
- )
- :lp
- for /f "tokens=1* delims=:." %%a in ("%str%") do (
- if not defined home (echo %%a:&set home=bat) else (
- set "var= %%a."
- if /i "!var:~2,7!"=="looking" (echo Bathomework is a nice job.) else (
- set "var=!var:you =me !"&set "var=!var:you.=me.!"&echo !var!
- )
- )
- if not "%%b"=="" set "str=%%b"&goto :lp
- )
- pause>nul
复制代码
- @echo off
- setlocal enabledelayedexpansion
- for /f "delims=" %%a in (test.txt) do (
- set "str=%%a"
- call :lp
- )
- pause>nul
- goto :eof
- :lp
- for /f "tokens=1*" %%i in ("%str%") do (
- set "v=%%i"
- if "!v!"=="you" set "v=me"
- if "!v!"=="you." set "v=me."
- call set "var=%%var%%%%v%% "
- if "!var:~-2!"==": " (echo !var!&set "var=")
- if "!var:~-2!"==". " (
- if /i "!var:~,7!"=="looking" (
- echo Bathomework is a nice job.&set "var="
- ) else (echo !var!&set "var=")
- )
- if not "%%j"=="" set "str=%%j"&goto :lp
- )
复制代码
作者: pusofalse 时间: 2008-8-30 15:07 标题: 回复 13楼 的帖子
兄的代码是否考虑过于复杂了。
------------------------------------
没想到改题,屏蔽原有内容。
[ 本帖最后由 pusofalse 于 2008-8-30 15:21 编辑 ]
作者: batman 时间: 2008-8-30 15:24
提示:考虑问题复杂化,解决问题简洁化
作者: terse 时间: 2008-8-30 17:16
延迟出错 更改
- @echo off
- for /f "tokens=*" %%i in (a.txt) do if not defined str (set str=%%i)else call set str=%%str%% %%i
- set str=%str:your=_%
- set str=%str:you=me%
- set str=%str:_=your%
- set str=%str::=:#%
- set str=%str:.=.#%
- set str=%str:Looking for a job is one of the toughest things me will ever have to do.=bathomework is a nice job,%
- set str=%str:#=^&^>^>b.txt echo. %
- >>b.txt echo.%str%
- pause
复制代码
[ 本帖最后由 terse 于 2008-8-30 20:45 编辑 ]
作者: 随风 时间: 2008-8-30 17:56
1- @echo off
- for /f "delims=" %%a in (a.txt) do call set "str=%%str%%%%a"
- set "str= %str% "
- set "str=%str: you = me %"
- set "str=%str: you. = me. %"
- echo %str:~1,-1%
- pause
复制代码
2- @echo off
- for /f "delims=" %%a in (a.txt) do call set "str=%%str%%%%a"
- set "str=%str::=:&echo\ %"
- set "str=%str:.=.&echo\ %"
- (echo %str%)>>b.txt
- start "" "b.txt"
复制代码
[ 本帖最后由 随风 于 2008-8-30 17:57 编辑 ]
作者: wangwei4106 时间: 2008-8-31 22:06
才发现set功能如此强大,看了这贴子并参与进去,学到了很多!
要是有多些这样的训练就好了,自己想训练自己可是没有一点目标!
更加也发现不了自己的缺项,希望多点讲解……
貌似你们就发了贴,还不明白为什么就要这样用……
作者: keen 时间: 2009-5-14 00:20
- @echo off&setlocal enabledelayedexpansion
- set /p str=<1.txt
- for %%a in (%str%) do (
- set var=%%a
- set a=!var:~0,4!
- if "!a!"=="you" set var=!var:you=me!
- set "str1=!str1!!var! "
- )
- set "str1=%str1::=:&echo\ %"
- set "str1=%str1:.=.&echo\ %"
- echo %str1:you.=me.%&pause
复制代码
[ 本帖最后由 keen 于 2009-5-14 14:04 编辑 ]
作者: lengmoke 时间: 2021-10-14 11:44
- @echo off
- for /f "delims=" %%i in (a.txt) do ( set "str=%%i" )
- set str=%str:your=####%
- set str=%str:you=me%
- set str=%str:####=your%
- set str=%str::=:^^^&echo, %
- set str=%str:.=.^&echo, %
- (echo %str%)>>b.txt
- for /f "delims=" %%i in (b.txt) do (
- if "%%i"==" Looking for a job is one of the toughest things me will ever have to do." (
- echo, bathomework is a nice job.
- ) else (
- echo,%%i
- )
- )
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |