批处理实现平方根运算

批处理实现平方根运算

在批处理当中实现全精度平方根运算,具有一定难度。本批处理平方根运算器实现平方根运算主要分为以下模块:
1.整数平方根运算模块

rem <开根方案>
:decimal_solution
    setlocal
    set num=%1
    set tnum=%1
    :: 计算长度,判断需要截取的目标长度(1 or 2)
    call :length %num% len
    set /a mod=len %% 2, skip = 2 - mod
    set target=!tnum:~0,%skip%!
    set tnum=!tnum:~%skip%!
    set "base="

    :: prec 当前精度
    set /a prec = 0, base_len=0, target_len=skip

    :dec_loop
        :: 推算下一个数
        :estimate
            :: 如果目标值 小于 基数,下一个数字判定为0
            call :cmp %target% %base%0 %target_len% %base_len%+1 cmp
            if !cmp! equ -1 (
                set /a mid=0, mp=0, mplen=0
                goto :out_estimate
            )

            if %base_len% gtr 5 (
                set /a est=!target:~0,6!/!base:~0,5!
            ) else (
                :: 在set/a计算范围内的,[粗暴]遍历
                for /l %%a in (0,1,10) do (
                    set /a mp=%base%%%a*%%a
                    if !mp! gtr !target! (set /a est=%%a-1 &goto :out_est_for)
                )
            )
            :out_est_for

            :: 199999996400/1999999988 = 99.9999988
            :: but 199999/19999 = 10
            if %est% geq 10 (
                set /a tbase_len=base_len+1
                if !target_len! gtr !tbase_len! (set /a est=9)
            )

            set /a mid=!est:~0,1!
            call :bignum_mp_single !base!!mid! !mid! !base_len!+1 1 mp mplen
            call :cmp !mp! !target! !mplen! !target_len! cmp
            :: 如果mp超出目标范围
            if !cmp! equ 1 (
                set /a mid-=1
                call :bignum_mp_single !base!!mid! !mid! !base_len!+1 1 mp mplen
            )
        :out_estimate

        set /p inp="%mid%"<nul
        rem echo,&echo tg !target!, mp !mp!, base !base!, mid !mid!, est !est!
        if "%tnum%" == "" (
            :: 如果target只剩下 00,方案结束
            if "%target%" == "00" ( goto :dec_loop_out )
            if %cmp% == 0 ( goto :dec_loop_out )
        )

        :: 计算下一段target的值
        call :bignum_minus %target% %mp% %target_len% %mplen% target target_len

        :: 扩充target,如果被开根数已经截取完,直接补0,精度+1
        if %skip% geq %len% (
            set target=%target%00
            set /a prec+=1
            if !prec! equ 1 set /p inp="."<nul
        ) else (
            if "%target%" == "0" (set target=!tnum:~0,2!
                          ) else (set target=!target!!tnum:~0,2!)
            set tnum=!tnum:~2!
            set /a skip+=2
        )
        set /a target_len+=2

        :: 更新基数 - base
        rem base=base*10+mid*2
        if "%base%" == "0" (
            set /a base=mid*2, base_len=1+base/10
        ) else (
            set /a db_mid=mid*2, dbmidlen=1+db_mid/10
            call :bignum_plus !base!0 !db_mid! !base_len!+1 !dbmidlen! base base_len
        )

    if %prec% leq %pr% (goto :dec_loop)
    :dec_loop_out
    rem echo,
    endlocal
    goto :eof

:: 比较
:cmp
    set /a La=%3, Lb=%4
    if %La% gtr %Lb% (set /a %5=1&goto :eof)
    if %La% lss %Lb% (set /a %5=-1&goto :eof)
    :: 如果长度相同,直接按字符串对比
    if "%1" gtr "%2" (set /a %5=1&goto :eof)
    if "%1" lss "%2" (set /a %5=-1&goto :eof)
    if "%1" equ "%2" (set /a %5=0&goto :eof)

:: 大数 乘以 单位数
:bignum_mp_single
    setlocal
    set num_a=%1
    set num_b=%2
    set /a pool = 0, maxid = %3
    set "res="
    for /l %%a in ( 1, 1, %maxid% ) do (
        set /a mp = !num_a:~-%%a,1! * num_b + pool, t = mp %% 10, pool = mp / 10
        set res=!t!!res!
    )

    if %pool% neq 0 (
        set /a maxid+=1
        set res=!pool!!res!
    )
    endlocal&set %5=%res%&set %6=%maxid%
    goto :eof

::大数加法
:bignum_plus
    setlocal
    set num_a=%1
    set num_b=%2
    set /a len_a=%3, len_b=%4
    set /a max = len_a
    if %len_b% gtr %len_a% (set /a max=len_b, len_b=len_a&set num_a=%num_b%&set num_b=%num_a%)
    set /a pool=0
    set res=
    for /l %%n in ( 1, 1, %max% ) do (
        if %%n leq %len_b% (
            set /a t = !num_a:~-%%n,1! + !num_b:~-%%n,1! + pool
        ) else (
            set /a t = !num_a:~-%%n,1! + pool
        )
        set /a mod = t %% 10, pool = t / 10
        set res=!mod!!res!
    )
    if %pool% gtr 0 (set /a max+=1 &set res=1%res%)
    endlocal &set %5=%res%&set %6=%max%
    goto :eof

::大数减法
:bignum_minus
    setlocal
    set num_a=%1
    set num_b=%2
    set /a len_a=%3, len_b=%4
    set /a max = len_a
    if %len_b% gtr %len_a% (set /a max=len_b, len_b=len_a&set num_a=%num_b%&set num_b=%num_a%)

    set /a minus = 0
    set "res="
    for /l %%n in ( 1, 1, %max% ) do (
        if %%n leq %len_b% (
            set /a dt = !num_a:~-%%n,1! - !num_b:~-%%n,1! - minus
        ) else (
            set /a dt = !num_a:~-%%n,1! - minus
        )
        if !dt! lss 0 (
            set /a t = dt + 10, minus=1
        ) else (
            set /a t = dt, minus=0
        )
        set res=!t!!res!
        if !t! equ 0 (set /a zero+=1) else (set /a zero=0)
    )
    set res=!res:~%zero%!
    endlocal &set %5=%res%&set /a %6=%max%-%zero%
    goto :eof

2.小数相乘模块

def MULTIPLICATION():
{
	:MULTIPLICATION <浮点乘表达式>
	set "expression=%1"
	if "!CHECK_ENABLE_DELAYED_EXPANSION!"=="%CHECK_ENABLE_DELAYED_EXPANSION%" (SETLOCAL) else (SETLOCAL ENABLEDELAYEDEXPANSION)
	set RESULT=1&set "expression=%expression:×= %"
	for %%a in (!expression!) do (
		CALL :CALCULATE !RESULT! %%a RESULT
	)
	echo !RESULT!
	ENDLOCAL
	GOTO :EOF
}
def CALCULATE():
{
	:CALCULATE <被乘数> <乘数> []
	for /f "tokens=1,2 delims=." %%a in ("%1") do (
		set A1=%%a&set A=!A1!%%b
		if "%%b"=="" (set PA=0) else (
			set A2=%%b
			for %%i in (512 256 128 64 32 16 8 4 2 1) do (
    				if not "!A2:~%%i!"=="" (
					set/a PA+=%%i
					set "A2=!A2:~%%i!"
				)
			)
			if "!A2:~1!"=="" (set/a PA+=1)
		)
	)
	for /f "tokens=1,2 delims=." %%a in ("%2") do (
		set B1=%%a&set B=!B1!%%b
		if "%%b"=="" (set PB=0) else (
			set B2=%%b
			for %%i in (512 256 128 64 32 16 8 4 2 1) do (
    				if not "!B2:~%%i!"=="" (
					set/a PB+=%%i
					set "B2=!B2:~%%i!"
				)
			)
			if "!B2:~1!"=="" (set/a PB+=1)
		)
	)
	CALL :CUTNUM !A! A NA
	CALL :CUTNUM !B! B NB
	set/a "N=NA+NB,PO=PA+PB"
	for /l %%i in (1 1 !N!) do (
		for /l %%j in (1 1 %%i) do (
			set/a j=%%i-%%j+1
			if defined A[%%j] (
				if defined B[!j!] (
					set/a sum=A[%%j]*B[!j!]+sum
				)
			)
		)
		set/a s=sum+1000
		set sum=!sum:~0,-3!
		set pul=!s:~-3!!pul!
	)
	if !PO! equ 0 (
		for /l %%i in (1 1 10) do (
			if "!pul:~0,1!"=="0" (
				set pul=!pul:~1!
			)
		)
		set "%3=!pul!"
	) else (
		set pre=!pul:~0,-%PO%!
		for /l %%i in (1 1 20) do (
			if "!pre:~0,1!"=="0" (
				set pre=!pre:~1!
			)
		)
		if not defined pre (set pre=0)
		set "%3=!pre!.!pul:~-%PO%!
	)
	for /l %%i in (1 1 !N!) do (set "A[%%i]="&set "B[%%i]=")&set "pul="&set/a "PA=0,PB=0,PO=0"
	GOTO :EOF
}

3.整数化小数模块

set zfhe=%rootx:~0,1%
for /f "delims=-" %%a in ("%rootx%") do set rootx=%%a
set serial=%rootx:~0,1%
if not "%serial%"=="0" if not "%serial%"=="1" if not "%serial%"=="2" if not "%serial%"=="3" if not "%serial%"=="4" if not "%serial%"=="5" if not "%serial%"=="6" if not "%serial%"=="7" if not "%serial%"=="8" if not "%serial%"=="9" if /I not "%serial%"=="Y" goto error
for /f "tokens=1,2 delims=." %%a in ("%rootx%") do (
set rootx=%%a%%b
set input=%%b
set serial=%%a
)
for /f "delims=0 tokens=*" %%i in ("%rootx%") do set rootx=%%i
if "%rootx%"=="" (
echo 0
goto sqrtend
)
set serial=
set n=0
if "%input%"=="" goto string2
:stringl
call set u=%%input:~%n%,1%%
if not "%u%"=="" (set/a n+=1
goto stringl
)
goto end
:string2
set n=0
:end
set /a var=%n%%%2
setlocal enabledelayedexpansion
if %var% equ 0 (
set joconfig=0
goto ounumber
) else (
set joconfig=1
goto jinumber
)
set onum=
:ounumber
if not "%n%"=="0" set /a thjtg=%n%/2-1
if "%n%"=="0" goto end1
if not "%thjtg%"=="0" for /l %%i in (1,1,%thjtg%) do set onum=0!onum!
echo 0.%onum%1>temp3.ini
goto end1
:jinumber
set /a thjtg=(%n%-1)/2
if not "%thjtg%"=="0" for /l %%i in (1,1,%thjtg%) do set onum=0!onum!
echo 0.%onum%1>temp3.ini
:end1
rem 创建用于计算字符串长度的模板,长度限制为 2^pow
set "sharp=#"
set "serial=9876543210"
set /a pow=11, maxlen=1^<^<pow
for /l %%a in (1,1,%pow%) do set sharp=!sharp!!sharp!
if "%testconfig%"=="1" goto test
if "%n%"=="0" (
call :decimal_solution %rootx%
if "%zfhe%"=="-" echo i
if not "%zfhe%"=="-" echo.
goto sqrtend
)
call :decimal_solution %rootx% >temp1.ini
set /p sqrti=<temp1.ini
if exist temp3.ini set /p onum=<temp3.ini
if "%joconfig%"=="0" CALL :MULTIPLICATION %sqrti%×%onum% >temp1.ini
if "%joconfig%"=="1" (
call :decimal_solution 10>temp2.ini
set /p sqrtxs=<temp2.ini
CALL :MULTIPLICATION %sqrti%×!sqrtxs!×%onum% >temp1.ini
)
endlocal
set /p sqrti=<temp1.ini
for /f "tokens=1,2 delims=." %%a in ("%sqrti%") do (
set rootx=%%a
set sqrti=%%b
)
for /f "tokens=* delims=0" %%i in ("%rootx%") do set rootx=%%i
if "%rootx%"=="" set rootx=0
call set sqrti=%%sqrti:~0,%pr%%%
if not "%zfhe%"=="-" echo %rootx%.%sqrti%
if "%zfhe%"=="-" echo %rootx%.%sqrti%i
:sqrtend
set config2=
set expression=
set input=
set joconfig=
set n=
set onum=
set pr=
set rootx=
set sqrti=
set sqrtxs=
set thjtg=
set var=
set zfhe=
set maxlen=
set pow=
set serial=
set sharp=
if exist temp1.ini del temp1.ini
if exist temp2.ini del temp2.ini
if exist temp3.ini del temp3.ini
if "%config%"=="1" pause
exit /b

4.外部调用模块

@echo off
if "%1"=="/?" goto help
if "%1"=="--help" goto help
if "%1"=="--version" goto version
if not "%1"=="" set rootx=%1
if "%1"=="" set /p rootx=请输入被开方数:
if "%1"=="" set config=1
if not "%2"=="" set pr=%2
if "%2"=="" set pr=3
if /I "%1"=="Y" set testconfig=1

整体代码如下:

@echo off
if "%1"=="/?" goto help
if "%1"=="--help" goto help
if "%1"=="--version" goto version
if not "%1"=="" set rootx=%1
if "%1"=="" set /p rootx=请输入被开方数:
if "%1"=="" set config=1
if not "%2"=="" set pr=%2
if "%2"=="" set pr=3
if /I "%1"=="Y" set testconfig=1
rem __________________任意实数开根板块________________________
set zfhe=%rootx:~0,1%
for /f "delims=-" %%a in ("%rootx%") do set rootx=%%a
set serial=%rootx:~0,1%
if not "%serial%"=="0" if not "%serial%"=="1" if not "%serial%"=="2" if not "%serial%"=="3" if not "%serial%"=="4" if not "%serial%"=="5" if not "%serial%"=="6" if not "%serial%"=="7" if not "%serial%"=="8" if not "%serial%"=="9" if /I not "%serial%"=="Y" goto error
for /f "tokens=1,2 delims=." %%a in ("%rootx%") do (
set rootx=%%a%%b
set input=%%b
set serial=%%a
)
for /f "delims=0 tokens=*" %%i in ("%rootx%") do set rootx=%%i
if "%rootx%"=="" (
echo 0
goto sqrtend
)
set serial=
set n=0
if "%input%"=="" goto string2
:stringl
call set u=%%input:~%n%,1%%
if not "%u%"=="" (set/a n+=1
goto stringl
)
goto end
:string2
set n=0
:end
set /a var=%n%%%2
setlocal enabledelayedexpansion
if %var% equ 0 (
set joconfig=0
goto ounumber
) else (
set joconfig=1
goto jinumber
)
set onum=
:ounumber
if not "%n%"=="0" set /a thjtg=%n%/2-1
if "%n%"=="0" goto end1
if not "%thjtg%"=="0" for /l %%i in (1,1,%thjtg%) do set onum=0!onum!
echo 0.%onum%1>temp3.ini
goto end1
:jinumber
set /a thjtg=(%n%-1)/2
if not "%thjtg%"=="0" for /l %%i in (1,1,%thjtg%) do set onum=0!onum!
echo 0.%onum%1>temp3.ini
:end1
rem 创建用于计算字符串长度的模板,长度限制为 2^pow
set "sharp=#"
set "serial=9876543210"
set /a pow=11, maxlen=1^<^<pow
for /l %%a in (1,1,%pow%) do set sharp=!sharp!!sharp!
if "%testconfig%"=="1" goto test
if "%n%"=="0" (
call :decimal_solution %rootx%
if "%zfhe%"=="-" echo i
if not "%zfhe%"=="-" echo.
goto sqrtend
)
call :decimal_solution %rootx% >temp1.ini
set /p sqrti=<temp1.ini
if exist temp3.ini set /p onum=<temp3.ini
if "%joconfig%"=="0" CALL :MULTIPLICATION %sqrti%×%onum% >temp1.ini
if "%joconfig%"=="1" (
call :decimal_solution 10>temp2.ini
set /p sqrtxs=<temp2.ini
CALL :MULTIPLICATION %sqrti%×!sqrtxs!×%onum% >temp1.ini
)
endlocal
set /p sqrti=<temp1.ini
for /f "tokens=1,2 delims=." %%a in ("%sqrti%") do (
set rootx=%%a
set sqrti=%%b
)
for /f "tokens=* delims=0" %%i in ("%rootx%") do set rootx=%%i
if "%rootx%"=="" set rootx=0
call set sqrti=%%sqrti:~0,%pr%%%
if not "%zfhe%"=="-" echo %rootx%.%sqrti%
if "%zfhe%"=="-" echo %rootx%.%sqrti%i
:sqrtend
set config2=
set expression=
set input=
set joconfig=
set n=
set onum=
set pr=
set rootx=
set sqrti=
set sqrtxs=
set thjtg=
set var=
set zfhe=
set maxlen=
set pow=
set serial=
set sharp=
if exist temp1.ini del temp1.ini
if exist temp2.ini del temp2.ini
if exist temp3.ini del temp3.ini
if "%config%"=="1" pause
exit /b

def MULTIPLICATION():
{
	:MULTIPLICATION <浮点乘表达式>
	set "expression=%1"
	if "!CHECK_ENABLE_DELAYED_EXPANSION!"=="%CHECK_ENABLE_DELAYED_EXPANSION%" (SETLOCAL) else (SETLOCAL ENABLEDELAYEDEXPANSION)
	set RESULT=1&set "expression=%expression:×= %"
	for %%a in (!expression!) do (
		CALL :CALCULATE !RESULT! %%a RESULT
	)
	echo !RESULT!
	ENDLOCAL
	GOTO :EOF
}
def CALCULATE():
{
	:CALCULATE <被乘数> <乘数> []
	for /f "tokens=1,2 delims=." %%a in ("%1") do (
		set A1=%%a&set A=!A1!%%b
		if "%%b"=="" (set PA=0) else (
			set A2=%%b
			for %%i in (512 256 128 64 32 16 8 4 2 1) do (
    				if not "!A2:~%%i!"=="" (
					set/a PA+=%%i
					set "A2=!A2:~%%i!"
				)
			)
			if "!A2:~1!"=="" (set/a PA+=1)
		)
	)
	for /f "tokens=1,2 delims=." %%a in ("%2") do (
		set B1=%%a&set B=!B1!%%b
		if "%%b"=="" (set PB=0) else (
			set B2=%%b
			for %%i in (512 256 128 64 32 16 8 4 2 1) do (
    				if not "!B2:~%%i!"=="" (
					set/a PB+=%%i
					set "B2=!B2:~%%i!"
				)
			)
			if "!B2:~1!"=="" (set/a PB+=1)
		)
	)
	CALL :CUTNUM !A! A NA
	CALL :CUTNUM !B! B NB
	set/a "N=NA+NB,PO=PA+PB"
	for /l %%i in (1 1 !N!) do (
		for /l %%j in (1 1 %%i) do (
			set/a j=%%i-%%j+1
			if defined A[%%j] (
				if defined B[!j!] (
					set/a sum=A[%%j]*B[!j!]+sum
				)
			)
		)
		set/a s=sum+1000
		set sum=!sum:~0,-3!
		set pul=!s:~-3!!pul!
	)
	if !PO! equ 0 (
		for /l %%i in (1 1 10) do (
			if "!pul:~0,1!"=="0" (
				set pul=!pul:~1!
			)
		)
		set "%3=!pul!"
	) else (
		set pre=!pul:~0,-%PO%!
		for /l %%i in (1 1 20) do (
			if "!pre:~0,1!"=="0" (
				set pre=!pre:~1!
			)
		)
		if not defined pre (set pre=0)
		set "%3=!pre!.!pul:~-%PO%!
	)
	for /l %%i in (1 1 !N!) do (set "A[%%i]="&set "B[%%i]=")&set "pul="&set/a "PA=0,PB=0,PO=0"
	GOTO :EOF
}
def CUTNUM():
{
	:CUTNUM <待切分数> <数据类型> [切分组数]
	set num=000%1
	if "!num:~-3!"=="!num:~-4!" (
		set %2[1]=!num!
		set %3=1
		GOTO :EOF
	)
	for /l %%i in (1 1 365) do (
		if "!num:~0,-3!"=="" (
			set/a %2[%%i]=!num!
			set %3=%%i
			GOTO :EOF
		)
		set/a %2[%%i]=1!num:~-3!-1000
		set num=!num:~0,-3%!
	)
	GOTO :EOF
}
rem <开根方案>
:decimal_solution
    setlocal
    set num=%1
    set tnum=%1
    :: 计算长度,判断需要截取的目标长度(1 or 2)
    call :length %num% len
    set /a mod=len %% 2, skip = 2 - mod
    set target=!tnum:~0,%skip%!
    set tnum=!tnum:~%skip%!
    set "base="

    :: prec 当前精度
    set /a prec = 0, base_len=0, target_len=skip

    :dec_loop
        :: 推算下一个数
        :estimate
            :: 如果目标值 小于 基数,下一个数字判定为0
            call :cmp %target% %base%0 %target_len% %base_len%+1 cmp
            if !cmp! equ -1 (
                set /a mid=0, mp=0, mplen=0
                goto :out_estimate
            )

            if %base_len% gtr 5 (
                set /a est=!target:~0,6!/!base:~0,5!
            ) else (
                :: 在set/a计算范围内的,[粗暴]遍历
                for /l %%a in (0,1,10) do (
                    set /a mp=%base%%%a*%%a
                    if !mp! gtr !target! (set /a est=%%a-1 &goto :out_est_for)
                )
            )
            :out_est_for

            :: 199999996400/1999999988 = 99.9999988
            :: but 199999/19999 = 10
            if %est% geq 10 (
                set /a tbase_len=base_len+1
                if !target_len! gtr !tbase_len! (set /a est=9)
            )

            set /a mid=!est:~0,1!
            call :bignum_mp_single !base!!mid! !mid! !base_len!+1 1 mp mplen
            call :cmp !mp! !target! !mplen! !target_len! cmp
            :: 如果mp超出目标范围
            if !cmp! equ 1 (
                set /a mid-=1
                call :bignum_mp_single !base!!mid! !mid! !base_len!+1 1 mp mplen
            )
        :out_estimate

        set /p inp="%mid%"<nul
        rem echo,&echo tg !target!, mp !mp!, base !base!, mid !mid!, est !est!
        if "%tnum%" == "" (
            :: 如果target只剩下 00,方案结束
            if "%target%" == "00" ( goto :dec_loop_out )
            if %cmp% == 0 ( goto :dec_loop_out )
        )

        :: 计算下一段target的值
        call :bignum_minus %target% %mp% %target_len% %mplen% target target_len

        :: 扩充target,如果被开根数已经截取完,直接补0,精度+1
        if %skip% geq %len% (
            set target=%target%00
            set /a prec+=1
            if !prec! equ 1 set /p inp="."<nul
        ) else (
            if "%target%" == "0" (set target=!tnum:~0,2!
                          ) else (set target=!target!!tnum:~0,2!)
            set tnum=!tnum:~2!
            set /a skip+=2
        )
        set /a target_len+=2

        :: 更新基数 - base
        rem base=base*10+mid*2
        if "%base%" == "0" (
            set /a base=mid*2, base_len=1+base/10
        ) else (
            set /a db_mid=mid*2, dbmidlen=1+db_mid/10
            call :bignum_plus !base!0 !db_mid! !base_len!+1 !dbmidlen! base base_len
        )

    if %prec% leq %pr% (goto :dec_loop)
    :dec_loop_out
    rem echo,
    endlocal
    goto :eof

:: 比较
:cmp
    set /a La=%3, Lb=%4
    if %La% gtr %Lb% (set /a %5=1&goto :eof)
    if %La% lss %Lb% (set /a %5=-1&goto :eof)
    :: 如果长度相同,直接按字符串对比
    if "%1" gtr "%2" (set /a %5=1&goto :eof)
    if "%1" lss "%2" (set /a %5=-1&goto :eof)
    if "%1" equ "%2" (set /a %5=0&goto :eof)

:: 大数 乘以 单位数
:bignum_mp_single
    setlocal
    set num_a=%1
    set num_b=%2
    set /a pool = 0, maxid = %3
    set "res="
    for /l %%a in ( 1, 1, %maxid% ) do (
        set /a mp = !num_a:~-%%a,1! * num_b + pool, t = mp %% 10, pool = mp / 10
        set res=!t!!res!
    )

    if %pool% neq 0 (
        set /a maxid+=1
        set res=!pool!!res!
    )
    endlocal&set %5=%res%&set %6=%maxid%
    goto :eof

::大数加法
:bignum_plus
    setlocal
    set num_a=%1
    set num_b=%2
    set /a len_a=%3, len_b=%4
    set /a max = len_a
    if %len_b% gtr %len_a% (set /a max=len_b, len_b=len_a&set num_a=%num_b%&set num_b=%num_a%)
    set /a pool=0
    set res=
    for /l %%n in ( 1, 1, %max% ) do (
        if %%n leq %len_b% (
            set /a t = !num_a:~-%%n,1! + !num_b:~-%%n,1! + pool
        ) else (
            set /a t = !num_a:~-%%n,1! + pool
        )
        set /a mod = t %% 10, pool = t / 10
        set res=!mod!!res!
    )
    if %pool% gtr 0 (set /a max+=1 &set res=1%res%)
    endlocal &set %5=%res%&set %6=%max%
    goto :eof

::大数减法
:bignum_minus
    setlocal
    set num_a=%1
    set num_b=%2
    set /a len_a=%3, len_b=%4
    set /a max = len_a
    if %len_b% gtr %len_a% (set /a max=len_b, len_b=len_a&set num_a=%num_b%&set num_b=%num_a%)

    set /a minus = 0
    set "res="
    for /l %%n in ( 1, 1, %max% ) do (
        if %%n leq %len_b% (
            set /a dt = !num_a:~-%%n,1! - !num_b:~-%%n,1! - minus
        ) else (
            set /a dt = !num_a:~-%%n,1! - minus
        )
        if !dt! lss 0 (
            set /a t = dt + 10, minus=1
        ) else (
            set /a t = dt, minus=0
        )
        set res=!t!!res!
        if !t! equ 0 (set /a zero+=1) else (set /a zero=0)
    )
    set res=!res:~%zero%!
    endlocal &set %5=%res%&set /a %6=%max%-%zero%
    goto :eof

::字符串长度计算
:length %str% %vname%
    setlocal
    set test=%~1_%sharp%
    set test=!test:~0,%maxlen%!
    set test=%test:*_=%
    set /a len=maxlen-(%test:#=1+%1)
    endlocal &set %2=%len%
    goto :eof
rem __________________________________________________________

:error
echo 当前输入非数字,请重新输入
if "%config%"=="1" pause
exit /b

::测试板块
:test
for /l %%i in (100,-1,1) do (
call :decimal_solution %%i
echo i
)
for /l %%i in (0,1,100) do (
call :decimal_solution %%i
echo.
)
endlocal
set testconfig=
exit /b

:help
echo sqrt [[x][批量测试]] [计算精度]
echo 开平方函数,返回一数开平方值(不要输入分数)
echo 如:
echo sqrt 2.1 (输出√2.1的值)
echo sqrt Y (测试√-100[10i]到√100的值)
echo sqrt 2 10 (输出精度为10的√2的值)
echo sqrt -9 5 (输出精度为5的√-9[3i]的值)
echo sqrt /? 
echo sqrt --help (显示帮助信息)
echo sqrt --version (显示版本信息)
exit /b

:version
echo sqrt+ function v1.0 powered by 何世恒
exit /b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值