![]() |
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-26 01:17 |
47,812 topics / 349,910 posts / today 0 new / 48,264 members |
| DOS批处理 & 脚本技术(批处理室) » About the problem of number bases during numerical operations! |
| Printable Version 3,135 / 11 |
| Floor1 pengfei | Posted 2006-09-05 02:07 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
When using SET /A for operations, if the first digit of the character is zero and the value contains 8 or 9, it will cause the value to not be read correctly.
Because except that hexadecimal has the 0x prefix and octal has the 0 prefix, the numeric value is in decimal. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits. Example: C:\>set /a minute=08+5 Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021). C:\>set /a minute=5+018 Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021). C:\>set /a minute=0029+8 Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021). C:\>set /a minute=125+09 Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021). Solutions: 1. When operating constants, if the constant contains 8 or 9, do not add zero in front, in short, the highest digit cannot be zero. Example: Incorrect (08, 009, 018, 0029) Correct (8, 9, 18, 29) 2. Prevent the highest digit of the variable from being zero during variable operation; Algorithm: First take the first digit of the variable and judge whether it is zero; If it is true, then take the remainder after the first digit and reassign it to the variable; Execute the loop until the first digit is not zero; Finally, use the zero-removed variable for operation. Code: @echo off set mu=0001028 echo Before removing zero=%mu% :again if "%mu:~0,1%"=="0" ( set mu=%mu:~1% goto again ) echo After removing zero=%mu% set /a num=%mu%+154 echo Operation result=%num% pause I encountered this problem when writing a batch script today, and finally figured out the reason. Also found the solution, and in the future, everyone can avoid detours when encountering corresponding problems. [ Last edited by pengfei on 2006-10-2 at 09:41 ] |
|
| Floor2 vkill | Posted 2006-09-05 03:41 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
Well, I didn't notice this number system
|
|
| Floor3 namejm | Posted 2006-09-05 05:17 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Hehe, there was a vague mention of handling when the first digit is 0 in the old posts of the forum, but I can't find that post right now.
|
|
| Floor4 lxmxn | Posted 2006-09-29 11:14 |
| 版主 Posts 4,938 Credits 11,386 | |
|
Learning...
You can also use this algorithm to judge the number of spaces in a string. Hehe` |
|
| Floor5 redtek | Posted 2006-09-29 13:08 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Bump
|
|
| Floor6 redtek | Posted 2006-09-29 22:55 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Just came here again, haha...
:again if "%mu:~0,1%"=="0" ( set mu=%mu:~1% goto again ) What you wrote here with :again and goto again is really cool! It continues until the leading 0s are removed until the IF condition naturally fails and jumps out to continue executing the new content :) Admired~~~ |
|
| Floor7 redtek | Posted 2006-09-30 00:34 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
Inspired by the wonderful application of " :again ... goto again " from the building - owner pengfei, I tried to make a simulation experiment of recursive factorial calculation of N by learning the principle of goto of the building - owner. It is really convenient to use goto well~:)
Thank you again for the building - owner's masterpiece "Regarding the base problem in numerical operations"~:) Calculate the factorial of N~:) There is no boundary crossing and no illegal check, just a work for learning the application of goto of the building - owner~:) |
|
| Floor8 zh159 | Posted 2006-09-30 00:44 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
if 写成一行也OK |
|
| Floor9 lxmxn | Posted 2006-10-02 09:21 |
| 版主 Posts 4,938 Credits 11,386 | |
|
Well, this is considered the most streamlined method. Give it a thumbs up first.
|
|
| Floor10 pengfei | Posted 2006-10-02 10:25 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
The algorithm I thought of when dealing with this problem at that time was to first extract the first digit and judge whether its value is zero. This is the first step. Then how to write the code? Getting the value and judging zero are easy. Take the character in the set to get the digit, and a single IF judgment can solve it.
The second step, if there are multiple zeros, a loop and re - assignment are needed. When I thought about it, it seemed that in batch processing, the only way to implement such a loop was goto (and then I thought again whether goto would keep looping, but then I saw that the previous IF was not only a judgment of zero but also played a role in conditional judgment). Here, one IF and one goto form a complete algorithm. The code was written quickly... [ Last edited by pengfei on 2006 - 12 - 7 at 11:58 AM ] |
|
| Floor11 无奈何 | Posted 2006-10-02 12:16 |
| 荣誉版主 Posts 356 Credits 1,338 | |
|
Here's the translation:
Here's a recursive one: |
|
| Floor12 不得不爱 | Posted 2006-10-02 19:58 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
If a number starts with 0, the system considers it as octal, and since there are no 8 or 9 in octal, so an error occurs!
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |