在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
发几个我的函数 2009-02-20 日更新:修正加法函数中一处bug, 计算任意位数的正整数加法@echo off call :jia 354687654165435 35486456 ok echo %ok% pause :jia 加法函数(封装)by @随风 @bbs.bathome.net ::计算任意位数的正整数加法 setlocal enabledelayedexpansion&set f=&set "t=" set var1=%~1&set var2=%~2&set /a j=0,n1=0,n2=0 for /l %%a in (0 1 9) do (set vard1=&set "vard2=" set var1=!var1:%%a= %%a !&set var2=!var2:%%a= %%a !) for %%a in (!var1!)do (set/a n1+=1&set vard1=%%a !vard1!) for %%a in (!var2!)do (set/a n2+=1&set vard2=%%a !vard2!) (if !n1! lss !n2! (set var1=%var2%&set "var2=%var1%" set vard1=%vard2%&set vard2=%vard1%))&set "var2=!var2: =!" for %%a in (!vard1!) do (if "!var2!"=="" set /a var2=0 set /a a=%%a+!var2:~-1!+j&set t=!a:~-1!!t!&set "a=0!a!" set "j=!a:~-2,1!"&set var2=!var2:~0,-1!) if !j! neq 0 set "t=!j!!t!" Endlocal&set %~3=%t%&goto :EOF 计算1000位以内的正整数减法@echo off call :jian 354687654165435 35486456 ok echo %ok% pause :jian 减法函数(封装)by @随风 @bbs.bathome.net ::计算1000位以内的正整数减法 setlocal enabledelayedexpansion&&set t=&set f=&set "lin=" for /l %%a in (1 1 10) do set "lin=0000000000!lin!" set lin=!lin!!lin!!lin!!lin!!lin!&set "lin=!lin!!lin!" set var1=!lin!%~1&set var2=!lin!%~2&set vard1=&set/a j=0 set var1=!var1:~-1000!&set "var2=!var2:~-1000!" if "!var1!" lss "!var2!" (set var1=%~2&set "var2=%~1" set "f=-") else set var1=%~1&set "var2=%~2" for /l %%a in (0 1 9) do set "var1=!var1:%%a= %%a !" for %%a in (!var1!) do set "vard1=%%a !vard1!" for %%a in (!vard1!) do (if "!var2!"=="" set/a var2=0 set /a a=%%a-j,b=!var2:~-1! if !a! lss !b! (set /a a+=10,j=1)else set /a j=0 set /a w=a-b&set t=!w!!t!&set var2=!var2:~0,-1!) for /f "tokens=* delims=0" %%a in ("!t!") do ( if "%%a"=="" (set t=0) else set "t=%%a") Endlocal&set %~3=%f%%t%&goto :EOF 计算任意位数的正整数乘法@echo off call :cen 354687654165435 35486456 ok echo %ok% pause :cen 乘法函数(封装)by @随风 @bbs.bathome.net ::计算任意位数的正整数乘法 setlocal enabledelayedexpansion if "%~1"=="0" Endlocal&set %~3=0&goto :EOF if "%~2"=="0" Endlocal&set %~3=0&goto :EOF set f=&set jia=&set ji=&set /a n1=0,n2=0 set vard1=&set "vard2="&set var1=%~1&set "var2=%~2" for /l %%a in (0 1 9) do ( set var1=!var1:%%a= %%a !&set var2=!var2:%%a= %%a !) for %%a in (!var1!)do (set /a n1+=1&set vard1=%%a !vard1!) for %%a in (!var2!)do (set /a n2+=1&set vard2=%%a !vard2!) if !n1! gtr !n2! (set vard1=%vard2%&set vard2=%vard1%) for %%a in (!vard1!) do (set "t="&set /a j=0 for %%b in (!vard2!) do (if "!jia!"=="" set /a jia=0 set /a a=%%a*%%b+j+!jia:~-1!&set "t=!a:~-1!!t!" set a=0!a!&set "j=!a:~-2,1!"&set jia=!jia:~0,-1!) set "ji=!t:~-1!!ji!" if "!j:~0,1!"=="0" (set ss=) else set "ss=!j:~0,1!" set jia=!ss!!t:~0,-1!) if not "!j:~0,1!"=="0" set "t=!j:~0,1!!t!" set "ji=!t!!ji:~1!" Endlocal&set %~3=%ji%&goto :EOF 500位内整数除法函数@echo off set suru=000012/001200 call :cu0 %suru:/= % ok echo. %ok% pause exit :cu0 500位内整数除法函数(封装)by @随风 bbs.bathome.net ::函数内有 cu1 cu2 cu3 cu4 四个标签,引用时需注意 setlocal enabledelayedexpansion&set "lin=00000" set /a zongw=1000,cs1w=0,cs2w=0,falg=0,x=0 if "!str!"=="1" Endlocal&set %~3=%ff%!num!&goto :EOF if "%~1"=="0" set sang=0&goto cu4 if "!str!"=="0" set sang=以零为除数的错误。&goto cu4 if not defined xiaosu set /a xiaosu=10 for /l %%a in (1 1 5) do set "lin=!lin!!lin!!lin!" set sang=&set ppp=&set var1=%~1&set "var2=%~2" for /f "tokens=* delims=0" %%a in ("!var1!")do set var1=%%a for /f "tokens=* delims=0" %%a in ("!var2!")do set var2=%%a for /l %%a in (0 1 9)do (set "var1=!var1:%%a= %%a !" set "var2=!var2:%%a= %%a !") for %%a in (!var1!) do set /a cs1w+=1 for %%a in (!var2!) do set /a cs2w+=1 for /l %%a in (1 1 10) do (set t=&set cs=%~2&set/a j=0 for /l %%b in (1 1 !cs2w!) do (set /a a=%%a*!cs:~-1!+j set t=!a:~-1!!t!&set a=0!a!&set "j=!a:~-2,1!" set cs=!cs:~0,-1!&set cs%%a=&set "bj%%a=") if !j! neq 0 (set cs%%a=!lin!!j!!t!&set "bj%%a=!j!!t!" ) else set cs%%a=!lin!!t!&set "bj%%a=!t!" set "cs%%a=!cs%%a:~-%zongw%!") set var2=!lin!!var2: =!&set "var2=!var2:~-%zongw%!" set /a cswc=cs1w-cs2w&set "var1=!var1: =!" if !cswc! lss 0 (set cswc=!cswc:-=!&set/a flag=1 for /l %%a in (1 1 !cswc!)do ( if %%a leq 11 set sang=0!sang!&set /a x=cswc-1 set "var1=!var1!0") set "sang=!sang:~0,1!.!sang:~1!") set ppp=!var1:~0,%cs2w%!&set "var1=!var1:~%cs2w%!" if !flag! equ 1 (set /a bul=1) else set /a bul=0 goto cu2 :cu1 if not defined var1 (set "var1=0" if not defined ppp goto cu4 if !flag! equ 0 (set sang=!sang!.&set /a flag=1)) set/a bul=1&set ppp=!ppp!!var1:~0,1!&set "var1=!var1:~1!" :cu2 if !x! geq %xiaosu% goto cu4 set pvar1=!lin!!ppp!&set "pvar1=!pvar1:~-%zongw%!" if "!pvar1!" lss "!var2!" ( if !bul! equ 1 (set sang=!sang!0&set /a bul=0 if !flag! equ 1 set /a x+=1) if "!ppp:~0,1!"=="0" set "ppp=" goto cu1) if !flag! equ 1 set /a x+=1 set /a bul=0 ::计算商 for /l %%a in (1 1 10) do ( if "!cs%%a!" equ "!pvar1!" ( set "sang=!sang!%%a"&set "yu=!bj%%a!"&goto cu3) if "!cs%%a!" gtr "!pvar1!" (set /a s=%%a-1 set "sang=!sang!!s!"&set yu=!t!&goto cu3) set "t=!bj%%a!") :cu3 计算差 set cjs=!ppp!&set cj1=&set m=&set/a jjj=0 for /l %%a in (0 1 9) do set "cjs=!cjs:%%a= %%a !" for %%a in (!cjs!) do set "cj1=%%a !cj1!" for %%a in (!cj1!) do (if "!yu!"=="" set/a yu=0 set /a a=%%a-jjj,b=!yu:~-1! if !a! lss !b! (set /a a+=10,jjj=1)else set/a jjj=0 set /a w=a-b&set m=!w!!m!&set yu=!yu:~0,-1!) for /f "tokens=* delims=0" %%m in ("!m!") do ( if "%%m"=="" (set m=0) else set "m=%%m") if !m! equ 0 ( if "!var1:0=!"=="" set sang=!sang!!var1!&goto cu4 set ppp=&goto cu1) else set "ppp=!m!" goto cu2 :cu4 if "!sang:~0,1!"=="." set "sang=0!sang!" Endlocal&set %~3=%ff%%sang%&goto :EOF 计算时间差@echo off set t=%time% call :time0 "%t%" "%time%" ok echo %ok% pause :time0 计算时间差 (封装) @echo off&setlocal&set /a n=0&rem code 随风 @bbs.bathome.net for /f "tokens=1-8 delims=.: " %%a in ("%~1:%~2") do ( set /a n+=10%%a%%100*360000+10%%b%%100*6000+10%%c%%100*100+10%%d%%100 set /a n-=10%%e%%100*360000+10%%f%%100*6000+10%%g%%100*100+10%%h%%100) set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100 set "ok=%s% 小时 %f% 分钟 %m% 秒 %n% 毫秒" endlocal&set %~3=%ok:-=%&goto :EOF 可以处理0开头的数、重复数及200位以内的超大整数(封装)@echo off :start setlocal enabledelayedexpansion&cls for /l %%a in (1 1 10) do set str=!str! !random! echo.&echo %str% call :sort "%str%" ok echo %ok% pause>nul endlocal&goto start :sort 可以处理0开头的数、重复数及200位以内的超大整数(封装) @echo off&setlocal enabledelayedexpansion for /f "delims==" %%a in ('set p. 2^>nul')do set "%%a=" for /l %%a in (1 1 20) do set "p.lin=0000000000!p.lin!" for %%a in (%~1) do (set s=!p.lin!%%a&set "s=!s:~-200!" if defined p...!s! (set p..%%a=!p..%%a! %%a set p...!s!=!p..%%a! %%a) else (set p...!s!=%%a)) for /f "tokens=2 delims==" %%a in ('set p...') do ( for %%i in (%%a) do set "p.ok=!p.ok! %%i") endlocal&set %~2=%p.ok:~1%&goto :EOF 计算指定天数 前/后 的日期及星期@echo off :start setlocal&cls set /p tian= 请指定要追溯的天数:&echo\ call :Date2Day "%date%" %tian% ok echo %ok% endlocal&pause>nul&goto start :Date2Day 计算指定天数 前/后 的日期及星期 (封装) @echo off&setlocal ENABLEEXTENSIONS for /f "tokens=1-3 delims=/-:\, " %%a in ('echo/%~1') do ( set /a yy=%%a,mm=100%%b%%100,dd=100%%c%%100) set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2 set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633 set /a i=j-%~2,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5 set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10 (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%) for %%a in (一/1 二/2 三/3 四/4 五/5 六/6 日/0) do ( for /f "tokens=1,2 delims=/" %%i in ("%%a")do set %%i=%%j&set ".%%j=%%i") if %~2 gtr 0 (set /a d=^(7-%~2%%7+%date:~-1%^)%%7&set x=前) else ( set /a d=^(~%~2+1+%date:~-1%^)%%7&set x=后) call set "d=星期%%.%d%%%" endlocal&set %~3=%yy%-%mm%-%dd% %d%&goto :EOF 进制转换(函数)1、任意进制互转@echo off&color 1f&set /a q=10,h=2 title 任意进制互转 默认:%q%进制转%h%进制 :start setlocal&echo.&echo. 被转换的数 被转换数的进制 需转换成的进制 echo.&echo. 如:95 16 10 表示将 16进制数95 转换 为10进制数 echo.&echo. 参数可用分割符为 空格 / + - \ 如:95/16/10 或95+16+10 echo.&echo. 默认将 10进制 转换为 2进制(即上例中如果不输入 16 10 则默认为 10 2) echo.&set /p num= 请输入需转换的数 &cls if not defined num endlocal&goto start for /f "tokens=1-3 delims=/+-\ " %%a in ("%num%") do ( set "num=%%a" if not "%%b"=="" if not "%%c"=="" set /a q=%%b,h=%%c ) call :nxn "%num%" ok %q% %h% for /l %%a in (1 1 12)do ( if %%a equ 4 ( echo. %q%进制转%h%进制 结果 echo.&echo. %num% = %ok% )else echo\ ) set /a w=%random%%%2 if %w% equ 1 (set f=1f) else (set f=df) color %f%&endlocal&goto start ::函数使用方法:call :nxn "98" ok 10 2 ::call :nxn "被转换的数" 保存结果的变量名 被转换数的进制 需转换成的进制 ::默认将 10进制 转换为 2进制(即上例中不输入 10 2) :nXn 任意进制互转 @随风 @bbs.bathome.net(封装) @echo off&setlocal enabledelayedexpansion if "%~4"=="" (set /a q=10,h=2)else (set /a q=%~3,h=%~4) set "str=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" for /l %%a in (0 1 35)do set .%%a=!str:~%%a,1!&set ".!str:~%%a,1!=%%a" set sun=&set jie=&set "var=%~1"&set /a nnn=1,num=0 for /l %%a in (0 1 100) do ( if not "!var:~%%a,1!"=="" (call set sun=%%.!var:~%%a,1!%% !sun!)) for %%a in (!sun!) do set /a num=%%a*nnn+num,nnn*=q :10进制转n进制 set /a yu=num%%h,num/=h set "jie=!.%yu%!!jie!"&if !num! gtr 0 goto 10进制转n进制 endlocal&set %~2=%jie%&goto :EOF 二进制转十六进制(封装)(通过4位一段转换,可处理超大数)@echo off&color 1f&title 二进制 转 十六进制 call :2x16 "11111111 11111111 11111111 11111110" ok echo %ok% pause ::以4位一段,分别转换为十六进制,不必考虑超大数问题 :2x16 二进制 转 十六进制 @随风 @bbs.bathome.net (封装) @echo off&setlocal enabledelayedexpansion set "str=0123456789ABCDEF" set k=&set kk=&set xx=&set "x=000%~1" set x=!x:0= 0!&set x=!x:1= 1!&set /a jj=0,ss=1 for %%a in (!x!) do set "xx=%%a !xx!" for %%a in (!xx!) do (set /a jj+=%%a*ss,ss=ss*2 if !ss! equ 16 set k=!jj! !k!&set /a jj=0,ss=1) for %%a in (!k!) do set kk=!kk!!str:~%%a,1! endlocal&set %~2=%kk%&goto :EOF 十六进制转二进制(封装)(通过4位一段转换,可处理超大数)@echo off&color 1f&title 十六进制 转 二进制 set mmm=bfa35e7d8 call :16x2 %mmm% ok echo.&echo 十六进制 %mmm% echo.&echo 二进制 %ok% echo.&pause ::以4位一段,分别转换为二进制,不必考虑超大数问题 :16x2 十六进制 转 二进制 @随风 @bbs.bathome.net (封装) @echo off&setlocal enabledelayedexpansion set str=0123456789abcdef&set ok=&set "num=%~1" for /l %%a in (0 1 15) do (set ".!str:~%%a,1!=%%a" call set "num=%%num:!str:~%%a,1!= !str:~%%a,1!%%") for %%i in (!num!) do (set /a x=0,n=!.%%i! for %%a in (8 4 2 1) do (set /a x+=%%a if !n! geq !x! (set ok=!ok!1) else ( set ok=!ok!0&set /a x-=%%a))) endlocal&set %~2=%ok%&goto :EOF 二进制转十进制(封装)(只能处理cmd范围内的数,小巧方便代码中调用)@echo off call :2x10 01100100 ok echo %ok% pause :2x10 2进制转 10进制 @随风 @bbs.bathome.net (封装) @echo off&setlocal enabledelayedexpansion set /a nnn=1,num=0&set nn=&set "n=%~1" set "n=!n:0= 0!"&set "n=!n:1= 1!" for %%a in (!n!) do set "nn=%%a !nn!" for %%a in (!nn!) do set /a num+=%%a*nnn,nnn*=2 endlocal&set %~2=%num%&goto :EOF 获取10进制数(含负数)的 原码、反码、补码(未封装)@echo off&color 1f&title 获取10进制数的 原码、反码、补码 ::获取10进制数的 原码、反码、补码 ::计算机中 负数用补码表达 ::补码:反码+1 ::反码:将二进制数(原码)按位取反 (即:1变0、0变1) ::原码:一个整数,按照绝对值大小转换成的二进制数,称为原码。 ::个人理解为在计算机中表达负数的二进制数叫补码 ::所以以下代码中,若输入的是负数则结果中补码代表该数 @echo off&rem @随风 @bbs.bathome.net @2009-01-15 :start setlocal enabledelayedexpansion for /l %%a in (1 1 15) do set "lin=00000!lin!" echo.&echo.&set /p y10= 请输入一个10进制数可以为负数 &cls if !y10! lss 0 (set /a n="~y10"+1,c=1) else set /a n=y10,c=1 :10x2 将10进制转成2进制 原码 set /a y=n%%2,n/=2&set "j=!y!!j!"&if !n! gtr 0 goto 10x2 set j=!lin!!j!&set "j=!j:~-32!" set ss=!j:0= 0!&set "ss=!ss:1= 1!" :: 获取反码 for %%a in (%ss%) do set /a n="^!%%a"&set "f=!f!!n!" set f=!lin!!f!&set f=!f:~-32!&set "ss=!f!" :buma 获取补码 set /a m=!ss:~-1!+c if !m! gtr 1 (set c=1&set b=0!b!)else set c=0&set "b=!m!!b!" set "ss=!ss:~0,-1!" if defined ss goto buma echo. echo 10进制数 %y10%&echo. echo 原码 %j:~-32,8% %j:~-24,8% %j:~-16,8% %j:~-8% echo 反码 %f:~-32,8% %f:~-24,8% %f:~-16,8% %f:~-8% echo 补码 %b:~-32,8% %b:~-24,8% %b:~-16,8% %b:~-8% endlocal&goto start 转换 2、8、10、16、进制数并显示2进制 原码、反码、补码(未封装)@echo off ::转换 2、8、10、16、进制数并显示2进制 原码、反码、补码 ::计算机中 负数用补码表达 ::补码:反码+1 ::反码:将二进制数(原码)按位取反 (即:1变0、0变1) ::原码:一个整数,按照绝对值大小转换成的二进制数,称为原码。 ::个人理解为在计算机中表达负数的二进制数叫补码 ::完全不懂计算机,只理解为计算机中负数用补码表达 ::代码中二进制数用32位显示,超出32位的会出错 ::只能处理cmd范围内的数 @echo off&title 转换 2、8、10、16、进制数并显示2进制 原码、反码、补码 ::@随风 @bbs.bathome.net @2009-1-12 set "str=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" set "color1=1f&set color2=df"&color 1f for /l %%a in (0 1 35) do ( call set ".%%a=%%str:~%%a,1%%" call set ".%%str:~%%a,1%%=%%a" ) :start setlocal enabledelayedexpansion&color %color1% echo. echo. ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ echo. ┃ ┃ echo. ┃输入格式:需转换的数+该数的进制(可用分割符 / \ + - 空格)┃ echo. ┃ ┃ echo. ┃默认输入的为10进制数、也可以是2、8、16进制、但必须指明进制┃ echo. ┃ ┃ echo. ┃如:需转换16进制 2afd 则输入 2afd+16 10进制数则不需要指明┃ echo. ┃ ┃ echo. ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ echo.&set /p num= 请输入需转换的数 &cls if not defined num endlocal&goto start for /f "tokens=1,2 delims=/\+- " %%a in ("%num%") do ( set "n0=%%a" if "%%b"=="" (set jz=10) else set /a jz=%%b ) echo.&echo 被转换的数为: !jz!进制数 !n0!&echo. if !jz! equ 16 (set /a n10=0x!n0!) else ( if !jz! equ 10 (set n10=!n0!) else ( if !jz! equ 8 (set /a n10=0!n0!) else ( set /a nnn=1,n10=0&call :2x10 !n0! ))) for %%a in (16 10 8 2) do call :10进制转n进制 !n10! %%a echo.&call :yuan&echo. endlocal&set color1=%color2%&set "color2=%color1%" goto start :10进制转n进制 set /a yu=n10%%%~2,n10/=%~2 set "jie=!.%yu%!!jie!"&if !n10! gtr 0 goto 10进制转n进制 set "xs= %~2" echo !xs:~-5! 进制 !jie! set d2=!jie!&set jie=&set "n10=%~1" goto :EOF :2x10 2进制转10进制 set /a n10+=!n0:~-1!*nnn,nnn*=2 set "n0=!n0:~0,-1!" if defined n0 goto 2x10 goto :EOF :yuan for /l %%a in (1 1 15) do set lin=00000!lin! set j=!lin!!d2!&set "j=!j:~-32!" set ss=!j:0= 0!&set "ss=!ss:1= 1!" :: 获取反码 for %%a in (%ss%) do set /a n="^!%%a"&set "f=!f!!n!" set f=!lin!!f!&set f=!f:~-32!&set "ss=!f!" set /a c=1 :buma 获取补码 set /a m=!ss:~-1!+c if !m! gtr 1 (set c=1&set b=0!b!)else set c=0&set "b=!m!!b!" set "ss=!ss:~0,-1!" if defined ss goto buma echo 原码 %j:~-32,8% %j:~-24,8% %j:~-16,8% %j:~-8% echo 反码 %f:~-32,8% %f:~-24,8% %f:~-16,8% %f:~-8% echo 补码 %b:~-32,8% %b:~-24,8% %b:~-16,8% %b:~-8% goto :EOF 到此这篇关于批处理bat函数:大数字加减乘除、时期时间计算、数字排序、进制转换等的文章就介绍到这了,更多相关bat数值计算内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论