中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-28 08:05
47,813 topics / 349,918 posts / today 0 new / 48,277 members
DOS批处理 & 脚本技术(批处理室) » [Challenge 2] Detection and Calculation of Variables [Difficulty: ★]
Printable Version  5,069 / 38
Floor31 111ab Posted 2007-05-10 13:35
新手上路 Posts 1 Credits 2
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
Floor32 flyinspace Posted 2007-05-10 17:24
银牌会员 Posts 517 Credits 1,206
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.
Floor33 bjsh Posted 2007-06-06 10:55
银牌会员 Posts 621 Credits 2,000
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
Floor34 26933062 Posted 2007-08-15 19:12
银牌会员 Posts 879 Credits 2,268
```
:: 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 ]
Floor35 youxi01 Posted 2007-08-15 20:58
高级用户 Posts 247 Credits 846 From 湖南==》广东
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.


[ Last edited by youxi01 on 2007-8-15 at 09:07 PM ]
Floor36 knoppix7 Posted 2007-08-15 22:31
银牌会员 Posts 634 Credits 1,287 From cmd.exe
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
Floor37 26933062 Posted 2007-08-16 20:05
银牌会员 Posts 879 Credits 2,268
```
:: 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
```
Floor38 youxi01 Posted 2007-08-16 20:31
高级用户 Posts 247 Credits 846 From 湖南==》广东
Oh, you can know it if you try a very large number
Floor39 knoppix7 Posted 2007-08-16 20:37
银牌会员 Posts 634 Credits 1,287 From cmd.exe
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?
Prev  1 2 3
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023