After several days of continuous testing and code optimization, I finally made a script that can perform division on N-digit real numbers. But the divisor had better stay within 16 digits, otherwise the waiting time gets too long; the more digits the divisor has, the longer the wait! Please test it....
Sigh, CMD's calculation speed is too slow....
@echo off
cls
title China DOS Union Forum made by qwe1234567
color 17
setlocal enabledelayedexpansion
if not %1*==* (set num1=%1&if not %2*==* (set num2=%2&goto jmp) ELSE goto 错误)
:输入
set num1=
set num2=
echo Real Number Division Script
echo.
echo In theory, this script can perform division on real numbers within N digits
echo.
echo Note: Other than Arabic numerals, the first character may be the "-" sign, and at most one "." is allowed,
echo no other characters are allowed. Otherwise, it will all be treated as invalid input...
echo.
echo China DOS Union Forum made by qwe1234567
set/p num1=Please enter the dividend:
set/p num2=Please enter the divisor:
:jmp
set fh1=
set fh2=
set fh=
if %num1%*==* goto 错误
if %num2%*==* goto 错误
if %num1:~0,1%==- set fh1=-&set num1=%num1:~1%
if %num2:~0,1%==- set fh2=-&set num2=%num2:~1%
if %fh1%*==-* (if %fh2%*==* set fh=-) else if %fh2%*==-* set fh=-
set err=0
echo %num1%|find ".">nul&&set num1=0%num1%0
call :去错 %num1% num1
echo %num2%|find ".">nul&&set num2=0%num2%0
call :去错 %num2% num2
if %err%==1 goto 错误
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 :去首0 %o1%
set o1=%sw%
call :去首0 %o2%
set o2=%sw%
call :去尾0 %t1%
set t1=%sw%
call :去尾0 %t2%
set t2=%sw%
if not %t1%==0 (set o3=%o1%.%t1%) else set o3=%o1%
if not %t2%==0 (set o4=%o2%.%t2%) else set o4=%o2%
if %o4%==0 goto 错误
if %o3%==0 echo The result is 0÷%fh2%%o4%=0&goto 输入
call :计数 %t1%
set s1=%sw%
call :计数 %t2%
set s2=%sw%
If %s1% geq %s2% (set/a s3=s1-s2
call :加尾0 !s3! %t2%
set t2=!sw!
) ELSE (set/a s3=s2-s1
call :加尾0 !s3! %t1%
set t1=!sw!)
set tmp1=%o1%%t1%
set tmp2=%o2%%t2%
call :去首0 %tmp2%
set tmp2=%sw%
call :计数 %sw%
set s2=%sw%
call :去首0 %tmp1%
set tmp1=%sw%
call :计数 %sw%
set s1=%sw%
set/a zsw=s1-s2
call :去尾0 %tmp2%
set tmp2=%sw%
call :计数 %sw%
set s2=%sw%
if %sw% gtr 8 set/a s2=8-sw
set tmp1=%tmp1%0000000000000000000000000000000000000000
set tmp=!tmp1:~0,%sw%!
set sw1=!tmp1:~%sw%!
set jg=
set tmp1=%tmp2:~0,8%
set sw0=!tmp:~0,%s2%!
if %sw% LEQ 8 set/a s2+=1
:开始计算
set/a jg0=sw0/tmp1
:计算
if %jg0%==0 goto 结果
set a1=%tmp2:~-8%
set a2=%tmp2:~0,-8%
set a3=*
set a4=0
set ji=
:乘
if %a1:~0,1%==0 set a1=1%a1%&set a3=1
set/a ji0=jg0*a1+a4
if %a3%==1 set ji=%ji:~-8%
set a4=%ji0:~0,-8%
set ji=%ji0:~-8%%ji%
if %a4%.==. set a4=0
if %a2%.==. goto 减
set a1=%a2:~-8%
set a2=%a2:~0,-8%
goto 乘
:减
if not %a4%==0 set ji=%a4%%ji%
set a1=%tmp%
set jg1=0
set jg2=
:减法
set a2=%a1:~-9%
set a1=%a1:~0,-9%
if not %a1%.==. set a2=1%a2%
set a3=%ji:~-9%
call :去首0 %a3%
set a3=%sw%
set ji=%ji:~0,-9%
set/a jg5=a2-a3+jg1
set/a jg1=0%jg5:~0,-9%-1
if %a1%.==. (if %jg5:~0,1%==- (set jg1==-1) else set jg1=0)
:1
if not %a1%.==. if %jg5:~0,-8%.==. set jg5=0!jg5!&goto 1
set jg2=%jg5:~-9%%jg2%
if %a1%*==* (if %jg1%==-1 (set/a jg0-=1&goto 计算) ELSE set tmp=%jg2%&goto 结果)
goto 减法
:结果
if %sw1%.==. set jg=%jg%%jg0%&goto 显示
set tmp=%tmp%%sw1:~0,1%
call :去首0 %tmp%
set tmp=%sw%
set sw1=%sw1:~1%
set jg=%jg%%jg0%
set sw0=!tmp:~0,%s2%!
goto 开始计算
:显示
if not %jg:~0,1%==0 (set/a zsw+=1)
if %jg:~0,1%==0 set jg=%jg:~1%
if %zsw% gtr 0 goto jxs
for /l %%c in (0,-1,!zsw!) do set jg=0!jg!
set o1=%jg:~0,1%
set o2=%jg:~1%
goto xs
:jxs
set o1=!jg:~0,%zsw%!
set o2=!jg:~%zsw%!
:xs
call :去尾0 %o2%
set o2=%sw%
if %o2%==0 (set jg=%o1%) else set jg=%o1%.%o2%
echo The result is:
echo %fh1%%o3%/%fh2%%o4%=%fh%%jg%
pause
goto :eof
:去首0
set sw=%1
:qs1
if %sw%==0 goto :eof
if %sw:~0,1%==0 set sw=!sw:~1!&goto qs1
goto :eof
:去尾0
set sw=%1
:qw1
if %sw%==0 goto :eof
if %sw:~-1%==0 set sw=!sw:~0,-1!&goto qw1
goto :eof
:计数
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
:去错
echo %1|find ".">nul||set %2=%1.0
for /f "tokens=1* delims=." %%i in ("%1") do echo %%i%%j|findstr "^*$">nul||set err=1
goto :eof
:加尾0
set sw=%2
if %1==0 goto :eof
for /l %%c in (1,1,%1) do set sw=!sw!0
goto :eof
:错误
echo You entered illegal characters, or the divisor is 0!
echo Please re-enter...
goto 输入