|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『楼 主』:
关于数值运算时的进制问题!
使用 LLM 解释/回答一下
在用SET /A进行运算时, (如果字符的第一位数为零,并且数值中含有8或9), 会导致数值不能正确读取.
因为除十六进制有 0x 前缀, 八进制有 0 前缀的, 数字值为十进位数字。因此, 0x12 与 18 和 022
相同。请注意八进制公式可能很容易搞混: 08 和 09 是无效的数字,因为 8 和 9 不是有效的
八进制位数。
例:
C:\>set /a minute=08+5
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
C:\>set /a minute=5+018
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
C:\>set /a minute=0029+8
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
C:\>set /a minute=125+09
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
解决方法:
1. 在运算常量时, 如果常量中含有8或9, 前面不要加零, 总之最高位数不能为零.
例: 错误 (08, 009, 018, 0029)
正确 (8, 9, 18, 29)
2. 防止变量运算时变量的最高位数为零;
算法:
首先取变量的第一位数, 判断其是否为零;
如果成立,则取出第一位后的余数, 重新赋予变量;
执行循环直到第一位数不为零止;
最后用去零的变量进行运算.
代码:
@echo off
set mu=0001028
echo 去零前=%mu%
:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)
echo 去零后=%mu%
set /a num=%mu%+154
echo 运算结果=%num%
pause
今天在写批处理脚本时遇到了这个问题, 终于把其中的原因弄明白了.
也找到了解决方法, 以后大家遇到相应的问题时可以避免走弯路.
Last edited by pengfei on 2006-10-2 at 09:41 ]
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 ]
|
|
2006-9-5 02:07 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
恩,到没有注意这进制
Well, I didn't notice this number system
|
|
2006-9-5 03:41 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
呵呵,论坛的老贴里隐约提到过首位为0时的处理,一下子找不到那个帖子了。
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.
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-9-5 05:17 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
学习中……
还可以用这种算法判断一个字符串中的空格数``
嘿嘿`
Learning...
You can also use this algorithm to judge the number of spaces in a string.
Hehe`
|
|
2006-9-29 11:14 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
|
2006-9-29 13:08 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
刚才又转到这儿来了,哈哈……
:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)
你写的这个 :again 和 goto again 够酷!
直到把开头的0去尽了直到IF条件自然的就不成立而跳出了跳转继续执新的内容:)
佩服~~~
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~~~
|
|
2006-9-29 22:55 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
深受 pengfei 楼主的 “ :again ... goto again ” 精彩应用的启发,
学着楼主goto原理试着做了一个模拟递归求N!的实验,goto用好了真是方便~:)
再一次感谢 楼主篇“关于数值运算时的进制问题”大作~:)
计算N的阶乘:)
没有越界和没有非法检查,就是为了学习楼主的goto的运用之作~:)
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /p n=请输入 N! :
set /a redtek=!n!
if !n!==0 (echo 0的阶乘为1 && goto :Eof )
:Start
set /a n-=1
if !n!==0 goto :Ok
set /a redtek*=n
goto :Start
:Ok
echo 阶乘为:!redtek!
echo 最大只能计算到 31的阶乘,再大溢出~:)
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~:)
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /p n=Please enter N! :
set /a redtek=!n!
if !n!==0 (echo The factorial of 0 is 1 && goto :Eof )
:Start
set /a n-=1
if !n!==0 goto :Ok
set /a redtek*=n
goto :Start
:Ok
echo The factorial is:!redtek!
echo The maximum can only calculate the factorial of 31, and overflow if larger~:)
|
|
2006-9-30 00:34 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
:again
if "%mu:~0,1%"=="0" set mu=%mu:~1%&goto again
if 写成一行也OK
:again
if "%mu:~0,1%"=="0" set mu=%mu:~1%&goto again
if 写成一行也OK
|
|
2006-9-30 00:44 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
恩,这个算是最精简的一个方法了`
顶一个先``
Well, this is considered the most streamlined method. Give it a thumbs up first.
|
|
2006-10-2 09:21 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
我当时处理这个问题的时候想到的算法是, 先提取第一位, 判断其值是否为零. 这是第一步, 那接下来代码怎么写呢? 取值和判断零都好办, 用set中的字符取位,一个IF判断就可以解决.
第二步, 如果存在多个零就需要用到循环以及重新赋值. 一想批处理中实现这样循环好像只有goto(又想到goto会不会一直循环, 但马上又看到前面一个IF不单是判断零, 还起到条件判断的作用). 到这里, 一个IF一个GOTO就构成了一个完整的算法.代码也很快写出来了...
Last edited by pengfei on 2006-12-7 at 11:58 AM ]
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 ]
|
|
2006-10-2 10:25 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
来个递归的:
@echo off
set x=00010280
call :sub %x%
echo %y%
goto :EOF
:sub
set y=%1
if "%y:~0,1%"=="0" call :sub %y:~1%
goto :EOF
Here's the translation:
Here's a recursive one:
@echo off
set x=00010280
call :sub %x%
echo %y%
goto :EOF
:sub
set y=%1
if "%y:~0,1%"=="0" call :sub %y:~1%
goto :EOF
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-10-2 12:16 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-10-2 19:58 |
|