Finally solved the character-digit limit on floating-point arithmetic; in theory it can calculate values in an N-digit range.
Extract the integer and fractional parts of the two numbers separately
Pad the fractional part with 0s on the low end so that the digits of the two numbers line up
Starting from the lowest fractional digit, calculate the sum of the two numbers one digit at a time; if it exceeds 9, carry 1 to the next higher digit, until the highest integer digit
(The calculation in the 47th-floor post gives an error for decimals with 2 decimal points,
32165.464.1654 +1321.654.31654 =33487.1118
The calculation in the 50th-floor post also gives an error when the first character is a decimal point,
Please enter the first number:.31654
Please enter the second number:1.31654
Operand missing.
The syntax of the command is incorrect.)
Neither of those two problems exists in this code. The code in the 47th-floor and 50th-floor posts still needs improvement.
@echo off
setlocal enabledelayedexpansion
if not %1*==* (set num1=%1&if not %2*==* (set num2=%2&goto jmp) ELSE goto err)
ECHO 1
:input
set/p num1=请输入第一个小数:
set/p num2=请输入第二个小数:
:jmp
if %num1%*==* goto err
if %num2%*==* goto err
set err=0
if %num1:~0,1%==. set num1=0%num1%
call :qc "%num1%" num1
if %num2:~0,1%==. set num2=0%num2%
call :qc "%num2%" num2
if %err%==1 goto err
for /f "tokens=1* delims=." %%a in ("%num1%") do (set o1=%%a
set t1=%%b)
for /f "tokens=1* delims=." %%a in ("%num2%") do (set o2=%%a
set t2=%%b)
call :qs0 %o1%
set o1=%sw%
call :qs0 %o2%
set o2=%sw%
call :qw0 %t1%
set t1=%sw%
call :qw0 %t2%
set t2=%sw%
call :js %t1%
set s1=%sw%
call :js %t2%
set s2=%sw%
If %s1% Geq %s2% (set/a s3=%s1%-%s2%
call :j0 !s3! %t2%
set tmp1=%o1%%t1%
set tmp2=%o2%!sw!
) ELSE (set/a s3=%s2%-%s1%
call :j0 !s3! %t1%
set tmp1=%o1%!sw!
set tmp2=%o2%%t2%
set s1=%s2%)
set jg1=0
set jg=
ECHO %tmp1% %tmp2%
:js2
set/a jg0=%tmp1:~-1%+%tmp2:~-1%+%jg1%
set jg1=0%jg0:~0,-1%
set jg=%jg0:~-1%%jg%
set tmp1=%tmp1:~0,-1%
set tmp2=%tmp2:~0,-1%
if %tmp1%*==* (if %tmp2%*==* (if %jg1%==01 (set jg=1%jg%&goto xs) ELSE goto xs) ELSE set tmp1=0&goto js2)
if %tmp2%*==* (if %jg1%==01 (set tmp2=0&goto js2) ELSE (set jg=%tmp1%%jg%&goto xs))
goto js2
:xs
call :qw0 !jg:~-%s1%!
set jg=!jg:~0,-%s1%!.%sw%
if %jg:~-1%==0 set jg=%jg:~0,-2%
:cls
echo 计算结果是:
if not %t1%==0 set o1=%o1%.%t1%
if not %t2%==0 set o2=%o2%.%t2%
echo %o1%+%o2%=%jg%
pause
goto :eof
:qs0
set sw=%1
:qs1
if %sw%==0 goto :eof
if %sw:~0,1%==0 set sw=!sw:~1!&goto qs1
goto :eof
:qw0
set sw=%1
:qw1
if %sw%==0 goto :eof
if %sw:~-1%==0 set sw=!sw:~0,-1!&goto qw1
goto :eof
:js
set sw=0
set sw1=%1
if %sw1%==0 set sw=1&goto :eof
:js1
if not %sw1%*==* set sw1=%sw1:~1%&set/a sw+=1&goto js1
goto :eof
:qc
echo %1|findstr "\.">nul||set %~2=%~1.0
for /f "tokens=1* delims=." %%i in (%1) do echo %%i%%j|findstr "^*$">nul||set err=1
goto :eof
:j0
set sw1=%1
set sw=%2
if %sw1%==0 goto :eof
for /l %%c in (1,1,%sw1%) do set sw=!sw!0
goto :eof
:err
echo 你输入了非法字符^!
echo 请重新输入...
goto input
[
Last edited by qwe1234567 on 2006-10-11 at 09:33 ]