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:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 2] Detection and Calculation of Variables [Difficulty: ★] View 4,213 Replies 38
Floor 31 Posted 2007-05-10 13:35 ·  中国 辽宁 铁岭 联通
新手上路
Credits 2
Posts 1
Joined 2007-05-10 11:28
19-year member
UID 88060
Gender Male
Status Offline
Originally posted by bjsh at 2007-5-1 05:16 AM:
Spent some time to write this..
Result: 24691356902469134

Brother bjsh:
I accidentally saw your post and was impressed by your code. I spent more than a day reading your code and just now understood the part of extracting numeric fields. I'm a newbie, don't laugh at me, haha. I have some questions to ask.
It feels that your regular expression of findstr can also match cases like "12345adfadfds45156" (both ends are numbers but the middle is characters). I want to change it to this:
findstr "^*$" tmp.txt
There is a parameter /r in your findstr. I didn't see this parameter when I checked the help of findstr in the command line. I tried the cases with and without /r respectively and didn't see any difference. What is the function of that /r?

Note: The regular expression "^*$" cannot match cases like "123457684 ", so the code in the get_number section needs to be slightly modified. My suggestion is as follows:
Original code: echo %1 >>tmp.txt & shift
Modified: echo %1>>tmp.txt& shift ::Note that there should be no spaces after %1 and after .txt here, otherwise the spaces will be input into .txt and affect the matching of the regular expression.
Or change to: echo %1>>tmp.txt
shift
Floor 32 Posted 2007-05-10 17:24 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by 111ab at 2007-5-10 01:35 PM:

Dear Brother bjsh:
I accidentally saw your post and was overwhelmed by your code. I spent more than a day reading your code and just now understood the part of extracting numeric fields. I'm a newbie, don't laugh at me, ...

I still don't quite understand regular expressions. I just started to get in touch with VBS.

But brother, you can take a close look at findstr /?

It has an explanation: /r means using general expressions. And according to the description of general expressions below, it should belong to the category of regular expressions.
知,不觉多。不知,乃求知
Floor 33 Posted 2007-06-06 10:55 ·  IANA 局域网IP(Private-Use)
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
to 111ab

It feels that your findstr regular expression can also match cases like "12345adfadfds45156" (both ends are numbers but the middle is characters). I want to change it to this:
findstr "^*$" tmp.txt



Note: The regular expression "^*$" cannot match cases like "123457684 ", so the code in the get_number segment needs to be slightly modified. My suggestion is as follows:
Original code: echo %1 >>tmp.txt & shift
Modified: echo %1>>tmp.txt& shift :: Note that there should be no spaces after %1 and after .txt here, otherwise the spaces will be input into .txt and affect the matching of the regular expression.
Or change to: echo %1>>tmp.txt
shift



Thanks brother for the guidance;
Brother's suggestion is very reasonable
Floor 34 Posted 2007-08-15 19:12 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
```
:: While learning from all the experts, I also wrote one, which is not very technical.
:: But it seems to meet the requirements of the original poster
:: The result obtained is 24691356902469134
:: Features:
:: Not limited to the size of numbers, as long as it is summation, it can be done
:: Idea:
:: Directly add the last digit, then add the carry digit, then discard the last digit of the original number, and cycle like this until the result.


@echo off & setlocal enabledelayedexpansion
set "num1=qwer/2asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"

set "num1=%num1:/= %"&set "num2=%num2:/= %"&set b=1
for %%i in (%num1% %num2%) do (
echo %%i|findstr "^*$">nul&&set num!b!=%%i&&set /a b+=1
)
set jin=0

:loop
set /a a=%num1:~-1%+%num2:~-1%+%jin%
set jieguo=%a:~-1%%jieguo%
set a=0%a%
set jin=%a:~-2,1%
set num1=%num1:~0,-1%
set num2=%num2:~0,-1%
if "%num1%"=="" set /a zz=%num2%+%jin%&goto lis
if "%num2%"=="" set /a zz=%num1%+%jin%&goto lis
goto loop

:lis
if "%zz%"=="0" set zz=
echo.&echo Result: %zz%%jieguo%
echo.&pause
```

[ Last edited by 26933062 on 2007-8-15 at 07:51 PM ]
致精致简!
Floor 35 Posted 2007-08-15 20:58 ·  中国 中电华通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Back to this problem again.
Just thought of a "filling positions" method to implement addition, thus avoiding all if checks, and the main part of the program runs in a for loop, avoiding the use of goto, thereby greatly improving efficiency (especially for very large numbers). The given program code supports addition of numbers within 200 digits.

::code by youxi01@cn-dos.net
@echo off&setlocal enabledelayedexpansion
set "str1=qwer/asdf2/asd34f/123456789012345665644654456454665446565446544665446546545644565465464646546544654546654654465654465654665465465465446565446564/asdf/aaaa"
set "str2=aaaaa2/23456789012345678654466544466544641125654456455464654546654465654465446554665445454654544545654/asdfssasd/asdaa"
call :GetNum %str1% num1
call :GetNum %str2% num2
call :lineup num1
call :lineup num2
set/a flag=0
for /l %%i in (2 1 199) do (
set/a var=!num1:~-%%i,1!+!num2:~-%%i,1!+!flag!
set var=0!var!
set flag=!var:~-2,1!
set str=!var:~-1!!str!
)
for /f "delims=0 tokens=*" %%i in ("!str!") do echo %%i
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
:lineup obj
for /l %%i in (1 1 200) do set %1=0!%1!
call set %1=!%1:~-200!


[ Last edited by youxi01 on 2007-8-15 at 09:07 PM ]
Floor 36 Posted 2007-08-15 22:31 ·  中国 北京 海淀区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
Experts, take a look if this works??

@echo off
set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"
SETLOCAL ENABLEDELAYEDEXPANSION
set tester=500
:main1
FOR /F "tokens=1,2* delims=/" %%i in ("%num1%") do (
set NTC=
set RUS=
set NTC=%%i
set /a RUS=%tester%+!NTC!>>nul>>nul
if !ERRORLEVEL!==9168 set shuzi1=!NTC!
set num1=%%j/%%k
goto main1
)
cls
:main2
FOR /F "tokens=1,2* delims=/" %%a in ("%num2%") do (
set NTC=
set RUS=
set NTC=%%a
set /a RUS=%tester%+!NTC!>>nul>>nul
if !ERRORLEVEL!==9168 set shuzi2=!NTC!
set num2=%%b/%%c
goto main2
)
cls
echo !shuzi1!
echo !shuzi2!
pause
Floor 37 Posted 2007-08-16 20:05 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
```
:: Wrote a padding one, as you said, there is not a single goto and if globally
:: But the efficiency doesn't seem to have improved at all?

@echo off & setlocal enabledelayedexpansion
set "num1=qwer/2asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"

set "num1=%num1:/= %"&set "num2=%num2:/= %"&set b=1
for %%i in (%num1% %num2%) do (
echo %%i|findstr "^*$">nul&&set num!b!=%%i&&set /a b+=1
)
echo !num1! + !num2!
for /l %%i in (1 1 200) do (
set num1=0!num1!
set num2=0!num2!
)
set num1=!num1:~-200!&set num2=!num2:~-200!

set jin=0
for /l %%i in (-1 -1 -200) do (
set /a a=!num1:~%%i,1!+!num2:~%%i,1!+!jin!
set jie=!a:~-1!!jie!
set a=0!a!
set jin=!a:~-2,1!
)
for /f "tokens=* delims=0" %%i in ("!jie!") do echo.&echo %%i
echo.&pause
```
致精致简!
Floor 38 Posted 2007-08-16 20:31 ·  中国 中电华通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Oh, you can know it if you try a very large number
Floor 39 Posted 2007-08-16 20:37 ·  中国 北京 西城区 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
The number given by LZ is very long.
Directly using set operations will report an error. ERRORLEVEL is 9168
Then just detect if ERRORLEVEL == 9168, right?
Forum Jump: