更新在bat文件中运行unsigned long long sum

时间:2017-05-31 07:03:34

标签: batch-file

是否有一种简单的方法可以在.BAT文件中将两个数字加起来> = 2 * 31? 我有一个运行总和,参数%1是现有文件的名称。

set sum=4123456789
set fsize=%~z1

我想将fsize添加到sum中。不幸的是,fsize(和sum)可以小到零或10千兆字节(%~z1 准确报告> = 2 * 31文件大小)。

我知道一个程序可以做到,如果有必要的话我会走那条路,但是我更喜欢用几行.BAT逻辑来做。

2 个答案:

答案 0 :(得分:1)

我认为最简单的方法是将加数分成两部分 - 整数和分数千兆(over::: <h1>2.5</h1> <table id=""> <tr> <td>balls:::1</td> <td>runs:::0</td> </tr> <tr> <td>balls:::2</td> <td>runs:::0</td> </tr> <tr> <td>balls:::3</td> <td>runs:::1</td> </tr> <tr> <td>balls:::4</td> <td>runs:::0</td> </tr> <tr> <td>balls:::5</td> <td>runs:::1</td> </tr> </table> 的倍数),分别添加各个部分,然后重新组合它们。请参阅以下示例脚本,其中包含许多解释性说明:

1000000000

最大可能总和为@echo off setlocal EnableExtensions DisableDelayedExpansion rem // Initialise variables: set /A "GIGASUM=0, FRACSUM=0" :LOOP rem // Get command line argument: shift set "NUM=%~0" if not defined NUM goto :NEXT rem // Check number for validity: (for /F "delims=0123456789" %%N in ("%NUM%") do rem/) && ( echo ERROR: non-numeric characters encountered! exit /B 1 ) rem // Split number into two parts, integer and fractional Gigas: set "NUM=000000000%NUM%" set "GIGA=%NUM:~,-9%" & set "FRAC=%NUM:~-9%" rem // Remove leading zeros from integer Gigas: for /F "tokens=* delims=0" %%N in ("%GIGA%") do set "GIGA=%%N" rem // Sum up fractional Gigas and determine carry: set /A "FRACSUM+=(1%FRAC%-1000000000)" set "CARRY=%FRACSUM:~,-9%" & set "FRACSUM=000000000%FRACSUM%" set "FRACSUM=%FRACSUM:~-9%" rem // Sum up integer Gigas and regard carry: set /A "GIGASUM+=GIGA+CARRY" rem // Loop back to next summand: goto :LOOP :NEXT rem // Remove leading zeros: for /F "tokens=* delims=0" %%N in ("%GIGASUM%%FRACSUM%") do set "SUM=%%N" if not defined SUM set "SUM=0" rem // Return resulting sum: echo %SUM% endlocal exit /B ,未检测到溢出。

答案 1 :(得分:0)

所需的逻辑并不复杂。这是一个版本:

@echo off
setlocal

:loop
set /p "pair=Enter two numbers separated by plus sign: "
if errorlevel 1 goto :EOF

for /F "tokens=1,2 delims=+" %%a in ("%pair%") do set "num1=%%a" & set "num2=%%b"
set "sum="
set "carry=0"
:nextDigit
   set /A sum1=%num1:~-1%+%num2:~-1%+carry
   set "sum=%sum1:~-1%%sum%"
   set /A carry=sum1/10
   set "num1=%num1:~0,-1%"
   if not defined num1 set "num1=0"
   set "num2=%num2:~0,-1%"
   if not defined num2 set "num2=0"
if "%carry%%num1%%num2%" neq "000" goto nextDigit

echo The sum is: %sum%
goto loop