批处理开平方.docx
《批处理开平方.docx》由会员分享,可在线阅读,更多相关《批处理开平方.docx(17页珍藏版)》请在冰豆网上搜索。
批处理开平方
批处理如何开平方
批处理开平方,网上已有很多方法。
但都存在一个问题:
精确位数低,还有计算错误。
甚至有的计算还很慢。
本批处理程序算法能在较短时间内计算到较大的位数,实现了批处理开方的更高程度。
以后会有更新:
1.对算法进行优化:
四百位以内平均十秒一位。
而非像现在需要十分钟。
2.扩大被开方数范围:
支持小数,且被开方数数字个数(含小数点)从10扩大到2147483640。
(注:
未更新前仅支持1627396281及以内,刚好10位数)
敬请期待!
另外,不知道如何将以下代码做成bat程序的请XX一下:
如何制作bat
注意,部分计算器对最后一位进行了四舍五入,本程序可能会与计算器的最后一位相差1。
这不是本程序的错误。
不相信本程序计算能力的的计算根号2,XX搜索:
根号21000位即可查到,可与本程序计算结果对比。
代码中大号红色加粗加下划线字仅为显示进度,为缩短计算时间可删除。
可节省不少时间。
代码如下:
(注意别复制到结尾,代码部分包含两个程序)
@echooff
title计算整数平方根的值
colorf0
PUSHD%~dp0
cd/d%~dp0
setlocalenabledelayedexpansion
cls
echo------------------
echo计算整数平方根的值
echo------------------
:
start
echo.&echo请输入被开方数(仅限整数),然后按回车键:
echo(被开方数最高为1627396281,超出范围,会计算出错)
setnumber=
set/p"number="
gotonext
:
next
cls
if"%number%"==""gotostart
set/atemp=%number%+0
ifnot"%temp%"=="%number%"gotoerror_a
ifnot%number%geq0gotoerror_b
if%number%geq1627396282gotoerror_c
gotothen
:
then
cls
echo.&echo请输入精确位数,然后按回车键:
echo(指精确到小数点后第几位)
echo(该程序不会对最后一位进行四舍五入)
echo(精确位数最高为238609294,超出范围,会计算出错)
echo(对于结果为整数的,程序会舍去小数部分)
setbit=
set/p"bit="
gotoother
:
other
cls
if"%bit%"==""gotothen
set/atemp=%bit%+0
ifnot"%bit%"=="%temp%"gotoerror_d
ifnot%bit%geq0gotoerror_d
if%bit%geq238609295gotoerror_e
setstart=yes
gotomain
:
error_a
cls
colorfc
echo错误:
输入的被开方数不是整数,请重新输入!
echo其绝对值不得超过批处理支持的最大值(2147483647)!
echo请不要在整数前加上正号(+)或多余的零!
echo.&echo按任意键继续:
&pause>nul
colorf0
cls
gotostart
:
error_b
cls
colorfc
echo错误:
平方根中被开方数不能小于0,请重新输入!
echo.&echo按任意键继续:
&pause>nul
colorf0
cls
gotostart
:
error_c
cls
colorfc
echo错误:
被开方数不能大于1627396281,请重新输入!
echo.&echo按任意键继续:
&pause>nul
colorf0
cls
gotostart
:
error_d
cls
colorfc
echo错误:
输入的精确位数不是正整数,请重新输入!
echo其绝对值不得超过批处理支持的最大值(2147483647)!
echo请不要在整数前加上正号(+)或多余的零!
echo.&echo按任意键继续:
&pause>nul
colorf0
cls
gotothen
:
error_e
cls
colorfc
echo错误:
精确位数不能大于238609294,请重新输入!
echo.&echo按任意键继续:
&pause>nul
colorf0
cls
gotothen
:
main
cls
echo.&echo已经开始计算。
echo每计算一位,都会对结果更新!
echo越往后计算,结果显示越慢!
请耐心等待本程序计算完成。
echo.&echo被开方数:
%number%
ifnot"%start%"=="yes"(
set/anum+=1
set/atga=!
num!
-%allbit%-1
set/atemp=!
tga!
%%4
ifnot"!
tga!
"=="1"(
if"!
temp!
"=="1"(
setnun=%nun%^%main:
~-1%
)else(
setnun=%nun%%main:
~-1%
)
)else(
setnun=%nun%%main:
~-1%
)
set/atemp=%tga%+1
if"!
temp!
"=="%bit%"gotoend
echo.&echo计算结果(未对其结果进行四舍五入):
echo.&echo%nun%
gotojs
)else(
echo.&echo请稍候……
gototemp
)
:
temp
setu=0
settga=0
call:
stringlenth"%number%"num
set/aallbit=(%num%+1)/2
setnum=%allbit%
settgb=%num%
set/anum+=1
for/l%%iin(01%num%)dosetjs[%%i]=0
if"%allbit%"=="1"(
setkfmain=js[1]
call:
kfmain
)else(
for/l%%iin(%tgb%-11)do(
setu=0
setkfmain=js[%%i]
call:
kfmain
setjs[%%i]=!
temp!
)
settemp=
for/l%%iin(%tgb%-11)do(
settemp=!
temp!
!
js[%%i]!
)
)
setall=%temp%
set/atemp=%all%*%all%
if"%temp%"=="%number%"(
setbit=0
setnun=%all%
gotoend
)
if"%bit%"=="0"(
setnun=%all%
gotoend
)
set"main=%all%."
set"nun=%all%."
setstart=no
gotojs
:
js
set"nmn=%main:
.=%"
set/atgb=%num%-1
setkfmain=js[0]
if"%tgb%"=="1"(
setjs[1]=%nmn%
)else(
for/l%%iin(%tgb%-11)do(
settemp=!
nmn:
~-%%i!
setjs[%%i]=!
temp:
~0,1!
)
)
setjs[%num%]=0
setu=0
call:
kfmain
setmain=%main%%temp%
gotomain
:
end
cls
echo.&echo计算完毕!
echo.&echo被开方数:
%number%
echo.&echo计算结果(未对其结果进行四舍五入):
echo!
nun!
echo.&echo按任意键继续:
&pause>nul
cls
echo请选择接下来的操作。
echo---------------------
echo0:
退出
echo1:
返回重新计算
echo2:
将结果保存至桌面。
echo---------------------
set"choice="
set/p"choice=请输入操作序号,然后按回车键:
"
if"%choice%"=="0"exit/b
if"%choice%"=="1"(
cls
gotostart
)
if"%choice%"=="2"gotoexit_one
gotoend
:
exit_one
if"%bit%"=="0"(
settemp=(保留整数)
)else(
settemp=(精确到小数点后第%bit%位)
)
cd/d"%UserProfile%\desktop"
ifexist"%number%的平方根的结果%temp%.txt"(
takeown"%number%的平方根的结果%temp%.txt"
del/f/s/q"%number%的平方根的结果%temp%.txt"
)
echo%number%的平方根的结果:
>>%number%的平方根的结果%temp%.txt
echo%temp%:
>>%number%的平方根的结果%temp%.txt
echo(未对其结果进行四舍五入):
>>%number%的平方根的结果%temp%.txt
echo%nun%>>%number%的平方根的结果%temp%.txt
gotoexit
:
exit
cls
echo.&echo保存完毕。
echo请选择接下来的操作。
echo--------------------------
echo0:
退出
echo1:
返回重新计算
echo2:
打开保存到的文件并退出
echo--------------------------
set"choice="
set/p"choice=请输入操作序号,然后按回车键:
"
if"%choice%"=="0"exit/b
if"%choice%"=="1"(
cls
gotostart
)
if"%choice%"=="2"(
start/d"%windir%"notepad.exe"%UserProfile%\desktop\%number%的平方根的结果%temp%.txt"
exit/b
)
gotoexit
:
kfmain
set/au+=1
if"%er%"=="2m"(
set%kfmain%=1
gotojsmain
)
if"%er%"=="5m"(
set%kfmain%=2
gotojsmain
)
if"%er%"=="4m"(
set%kfmain%=3
gotojsmain
)
if"%er%"=="2l"(
set%kfmain%=4
gotojsmain
)
if"%u%"=="1"(
set%kfmain%=5
gotojsmain
)
if"%er%"=="7m"(
set%kfmain%=6
gotojsmain
)
if"%er%"=="5l"(
set%kfmain%=7
gotojsmain
)
if"%er%"=="9m"(
set%kfmain%=8
gotojsmain
)
if"%er%"=="7l"(
set%kfmain%=9
gotojsmain
)
if"%er%"=="1m"settemp=0
if"%er%"=="1l"settemp=1
if"%er%"=="3m"settemp=2
if"%er%"=="3l"settemp=3
if"%er%"=="4l"settemp=4
if"%er%"=="6m"settemp=5
if"%er%"=="6l"settemp=6
if"%er%"=="8m"settemp=7
if"%er%"=="8l"settemp=8
if"%er%"=="9l"settemp=9
goto:
eof
:
jsmain
settgc=0
set/atgd=%num%*2-1
for/l%%iin(%tgd%-10)dosetdg[%%i]=0
for/l%%iin(01%tgb%)do(
for/l%%rin(01%num%)do(
set/ai=%%i+%%r
set/atemp=!
js[%%i]!
*!
js[%%r]!
+!
tgc!
set/adg[!
i!
]+=!
temp!
%%10
set/atgc=!
temp!
/10
)
settgc=0
if%tga%geq50(
if%tga%geq100(
set/p=set/p=总进度^:
%u%^(最少3^,最多4^)^,副进度^:
%%i^(共%tgb%^)^^^^)else(
set/p=set/p=总进度^:
%u%^(最少3^,最多4^)^^^^)
)
)
set/atgd-=1
for/l%%iin(01%tgd%)do(
set/atemp=%%i+1
set/adg[!
temp!
]+=!
dg[%%i]!
/10
)
set/atgd+=1
set/atge=1+!
tgd!
-(%allbit%*2)
settgf=
for/l%%iin(!
tgd!
-1!
tge!
)do(
set/atemp=!
dg[%%i]!
%%10
if"%%i"=="!
tgd!
"(
ifnot"!
temp!
"=="0"(
settgf=!
temp!
)
)else(
settgf=!
tgf!
!
temp!
)
)
ifnot"%start%"=="yes"(
if!
tgf!
geq%number%(
seter=!
%kfmain%!
m
)else(
seter=!
%kfmain%!
l
)
)else(
if!
tgf!
gtr%number%(
seter=!
%kfmain%!
m
)else(
seter=!
%kfmain%!
l
)
)
gotokfmain
:
StringLenth
settheString=%~1
ifnotdefinedtheStringgoto:
eof
settemp=0
gotoStringLenth_continue
:
StringLenth_continue
set/atemp+=1
setthestring=%thestring:
~0,-1%
ifdefinedthestringgotoStringLenth_continue
ifnot"%2"==""set%2=!
temp!
goto:
eof
如果只计算根号2的值,可用下面程序(速度更快)
@echooff
title计算2的平方根的值
colorf0
pushd%~dp0
cd/d%~dp0
setlocalenabledelayedexpansion
cls
echo------------------
echo计算2的平方根的值
echo------------------
:
start
colorf0
echo.&echo请输入精确位数,然后按回车键:
echo(精确位数指精确到小数点后第几位)
echo(该程序不会对最后一位进行四舍五入)
echo(精确位数最高为238609294,超出范围,会计算出错)
set"bit="
set/p"bit="
set/abito=%bit%+0
ifnot"%bito%"=="%bit%"(
cls
colorfc
echo.&echo输入的精确位数不是整数,请重新输入!
echo输入的精确位数不能超过“2147483647”!
echo请不要在精确位数前输入多余的零!
echo.&echo按任意键继续:
&pause>nul
cls
gotostart
)
ifnot%bit%geq0(
cls
colorfc
echo错误:
输入的精确位数不是正整数,请重新输入!
echo.&echo按任意键继续:
&pause>nul
cls
gotostart
)
ifnot%bit%leq238609294(
cls
colorfc
echo错误:
输入的精确位数不能大于238609294,请重新输入!
echo.&echo按任意键继续:
&pause>nul
cls
gotostart
)
gotostart_ct
:
start_ct
:
:
可在下面保存进度
:
:
"例:
已算出结果为1.41421356"
:
:
则可改为"setnun=1.41421356"。
注意小数点后四位一个英文状态下的空格,
:
:
但末尾不得有空格。
(例:
正确:
"1.4142",错误:
"1.4142")
:
:
清除进度,改为"setnun=1."。
"1."不能丢!
!
!
setnun=1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186
set"num=%nun:
=%"
call:
stringlenth"%num%"number
set/anumber=%number%-1
setc=1
gotomain
:
main
cls
set/anumber=%number%+1
:
:
显示优化,四位一空格。
set/atga=%number%-2
if"%c%"=="0"(
seta=%num:
~-1%
set/ab=!
tga!
%%4
ifnot"%tga%"=="1"(
if"%b%"=="0"seta=^!
a!
)
setnun=%nun%!
a!
)else(
setc=0
set/ab=!
tga!
%%4
)
echo.&echo已经开始计算。
echo在没有看到结尾信息时,请勿关闭本窗口!
echo每计算一位,都会对结果更新!
该程序不会对最后一位进行四舍五入。
echo越往后计算,结果显示越慢!
请耐心等待本程序计算完成。
echo.&echo计算结果:
echo%nun%
echo.
set/atga=%number%-2
if%tga%geq%bit%gotoexitone
if"%tga%"=="0"gotojsks
if"%tga%"=="1"(
seta[1]=%num:
~-1%
gotojsks
)
for/l%%iin(%tga%,-1,1)do(
setaaqq=!
num:
~-%%i!
seta[%%i]=!
aaqq:
~0,1!
)
gotojsks
:
jsks
set/atgz=%number%-1
seta[%number%]=0
:
:
(下)原为seta[%tgz%]=!
num:
~0,1!
,其结果为1
seta[%tgz%]=1
setmainmain=00
setz=0
gotokfmain
:
kfmain
set/az+=1
if"%mainmain%"=="2m"(
seta[0]=1
call:
jsmain
gotokfmain
)
if"%mainmain%"=="5m"(
seta[0]=2
call:
jsmain
gotokfmain
)
if"%mainmain%"=="4m"(
seta[0]=3
call:
jsmain
gotokfmain
)
if"%mainmain%"=="2l"(
seta[0]=4
call:
jsmain
gotokfmain
)
if"%mainmain%"=="00"(
seta[0]=5
call:
jsmain
gotokfmain
)
if"%mainmain%"=="7m"(
seta[0]=6
call:
jsmain
gotokfmain
)
if"%mainmain%"=="5l"(
seta[0]=7
call:
jsmain
gotokfmain
)
if"%mainmain%"=="9m"(
seta[0]=8
call:
jsmain
gotokfmain
)
if"%mainmain%"=="7l"(
seta[0]=9
call:
jsmain
gotokfmain
)
if"%mainmain%"=="1m"settempab=0
if"%mainmain%"=="1l"settempab=1
if"%mainmain%"=="3m"settempab=2
if"%mainmain%"=="3l"settempab=3
if"%mainmain%"=="4l"settempab=4
if"%mainmain%"=="6m"settempab=5
if"%mainmain%"=="6l"sette