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 07:54
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 2] Detection and Calculation of Variables [Difficulty: ★] View 4,211 Replies 38
Original Poster Posted 2007-04-30 13:51 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"

Goal: Extract the combinations of pure numbers from the above two conditions, calculate the sum of these two numbers, and then output the result correctly to the screen. (Only one subroutine can be used to extract the above two strings, because it is for testing, only two string combinations are given)

Writing conditions: cmd (commands included in help)

Difficulty: Overflow in digit position and calculation of random numbers

Target audience: People with certain batch processing foundation.

The challenge will gradually increase in difficulty. (Everyone try to do what you can)

Writing points reward: 3 points.

One point for completing the extraction task, two points for completing the calculation task.

Adding points is not the purpose. It is an affirmation of ability.

The second floor correctly obtained the random string, but there was a problem in the processing calculation.

The output is in scientific notation. But the requirement is: output the exact value. (Complete the first item, add one point)

[ Last edited by flyinspace on 2007-4-30 at 01:19 PM ]
知,不觉多。不知,乃求知
Floor 2 Posted 2007-04-30 14:48 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Since I know nothing about P, write a VBS.

num1="qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
num2="aaaaa2/23456789012345678/asdfssasd/asdaa"
numarr=split(num1&"/"&num2,"/")
For i=0 To UBound(numarr)
if IsNumeric(numarr(i)) then
num=num+CDbl(numarr(i))
end if
Next
msgbox num
msgbox FormatNumber(num,0,,,0)


[ Last edited by baomaboy on 2007-4-30 at 04:37 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
zh159 +3 2007-04-30 15:46
flyinspace +1 2007-04-30 15:46
Floor 3 Posted 2007-04-30 15:18 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by baomaboy at 2007-4-30 01:48 AM:
Since I know nothing about P, write a VBS:

num1="qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
num2="aaaaa2/23456789012345678/asdfssasd/asdaa"
numarr=split(num1&"/"&num2,"/")
For i=0 To UBound(numarr)
if IsNumeric(numarr(i)) then
num=num+CDbl(numarr(i))
end if
Next
msgbox num



Challenge failed:
Reasons see attachment:

We require the correct output result.

[ Last edited by flyinspace on 2007-4-30 at 02:35 AM ]
Attachments
1.JPG
知,不觉多。不知,乃求知
Floor 4 Posted 2007-04-30 15:41 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The person upstairs, the result of the second floor is correct. E+16 is the scientific notation representing N*(1 followed by 16 zeros).
24691356902469134 = 2.46913569024691*10000000000000000 = 2.46913569024691E+16
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +1 2007-04-30 15:46
Floor 5 Posted 2007-04-30 15:46 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
I'm sorry, I also know his result is correct...

But our requirement is:
Output: 24691356902469134
This result, not the result of 2.46913569024691E+16...

This is my unclear explanation...

What if our number changes a bit?? There are 30 digits??

We require precision... Hehe.

Finally, thank zh159 for pointing out.
知,不觉多。不知,乃求知
Floor 6 Posted 2007-04-30 16:35 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
Originally posted by flyinspace at 2007-4-30 15:46:
I'm sorry, I also know that his result is correct...

But our requirement is:
Output: 24691356902469134
This result, not the result of 2.46913569024691E+16...

This...


Then format it:
msgbox FormatNumber(num,0,,,0)

But I estimate that no one can get the last two digits 34. A few years ago, I found in Excel that all display 0 after exceeding 15 digits. Maybe Microsoft thinks that 15 digits are enough and limits it, heh heh, just kidding. Anyway, I don't need 15 digits and won't delve into it. If someone knows, please give a pointer.

[ Last edited by baomaboy on 2007-4-30 at 04:51 PM ]
Floor 7 Posted 2007-04-30 20:15 ·  中国 广东 电信
初级用户
游荡的孤魂
Credits 79
Posts 29
Joined 2007-04-02 11:08
19-year member
UID 83714
Gender Male
From 湖南
Status Offline
There was a special discussion on the topic of calculating the sum of two numbers in the forum, and it's difficult in the essence posts.
Floor 8 Posted 2007-04-30 20:55 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
I once wrote calculations (multiplication) of large numbers, with numbers within 100 digits.

Also, looking at the code of 2F, it seems that only one of the two strings is a number! (That is to say, there are no other special cases, such as what to do if there are two numbers, etc.?)

[ Last edited by youxi01 on 2007-4-30 at 08:56 PM ]
Floor 9 Posted 2007-04-30 21:26 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Provide ideas: (Because I'm going to work soon, I can't give detailed code. Also, because of moving, I won't be able to access the Internet in the short term, so I may leave the union temporarily.)
1. Regarding the extraction of each character segment, there is related code in Challenge 1 yesterday.
2. The problem of whether the extracted each character segment is related to digital judgment. There are three solutions to this problem:
1> Use the particularity of set/a, this method is not strict, and cannot prevent the situation where the character segment is exactly 0.
2> Use findstr + regular expression, which is relatively precise, but inefficient.
3> The method that I personally recommend: directly compare letters (including Chinese characters) with numbers. If a certain character segment is purely digital, it must be smaller than a. Disadvantage: Cannot handle special characters.
3. Regarding the calculation of extremely large numbers. We can adopt the method of segmental calculation. There has been related discussion, and I have also written related code, which will not be repeated here.
Floor 10 Posted 2007-04-30 22:23 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
@echo off & setlocal EnableDelayedExpansion
set "a=123456789012485642424543456"
set "b=234567890000000000000005678"
call :GetLen "%a%"
set "Len1=%Len%"
call :GetLen "%b%"
set "Len2=%Len%"
set "s="
set "bit=0"
set "count=0"
:recompute
set /a "count+=1"
if %Len1% GEQ 9 (
set "str1=%a:~-9%
set "a=%a:~0,-9%"
set /a "Len1-=9"
) else (
set "str1=%a%"
set "a=0"
)
if %Len2% GEQ 9 (
set "str2=%b:~-9%"
set "b=%b:~0,-9%
set /a "Len2-=9"
) else (
set "str2=%b%"
set "b=0"
)
call :formatstr %str1%
set "str1=%xxx%
call :formatstr %str2%
set "str2=%xxx%
set /a "num=!str1!+!str2!+!bit!"
call :GetLen "%num%"
if '%Len% GTR 9' (
set "bit=%num:~0,1%"
set "num=%num:~-9%"
)
set "s=%num%%s%"
echo : %str1%+%str2%=%s%
if "%a%"=="0" (
if "%b%" == "0" (
goto :END
)
)
goto :recompute


:formatstr _str_
set "xxx=%~1"
:reformat
set "lll=9"
if "%xxx:~0,1%"=="0" (
set "xxx=!xxx:~1,%lll%!
set /a "lll-=1"
goto :reformat
)
goto :EOF

:GetLen
set "string=%~1"
for /l %%i in (0,1,255) do (
if "!string:~%%i,1!"=="" (
set "Len=%%i"
goto :EOF
)
)
goto :EOF
:END
echo ——————————————————————————
echo Final answer: %s%
echo ——————————————————————————
echo Program demonstration ends, press any key to exit & pause>nul

[ Last edited by flyinspace on 2007-4-30 at 04:29 PM ]
知,不觉多。不知,乃求知
Floor 11 Posted 2007-05-01 00:58 ·  中国 新疆 乌鲁木齐 天山区 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
num1="qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
num2="aaaaa2/23456789012345678/asdfssasd/asdaa"
len1=len(num1)
len2=len(num2)


for i=1 to len1
nu1=mid (num1,i,1)

on error resume next
a=int(nu1)
if err.number=0 then
b=b&a
end if
next

for a=1 to len2
nu2=mid (num2,a,1)

on error resume next
d=int(nu2)
if err.number=0 then
s=s&d

end if
next
s=int(s)
b=int(b)

he=s+b

msgbox(he) The correct answer should be 2564691356902469134, I calculated it twice with a calculator

[ Last edited by zhoushijay on 2007-4-30 at 04:53 PM ]
Floor 12 Posted 2007-05-01 04:18 ·  中国 河北 保定 联通
银牌会员
★★★
Credits 1,513
Posts 554
Joined 2005-12-30 00:50
20-year member
UID 48180
Gender Male
Status Offline
When a numeric string and a number are operated together, the numeric string is treated as a number
Floor 13 Posted 2007-05-01 05:01 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
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 11:58 AM:
nu1=mid (num1,i,1)

on error resume next
a=int(nu1)
if err.number=0 then
b=b&a
end if
next

for a=1 to len2
nu2=mid (num2,a,1)

on error resume next
d=int(nu2)
if err.number=0 then
s=s&d

end if
next
s=int(s)
b=int(b)

he=s+b
msgbox(he)
The correct answer should be 2564691356902469134. I calculated it twice with a calculator.
=1234567890123456
23456789012345678+
--------------------------------
24691356902469134

No matter how I compare, I didn't make a mistake.

Why is it
2564691356902469134?

[ Last edited by flyinspace on 2007-4-30 at 04:10 PM ]
知,不觉多。不知,乃求知
Floor 14 Posted 2007-05-01 05:16 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Spared some time to write this..
Result: 24691356902469134


  1. @echo off & setlocal enabledelayedexpansion
  2. set "num1=qwer/asdf2/asd34f/1234567890123456/asdf/aaaa"
  3. set "num2=aaaaa2/23456789012345678/asdfssasd/asdaa"
  4. set "num1=%num1:/= %" & call :get_number !num1! & set "num1=!t!"
  5. set "num2=%num2:/= %" & call :get_number !num2! & set "num2=!t!"
  6. set "result="
  7. :c_loop
  8. set /a x=%num1:~-8%
  9. set /a y=%num2:~-8%
  10. set /a z=%x%+%y%
  11. if defined flag set /a z=%z%+%flag%
  12. set "flag=%z:~0,-8%" >>nul 2>>nul && set "z=%z:~-8%"
  13. set "num1=%num1:~0,-8%"
  14. set "num2=%num2:~0,-8%"
  15. set "result=%z%%result%"
  16. if not defined num1 if not defined num2 set "result=%flag%%result%" & goto :show
  17. if not defined num1 set /a num1=0
  18. if not defined num2 set /a num2=0
  19. goto c_loop
  20. :show
  21. echo %result% & pause & goto :eof
  22. :get_number
  23. :loop
  24. echo %1 >>tmp.txt & shift
  25. if not "%1"=="" goto :loop
  26. for /f %%a in ('findstr /r "^" tmp.txt') do set "t=%%a"
  27. del tmp.txt
BJSH posted on: 2007-04-30 16:04
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +3 2007-05-01 05:34
Floor 15 Posted 2007-05-01 05:17 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by zhoushijay at 2007-4-30 11:58:
nu1=mid (num1,i,1)

on error resume next
a=int(nu1)
if err.number=0 then
b=b&a
end if
next

for a=1 to len2
nu2=mid (num2,a,1)

on error resume next
d=int(nu2)
if err.number=0 then
s=s&d

end if
next
s=int(s)
b=int(b)

he=s+b
msgbox(he)
The correct answer should be 2564691356902469134, I calculated it twice with a calculator

How can a 16-digit number plus a 17-digit number result in a 19-digit number?

   1234567890123456
 +23456789012345678
--------------------------------------
 =24691356902469134
=================
2564691356902469134
Forum Jump: