China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-28 05:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Symmetric output bonus question (challenging) View 4,183 Replies 64
Floor 31 Posted 2008-07-11 15:37 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by 26933062 at 2008-7-11 15:18:
Another one, with a clearer train of thought.

@echo off&setlocal enabledelayedexpansion
set "str1= 0 1 2 3 4 5 6 7 8 9"
set "str2= j i h g f e d c b a"
echo.&set /p n ...

Brother Xiaolou, this solution still lacks in generality.
批处理之家新域名:www.bathome.net
Floor 32 Posted 2008-07-11 15:40 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Oh?
How is it not universal?
致精致简!
Floor 33 Posted 2008-07-11 15:48 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
for /l %%a in (1 2 19) do (...
Just this sentence.
批处理之家新域名:www.bathome.net
Floor 34 Posted 2008-07-11 16:14 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-7-11 15:48:
for /l %%a in (1 2 19) do (。。。
Just this sentence.
Added a judgment and calculation for the number of digits, should this be okay?
致精致简!
Floor 35 Posted 2008-07-11 16:50 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
I have points no

@echo off&setlocal enabledelayedexpansion
set/p p=Please enter:
set str=abcdefghij
for /l %%i in (0 1 9) do set %%i=%%i&set/a !str:~%%i,1!=9-%%i
:lp
if defined p set .!%p:~,1%!=!%p:~,1%!&set p=!p:~1!&goto lp
for /l %%i in (0 1 9) do (
if defined .%%i (
set var1=!var1!%%i&set/a _%%i=9-%%i
call set var2=%%str:~!_%%i!,1%%!var2!
)else set var1=!var1!#&set var2=#!var2!
)
echo !var1! ^| !var2!
pause
简单!简单!再简单!
Floor 36 Posted 2008-07-11 17:01 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by terse at 2008-7-11 16:50:
Do I have points?

@echo off&setlocal enabledelayedexpansion
set/p p=Please enter:
set str=abcdefghij
for /l %%i in (0 1 9) do set %%i=%%i&set/a !str:~%%i,1!=9-%%i
:lp
if defined p set ...

The for /l %%i in (0 1 9)... is not universal, please brother take a closer look at the instructions in my top post.
批处理之家新域名:www.bathome.net
Floor 37 Posted 2008-07-11 17:03 ·  中国 江苏 苏州 常熟市 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
This is another one that generates temporary files. It's a beginner's approach, but it has very good generality!

@echo off
set/p input=Please enter:
rem " .%input%" The "." in it is to prevent errors when entering pure single-digit numbers (1-9?)
echo .%input%>%temp%\temp.txt
set string=0123456789abcdefghij
set num=
:pro1
set /a num+=1
set strtmp=%string:~0,1%%string:~-1%
set str%num%=##
rem The following line is an application of the findstr category
findstr %temp%\temp.txt>nul&&set str%num%=%strtmp%
set string=%string:~1,-1%
if not defined string goto pro2
goto pro1

:pro2
setlocal enabledelayedexpansion
set str_1=x
for /l %%i in (1,1,%num%) do call set str_1=!str_1:~0,-1!%%str%%i%%
for /l %%i in (1,1,%num%) do call set str_2=%%str%%i%%!str_2:~1!
echo %str_1:~,-1% ^| %str_2:~1,-2%

pause


I feel that the generality is the best. You can verify if you don't believe it. Experts can simplify it according to my idea. The things I write often have redundant code.

The writing method that doesn't generate temporary files is actually simpler. I'm mainly used to using temporary files.

@echo off
set/p input=Please enter:
set string=0123456789abcdefghij
set num=
:pro1
set /a num+=1
set strtmp=%string:~0,1%%string:~-1%
set str%num%=##
rem The following line is an application of the findstr category
echo %input%|findstr >nul&&set str%num%=%strtmp%
set string=%string:~1,-1%
if not defined string goto pro2
goto pro1

:pro2
setlocal enabledelayedexpansion
set str_1=x
for /l %%i in (1,1,%num%) do call set str_1=!str_1:~0,-1!%%str%%i%%
for /l %%i in (1,1,%num%) do call set str_2=%%str%%i%%!str_2:~1!
echo %str_1:~,-1% ^| %str_2:~1,-2%

pause


[ Last edited by quya on 2008-7-11 at 07:33 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +5 2008-07-11 17:09
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 38 Posted 2008-07-11 18:38 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Again


@echo off&setlocal enabledelayedexpansion
set "str=0123456789abcdefghij"
set str1=!str!
:loop
set .!str:~0,1!=!str:~-1,1!
set .!str:~-1,1!=!str:~0,1!
set str=!str:~1,-1!
set /a nnn+=1
if defined str goto loop
for /f "tokens=2 delims==" %%a in ('set .') do set str1=!str1:%%a= %%a!

set /p num=Please enter, can not enter spaces &cls&set num=!num: =!
for %%i in (!str1!) do set num=!num:%%i= %%i!
for %%a in (!num!) do set num=!num! !.%%a!
for %%a in (!num!) do set _%%a=a
for %%a in (!str1!) do if defined _%%a (set var=!var!%%a) else set var=!var!#
echo.&echo !var:~0,%nnn%! !var:~%nnn%!
pause>nul


[ Last edited by 26933062 on 2008-7-13 at 11:10 AM ]
致精致简!
Floor 39 Posted 2008-07-11 20:35 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by zw19750516 at 2008-7-11 17:01:

for /l %%i in (0 1 9)...
Not universal, please brothers take a close look at the instructions in my top post.

When inputting 7, the output is as follows:
#######7## | ##c#######
When inputting a5, the output is as follows:
#####5###9 | a###e#####
When inputting 12j, the output is as follows:
012####### | #######hij
I think my code has achieved
Little brother is stupid, can't figure out where it's wrong, hope brother zw19750516 can give instructions
简单!简单!再简单!
Floor 40 Posted 2008-07-11 22:26 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
The problem with what you have upstairs is:

1. If there are fewer characters on both sides, you need to modify multiple places in the code.
2. The biggest shortcoming is that if the left and right are swapped, you have to change it again. What if the left is reversed? What if there are letters on the left? What if the characters on both sides are completely irregular?

So it's really not universal.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 41 Posted 2008-07-12 10:54 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
I fully agree with Laotu. Also, please brothers check the requirements in the top post first when solving my problems each time, because my thinking of setting problems might be a bit careless at the beginning, but I will keep modifying it. It's also hard for the brothers.
批处理之家新域名:www.bathome.net
Floor 42 Posted 2008-07-12 10:58 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
:loop
set .!str:~0,1!=!str:~-1,1!
set .!str:~-1,1!=!str:~0,1!
set str=!str:~1,-1!
set /a nnn+=1
if defined str goto loop

Brother Xiaolou's idea this time is the same as my first plan, both intercepting characters from both ends at the same time. It's just that brother is more skilled in processing, adding the assignment with itself as a variable. I've learned.

[ Last edited by zw19750516 on 2008-7-12 at 11:35 AM ]
批处理之家新域名:www.bathome.net
Floor 43 Posted 2008-07-12 12:36 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
I can't understand your code basically. I get a headache when I see !variables! and so on, so I don't use them unless it's absolutely necessary. I replace what can be replaced with CALL. (CALL is also something I just learned recently.)

This time taking the initiative to use delayed variables is my first time ever. I really can't think of any other way. Usually I just copy others' code for delayed variables.

Also, most of your inputs need to be separated by spaces. I don't know why. I think with your skills, there must be other ways. It's so awkward to input with spaces.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 44 Posted 2008-07-12 12:56 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Additionally, most of your inputs need to be separated by spaces. I don't know why. I think with your skills, you must be able to use other methods. It's so awkward to input with spaces.

Old-fashioned. To solve it, is it a problem? (The code in my 25th floor is not input with spaces), but this will make the code complicated. The core problem of this question is not on the processing of this. Therefore, in order to highlight the core code of solving the problem more, my second solution adopts the method of inputting with spaces.
批处理之家新域名:www.bathome.net
Floor 45 Posted 2008-07-12 13:36 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
I can't agree with the LZ's view.

Looking at the problem - solving in the past few days, I find that everyone is a bit obsessed, emphasizing skills over practicality.

Programs are ultimately used to facilitate oneself and others, so first, the human - computer interface should be friendly, default settings should be reasonable, in line with the usage habits of most people, and there can be personalized settings. Second, compatibility should be good. Third, the running speed should be fast, and the memory and hard disk space occupied should be small.

Examples of a friendly human - computer interface are as follows:

1. For example, it is not pleasant that a space must be added when entering this question.
2. For example, it is not pleasant that something must be clicked at the beginning when running.
And so on.

For such problems, it is not good that users have to adapt every time they use it. It is better to make the program a bit more complicated because the program is only written once (of course, debugging is done multiple times), but users will use it N times. The EPR program used in our company has been torturing me all the time.

Places where skills should be used:

1. Where programming is impossible without this skill.
2. Where it can significantly improve the running speed, such as the optimization of loop statements. And some codes that run only once don't matter. If one pursues skills too much, it will make code maintenance difficult.
3. Where skills should be used but are not, for example, the modularization of programming nowadays makes some programmers who program large - scale programs not use their brains. Finally, the programs they write are not humanized or rationalized at all. For example, the running result will be something like 3.00 pigs. Taking the ID automatically generated by the database as the search condition, while the user - friendly search conditions are hard to find, and fields that don't interact with the user at all are not hidden, etc.

I'll write so much for now. Actually, I have encountered too many problems in practice.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Forum Jump: