@echo off
for %%a in (
"1.1 - 0.9"
"1.12345678987654321 + -9.87654321012345679"
"6.6543212345678 - 5.5432101234567"
"-2 - 2.4948463537"
"-93701350643248383.03974234214254643221455737 - 364354503.67365700005267589864236576"
"-0.4253500 - -5762.4948463537"
"0 - 0"
"232.556 - 232.556"
"0 + 0"
"0 - 234.342250988"
"-3.4536 - 3456"
"325689088000004600 - 94872734352.487409900094848377236474830937"
"-34.435464573575735 + -3425.0298576283039836"
"4 - 1000000.0000001"
) do (
for /f "tokens=1-3" %%i in (%%a) do (
setlocal enabledelayedexpansion
rem 由于cmd自身的各种限制,不做过多考虑,暂支持两个长度在4000位以内不分正负的整数或小数的加减运算
rem 用法call :calc num1 [+/-] num2 result
call :calc %%i %%j %%k result
echo;%%~a
echo;= !result!
echo;
endlocal
)
)
pause&exit
:calc
rem 屏蔽数字合法性检测可提高效率
echo;%~1|findstr "^-0\.0*[1-9][0-9]*$ ^0\.0*[1-9][0-9]*$ ^0$ ^-[1-9][0-9]*$ ^[1-9][0-9]*$ ^-[1-9][0-9]*\.[0-9][0-9]*$ ^[1-9][0-9]*\.[0-9][0-9]*$">nul||set n=1
echo;%~3|findstr "^-0\.0*[1-9][0-9]*$ ^0\.0*[1-9][0-9]*$ ^0$ ^-[1-9][0-9]*$ ^[1-9][0-9]*$ ^-[1-9][0-9]*\.[0-9][0-9]*$ ^[1-9][0-9]*\.[0-9][0-9]*$">nul||set n=1
if defined n (set "%~4=数字不合法"&goto :eof)
if "%~2" neq "+" if "%~2" neq "-" (set "%~4=算术运算符不正确"&goto :eof)
if "%~4" equ "" (set "%~4=缺少结果变量"&goto :eof)
if "%~1" equ "0" (
if "%~3" equ "0" (set "%~4=0") else (
set a=%~3
if "%~2" equ "+" (
set "%~4=%~3"
) else (
if "!a:~,1!" equ "-" (set "%~4=!a:~1!") else (set "%~4=-%~3")
)
)
goto :eof
)
if "%~3" equ "0" (set "%~4=%~1"&goto :eof)
if "%~1" equ "%~3" if "%~2" equ "-" (set "%~4=0"&goto :eof)
set a=%~1.0
set b=%~3.0
for /f "tokens=1,2 delims=." %%a in ("%a:-=%") do set "a_1=%%a"&set "a_2=%%b"
for /f "tokens=1,2 delims=." %%a in ("%b:-=%") do set "b_1=%%a"&set "b_2=%%b"
call :strlen %a_1% L1_1
call :strlen %a_2% L1_2
call :strlen %b_1% L2_1
call :strlen %b_2% L2_2
for %%i in (1 2) do (
set "zero="&set m=0
if !L1_%%i! leq !L2_%%i! (
set /a m=L2_%%i-L1_%%i
if !m! neq 0 (
for /l %%a in (1 1 !m!) do set zero=!zero!0
)
if "%%i" equ "1" (set a_%%i=!zero!!a_%%i!) else set a_%%i=!a_%%i!!zero!
set Len_%%i=!L2_%%i!
) else (
set /a m=L1_%%i-L2_%%i
for /l %%a in (1 1 !m!) do set zero=!zero!0
if "%%i" equ "1" (set b_%%i=!zero!!b_%%i!) else set b_%%i=!b_%%i!!zero!
set Len_%%i=!L1_%%i!
)
)
set /a Len=Len_1+Len_2+1
if "%~2" equ "+" (
if "!a:~,1!" neq "-" (
if "!b:~,1!" neq "-" (
call :jia %a_1%.%a_2% %b_1%.%b_2% %Len% s
set "%~4=!s!"
) else (
call :jian %a_1%.%a_2% %b_1%.%b_2% %Len% s
if "%a_1%.%a_2%" gtr "%b_1%.%b_2%" (set "%~4=!s!") else set "%~4=-!s!"
)
) else (
if "!b:~,1!" neq "-" (
call :jian %a_1%.%a_2% %b_1%.%b_2% %Len% s
if "%a_1%.%a_2%" gtr "%b_1%.%b_2%" (set "%~4=-!s!") else set "%~4=!s!"
) else (
call :jia %a_1%.%a_2% %b_1%.%b_2% %Len% s
set "%~4=-!s!"
)
)
) else (
if "!a:~,1!" neq "-" (
if "!b:~,1!" neq "-" (
call :jian %a_1%.%a_2% %b_1%.%b_2% %Len% s
if "%a_1%.%a_2%" lss "%b_1%.%b_2%" (set "%~4=-!s!") else set "%~4=!s!"
) else (
call :jia %a_1%.%a_2% %b_1%.%b_2% %Len% s
set "%~4=!s!"
)
) else (
if "!b:~,1!" neq "-" (
call :jia %a_1%.%a_2% %b_1%.%b_2% %Len% s
set "%~4=-!s!"
) else (
call :jian %a_1%.%a_2% %b_1%.%b_2% %Len% s
if "%a_1%.%a_2%" lss "%b_1%.%b_2%" (set "%~4=!s!") else set "%~4=-!s!"
)
)
)
goto :eof
:strlen
setlocal
set "$=%1#"
set len=&for %%a in (4000 2048 1024 512 256 128 64 32 16)do if !$:~%%a!. neq . set/a len+=%%a&set $=!$:~%%a!
set $=!$!fedcba9876543210&set/a len+=0x!$:~16,1!
endlocal&set %2=%len%&goto :eof
:jia
setlocal
set a=%~1
set b=%~2
set t=0
set "s="
for /l %%a in (-1 -1 -%~3) do (
if "!a:~%%a,1!" equ "." (
set s=.!s!
) else (
set /a "c=t+!a:~%%a,1!+!b:~%%a,1!"
if !c! geq 10 (set t=1) else set t=0
set s=!c:~-1!!s!
)
)
if %t% equ 1 (set s=1!s!)
for /f "tokens=1,2 delims=." %%a in ("%s%") do (
for /f "tokens=1* delims=0" %%c in (".%%b") do if "%%c%%d" equ "." set s=%%a
)
endlocal&set %~4=%s%&goto :eof
:jian
setlocal
if "%~1" lss "%~2" (
set a=%~2
set b=%~1
) else (
set a=%~1
set b=%~2
)
set t=0
set "s="
for /l %%a in (-1 -1 -%~3) do (
if "!a:~%%a,1!" equ "." (
set s=.!s!
) else (
set /a "c=10+!a:~%%a,1!-!b:~%%a,1!-t"
if !c! lss 10 (set t=1) else set t=0
set s=!c:~-1!!s!
)
)
for /f "tokens=1,2 delims=." %%a in ("%s%") do (
for /f "tokens=* delims=0" %%c in ("%%a") do if "%%c" equ "" (set pre=0) else set pre=%%c
for /f "tokens=* delims=0" %%c in ("%%b") do if "%%c" equ "" (set s=!pre!) else set s=!pre!.%%b
)
endlocal&set %~4=%s%&goto :eof