标题: [求助]批处理计算出错。
[打印本页]
作者: yiyepianzhou
时间: 2007-10-21 19:22
标题: [求助]批处理计算出错。
我写了一个从1加到任意正数的批处理,代码如下:
@echo off
title 1到大于0的一定数相加
setlocal EnableDelayedExpansion
set /p n=输入下限:
for /l %%b in (1,1,!n!) do set /a c=c+%%b
echo !c!
pause
输入的数如果太大就要算错
比如输入487546
结果就是-1407279457
为什么错了?
作者: slore
时间: 2007-10-21 19:51
有数据范围……
4字节
-2^31~2^31-1
-2147483648~2147483647
作者: yiyepianzhou
时间: 2007-10-22 17:20
应该怎么写?
作者: lxmxn
时间: 2007-10-22 17:59
搜搜以前的老贴子,有计算任何位数的脚本。
作者: yiyepianzhou
时间: 2007-10-22 23:26
谢谢
作者: bjsh
时间: 2007-10-24 13:14
Quote: |
- @echo off &SetLocal EnableDelayedExpansion
- set /p x=请输入被除数:
- set /p y=请输入除数:
- set /p n=请输入欲保留的小数的位数:
- set /a count=0
- set /a bjsh=10
- :loop
- if %count% geq %n% goto start
- set /a bjsh*=10
- set /a count+=1
- goto loop
- :start
- set /a rest=%x%*%bjsh%/%y%-%x%*%bjsh%/10/%y%*10
- if "%n%"=="0" (set /a last=%x%/%y%) else (
- set /a last=%x%*%bjsh%/10/%y%-%x%*%bjsh%/100/%y%*10
- )
- if %rest% geq 5 set /a last+=1
- if "%n%"=="0" echo 结果为%last% & pause & goto exit
- set /a result=%x%/%y%
- set result=%result%.
- set /a end=%n%-1
- set /a bjsh=10
- for /l %%i in (1,1,%end%) do (
- set /a add=%x%*!bjsh!/%y%-%x%*!bjsh!/10/%y%*10
- set result=!result!!add!
- set /a bjsh*=10
- )
- set result=%result%%last%
- echo 结果为%result% & pause
- :exit
BJSH发表于: 2007-10-24 13:07 |
|
研究研究这段代码 对你有帮助
http://www.cn-dos.net/forum/sear ... =yes&sid=nYpctP
[
Last edited by bjsh on 2007-10-24 at 01:20 PM ]
作者: wanqiming
时间: 2008-2-16 21:08
标题: df
dg