![]() |
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-08-10 13:05 |
47,811 topics / 349,897 posts / today 0 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » [Challenge 2] Detection and Calculation of Variables [Difficulty: ★] |
| Printable Version 4,509 / 38 |
| Floor1 flyinspace | Posted 2007-04-30 13:51 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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 ] |
|
| Floor2 baomaboy | Posted 2007-04-30 14:48 |
| 银牌会员 Posts 554 Credits 1,513 | |
|
Since I know nothing about P, write a VBS.
[ Last edited by baomaboy on 2007-4-30 at 04:37 PM ] |
|
| Floor3 flyinspace | Posted 2007-04-30 15:18 |
| 银牌会员 Posts 517 Credits 1,206 | |
Originally posted by baomaboy at 2007-4-30 01:48 AM: Attachments 1.JPG (3.91 KiB) |
|
| Floor4 zh159 | Posted 2007-04-30 15:41 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
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 |
|
| Floor5 flyinspace | Posted 2007-04-30 15:46 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
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. |
|
| Floor6 baomaboy | Posted 2007-04-30 16:35 |
| 银牌会员 Posts 554 Credits 1,513 | |
Originally posted by flyinspace at 2007-4-30 15:46: 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 ] |
|
| Floor7 digger | Posted 2007-04-30 20:15 |
| 初级用户 Posts 29 Credits 79 From 湖南 | |
|
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.
|
|
| Floor8 youxi01 | Posted 2007-04-30 20:55 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
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 ] |
|
| Floor9 youxi01 | Posted 2007-04-30 21:26 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
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. |
|
| Floor10 flyinspace | Posted 2007-04-30 22:23 |
| 银牌会员 Posts 517 Credits 1,206 | |
|
@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 ] |
|
| Floor11 zhoushijay | Posted 2007-05-01 00:58 |
| 高级用户 Posts 375 Credits 845 | |
|
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 ] |
|
| Floor12 baomaboy | Posted 2007-05-01 04:18 |
| 银牌会员 Posts 554 Credits 1,513 | |
|
When a numeric string and a number are operated together, the numeric string is treated as a number
|
|
| Floor13 flyinspace | Posted 2007-05-01 05:01 |
| 银牌会员 Posts 517 Credits 1,206 | |
Originally posted by zhoushijay at 2007-4-30 11:58 AM: |
|
| Floor14 bjsh | Posted 2007-05-01 05:16 |
| 银牌会员 Posts 621 Credits 2,000 | |
|
Spared some time to write this..
Result: 24691356902469134
|
|
| Floor15 zh159 | Posted 2007-05-01 05:17 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
Originally posted by zhoushijay at 2007-4-30 11:58: How can a 16-digit number plus a 17-digit number result in a 19-digit number? 1234567890123456 +23456789012345678 -------------------------------------- =24691356902469134 ================= 2564691356902469134 |
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |