The code on the 10th floor has been updated; it can remove multiple zeros at the highest digit. Please test...
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!
DigestI
View 41,150 Replies 136
| Rater | Score | Time |
|---|---|---|
| — | +6 | 2006-10-20 20:19 |
Posted by 无奈何 2006-10-04 18:56
- @echo off
- setlocal
- set number1=%~1
- set number2=%~2
- if "%number1%" == "" goto :EOF
- if "%number2%" == "" set number2=0
- set D1=0
- set D2=0
- set flag=0
- set D1=%number1:*.=%
- call set I1=%%number1:.%D1%=%%
- set D2=%number2:*.=%
- call set I2=%%number2:.%D2%=%%
- if "%D1%" == "%number1%" set D1=0
- if "%D2%" == "%number2%" set D2=0
- set D1=1%D1%
- set D2=1%D2%
- call :fill %D1% %D2% D1 D2
- call :sum $%D1% $%D2%
- if "%sum:~0,1%" GTR "2" set /a flag=1
- set D_sum=%sum:~1%
- call :sum $%I1% $%I2%
- set I_sum=%sum%
- echo %number1% + %number2% =
- if %D_sum% EQU 0 (
- echo. %I_sum%
- ) else (
- echo. %I_sum%.%D_sum%
- )
- goto :EOF
- :sum
- set sum=
- set temp1=%1
- set temp2=%2
- if "%flag%" == "1" set H=1
- set n=1
- :next
- call set x=%%temp1:~-%n%,1%%
- call set y=%%temp2:~-%n%,1%%
- if "%x%" == "$" (
- if "%y%" == "$" (
- if "%H%" GTR "0" set sum=%H%%sum%
- goto :EOF
- ))
- if "%x%" == "$" set x=0
- if "%y%" == "$" set y=0
- call :sub %x% %y% %H%
- set sum=%ERRORLEVEL%%sum%
- set /a n+=1
- goto next
- :sub
- set H=0
- if "%1" == "" exit /b 0
- if "%2" == "" exit /b %1
- if "%3" == "" call :sub %1 %2 0 &goto :EOF
- set /a s=%1 + %2 + %3
- set H=%s:~0,-1%
- set L=%s:~-1%
- exit /b %L%
- goto :EOF
- :fill
- setlocal
- set n=-1
- set temp1=%1
- set temp2=%2
- :loop
- set /a n+=1
- call set x=%%temp1:~%n%,1%%
- call set y=%%temp2:~%n%,1%%
- if "%x%" == "" (
- if "%y%" == "" (
- endlocal &set %3=%temp1% &set %4=%temp2%
- goto :EOF
- ))
- if "%x%" == "" set temp1=%temp1%0
- if "%y%" == "" set temp2=%temp2%0
- goto :loop
- goto :EOF
call sum %RANDOM%
call sum %RANDOM% %RANDOM%
call sum .%RANDOM% %RANDOM%
call sum %RANDOM% .%RANDOM%
call sum %RANDOM%.%RANDOM% %RANDOM%
call sum %RANDOM% %RANDOM%.%RANDOM%
call sum %RANDOM%.%RANDOM% %RANDOM%.%RANDOM%
call sum %RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM% %RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%%RANDOM%
pause
Originally posted by pengfei at 2006-10-4 23:50:
Solved the problem where the first digit after the decimal point is 0 and an 8 or 9 appears after it, causing the value to be an invalid number.
A brand-new algorithm; the rough idea is: read the decimal digit count and pad zeros where insufficient. After removing the . sign, decimal and inte ...
| Rater | Score | Time |
|---|---|---|
| xycoordinate | +1 | 2007-03-04 23:16 |
Integer/floating-point segmented calculation code: (For code, please see the 4th floor)
The integer part can calculate up to 9 digits, and the decimal part can calculate up to 8 digits.
The decimal part and integer part together can calculate up to 17 digits.
Otherwise it will overflow or be an invalid number...
Integer/floating-point merged calculation code: (For code, please see the 10th floor)
The integer part can calculate up to 8 digits, and the decimal part can calculate up to 8 digits.
The decimal part and integer part together can calculate up to 9 digits
Otherwise it will overflow or be an invalid number...