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-22 09:32
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 2] Detection and Calculation of Variables [Difficulty: ★] View 4,214 Replies 38
Floor 16 Posted 2007-05-01 05:35 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by bjsh at 2007-4-30 04:16 PM:
Spared some time to write this..
The result is: 24691356902469134

Not bad, but temporary files will be generated.

In fact, there is a way to not generate temporary files. youxia01 gave us the best hint:)
知,不觉多。不知,乃求知
Floor 17 Posted 2007-05-01 05:44 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Originally, I didn't want to generate temporary files either;

Originally, I wanted to replace / with alt+13;

But I didn't achieve my idea; I really wanted to go and debug to see;

But I was short on time.;

So I just wrote it down..
Floor 18 Posted 2007-05-01 05:50 ·  中国 新疆 乌鲁木齐 天山区 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"

These two numbers should be
2341234567890123456
+ 223456789012345678
-----------------------------------------
2564691356902469134
It seems that there are numbers mixed among the letters in the front.

[ Last edited by zhoushijay on 2007-4-30 at 04:52 PM ]
Floor 19 Posted 2007-05-01 06:07 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by zhoushijay at 2007-4-30 04:50 PM:
set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"

These two numbers should be
2341234567890123456
+ 22345678901234567 ...


Oh. We just need the group of pure numbers.
知,不觉多。不知,乃求知
Floor 20 Posted 2007-05-01 06:20 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by bjsh at 2007-4-30 16:44:
Originally didn't want to generate temporary files;

Originally wanted to replace / with alt+13;

But my idea wasn't realized; really wanted to go debug and take a look;

But unfortunately time was relatively tight.;

Just wrote it down..

To replace / with a newline, you can refer to this:

@echo off&Setlocal EnableDelayedExpansion
for /f "delims=" %%a in (1.txt) do (
set str=%%a
set str=!str:/=$_!
exit|cmd/kprompt !str!>>new_.txt
)
start new_.txt
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bjsh +4 2007-05-01 07:30
Floor 21 Posted 2007-05-01 06:28 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
The original content contains some non-standard or hard-to-understand parts. Here is the translation:

I originally wrote a big number addition, and it was modified according to the requirements


@ECHO OFF 2>nul 3>nul&setlocal enabledelayedexpansion
set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"
set num=%num1:/= % %num2:/= %
for %%i in (%num%) do echo %%i|findstr /b /e *&&SET Z=/0%%i !z!
call :next %z%&&goto :eof
:next
set A=%~1&set B=%~2
:ADD
SET/AX-=1
CALL SET/AT1=%%A:~%X%,1%%+%%B:~%X%,1%%||(
ECHO %T3%%C%&&PAUSE&&GOTO :EOF)
CALL SET T3=%%A:~2,%X%%%%%B:~2,%X%%%
SET/A1/T2&&SET/AT1=T1+1
SET/AT2=1/(T1/10)||SET T2=0
SET C=%T1:~-1%%C%
GOTO ADD
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
chenall +5 2007-04-30 23:36
flyinspace +3 2007-05-01 06:36
bjsh +4 2007-05-01 08:22
Floor 22 Posted 2007-05-01 06:38 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
SET/A1/T2&&SET/AT1=T1+1
SET/AT2=1/(T1/10)||SET T2=0
This is a bit unclear?

SET/A1/T2 && SET/AT1=T1+1

Brother event79, can you explain it?
知,不觉多。不知,乃求知
Floor 23 Posted 2007-05-01 06:47 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
This is to perform the addition of single digits incrementally from right to left by character positions.

SET/A 1/T2 && SET/AT1=T1+1 // set /a 1/t2 is used to judge whether t2 (t2 is the carry of the previous cycle) is zero. If not, add 1 to the current result t1.

SET/AT2=1/(T1/10) || SET T2=0 // set /a t2=1/(t1/10) is used to judge whether the current cycle result (t1) is greater than 10. If it is greater than ten, t2 will be assigned 1 (the previous step of the next cycle). If it is less than ten, set the carry t2 to zero.
Floor 24 Posted 2007-05-01 06:56 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by everest79 at 2007-4-30 05:47 PM:
This is adding the units digits incrementally from right to left by character position

SET/A1/T2&&SET/AT1=T1+1 // set /a 1/t2 is used to judge whether t2 (t2 is the carry from the previous loop) is zero. If not, add 1 to the current result t1
SET/AT2=1/(T1/10)||SET T2=0 // set /a t2=1/(t1/10) is used to judge whether the current loop result (t1) is greater than 10. If it is greater than ten, t2 will be assigned 1 (for the next loop step). If it is less than ten, the carry t2 is set to zero



Thanks brother for the explanation, I understand the meaning of this sentence!

New question: Why set/a1/t2&&set/at1=t1+1
What's the use of adding this /?

According to your statement: This sentence is equivalent to>
set /a xxx= 1 / t2
Using the particularity of set /a: 
If xxx is less than 1, then xxx= 0
Being 0 satisfies the condition. Then t2 += 1

Learned another trick. It's a bit similar to the feeling of set /p? And without the = sign.
知,不觉多。不知,乃求知
Floor 25 Posted 2007-05-01 07:00 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
set /a a=1+1 can be abbreviated as set/aa=1+1. At that time, I just saw it in someone else's example, so I wrote it like that. It's not a good writing habit
Floor 26 Posted 2007-05-01 07:06 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by everest79 at 2007-4-30 06:00 PM:
set /a a=1+1 can be abbreviated as set/aa=1+1. At that time, I just saw it in someone else's example, so I wrote it like that. It's not a good writing habit.


set /a a=1/a is also equivalent to set /a1/a??
知,不觉多。不知,乃求知
Floor 27 Posted 2007-05-01 07:59 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
No, it's not.
`set /a a=1/a` is a calculation assignment.
`set /a 1/a` is just a calculation.
Floor 28 Posted 2007-05-02 17:35 ·  中国 广东 清远 英德市 电信
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
There seems to be a bit of an issue with the code for 21F. Let's continue to make modifications.

The testing is as follows:

19586
453
10039
Press any key to continue...


I also send a section of immature code (when there are more segments, the efficiency is relatively high):

::code by youxi01@cn-dos.net
@echo off & setlocal enabledelayedexpansion
set "str1=qwer/asdf2/asd34f/13546641654546546654654654144553098785546154/asdf/aaaa"
set "str2=aaaaa2/453216543225443263845482842643/asdfssasd/asdaa"
call :GetNum "%str1%" num1
call :GetNum "%str2%" num2
call :Split %num1%
set/a Mnum=%num%,num=0,flag=0
call :Split %num2%
if %Mnum% LSS %num% set/a Mnum=%num%
set/a Mnum_=%Mnum%-1
for /l %%i in (1 1 %Mnum_%) do (
set/a Rnum%%i+=!flag!
if !Rnum%%i! GTR 10000 set/a flag=1
set Rnum%%i=000!Rnum%%i!
set Rnum%%i=!Rnum%%i:~-4!
set Rstr=!Rnum%%i!!Rstr!)
set/a Rnum%Mnum%+=%flag%
echo !Rnum%Mnum%!!Rstr!
pause>nul
:GetNum OBJ Res
for /f "delims=/ tokens=1,*" %%i in ("%~1") do (
if %%i LSS a set %2=%%i & goto :eof
call :GetNum "%%j" %2
) & goto :eof
:Split OBJ
set str=%~1
set/a num+=1
if %str% LSS 10000 set/a Rnum%num%+=%str% & goto :eof
set/a Rnum%num%+=10000%str:~-4%%%10000
set str=%str:~0,-4%
call :Split "%str%"
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +3 2007-05-02 18:58
Floor 29 Posted 2007-05-02 19:02 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by youxi01 at 2007-5-2 04:35 AM:
There seems to be a bit of an issue with the code for 21F; it needs to be modified further:
Test as follows:

19586
453
10039
Press any key to continue. . .


I'll also post a piece of immature code myself (if there are many segments...

The code is really tiring to read..

Is there an algorithm??

Give a hint?
知,不觉多。不知,乃求知
Floor 30 Posted 2007-05-03 15:17 ·  中国 广东 清远 英德市 电信
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
::code by youxi01@cn-dos.net
@echo off & setlocal enabledelayedexpansion

set "str1=qwer/asdf2/asd34f/13546641/asdf/aaaa"
set "str2=aaaaa2/45321/asdfssasd/asdaa"

call :GetNum "%str1%" num1
call :GetNum "%str2%" num2

call :Split %num1%

REM Mnum saves the number of segments of the result (used to accept the maximum number of segments);
REM Reset the value of num to 0 to continue calling the "function"
REM flag is the carry flag, if it is 1, the next segment number is added by 1; here it is the initial value, set to 1.
set/a Mnum=%num%,num=0,flag=0

call :Split %num2%
if %Mnum% LSS %num% set/a Mnum=%num%
set/a Mnum_=%Mnum%-1

for /l %%i in (1 1 %Mnum_%) do (
set/a Rnum%%i+=!flag!
if !Rnum%%i! GTR 10000 set/a flag=1

REM Adding 000 in front is to fill the number, to prevent numbers with less than 4 digits like 333.
set Rnum%%i=000!Rnum%%i!

set Rnum%%i=!Rnum%%i:~-4!
set Rstr=!Rnum%%i!!Rstr!)

set/a Rnum%Mnum%+=%flag%
echo !Rnum%Mnum%!!Rstr!

pause>nul

:GetNum OBJ Res
for /f "delims=/ tokens=1,*" %%i in ("%~1") do (

REM Use whether it is less than a to detect whether it is a number. If it is, it must be less than a (no matter how many digits it is)
REM If a number is detected, exit.
if %%i LSS a set %2=%%i & goto :eof

REM Loop detection;
call :GetNum "%%j" %2
) & goto :eof

:Split OBJ
set str=%~1
set/a num+=1

REM If the cut number is less than 10000, set its own value to the original value plus the number and exit;
if %str% LSS 10000 set/a Rnum%num%+=%str% & goto :eof

REM Use the willsort method to eliminate errors caused by set/a calculation similar to 0001;
set/a Rnum%num%+=10000%str:~-4%%%10000

set str=%str:~0,-4%
call :Split "%str%"
Forum Jump: