[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[数值计算] 【已解决】批处理for里面如何大数相加?

本帖最后由 5haiji 于 2020-7-17 10:41 编辑

求大哥帮忙改改 主要是for里面大数相加 搞了好几天拜托拜托
  1. @echo off
  2. @title 本机信息
  3. Color 0D
  4. echo ---------------------------------
  5. for /f "tokens=1 skip=1" %%i in ('wmic cpu get NumberOfLogicalProcessors') do if %%i gtr 1 ( set ip=%%i)
  6. echo 你的服务器CPU线程数:%ip%
  7. set nc=0
  8. for /f "tokens=1 skip=1" %%c in ('wmic memorychip get capacity') do if %%c gtr 1 (
  9. set /a nc=%%c+%nc%
  10. call :jia %nc% %ok% ok
  11. echo %ok%
  12. )
  13. set suru=%nc%/1073741824
  14. call :cu0  %suru:/= % ok
  15. echo 你的服务器内存大小:%ok% G
  16. echo ---------------------------------
  17. pause
  18. :cu0 by @随风 @bbs.bathome.net
  19. ::by @随风 @bbs.bathome.net
  20. setlocal enabledelayedexpansion&set "lin=00000"
  21. set /a zongw=1000,cs1w=0,cs2w=0,falg=0,x=0
  22. if "!str!"=="1" Endlocal&set %~3=%ff%!num!&goto :EOF
  23. if "%~1"=="0" set sang=0&goto cu4
  24. if "!str!"=="0" set sang=以零为除数的错误。&goto cu4
  25. if not defined xiaosu set /a xiaosu=10
  26. for /l %%a in (1 1 5) do set "lin=!lin!!lin!!lin!"
  27. set sang=&set ppp=&set var1=%~1&set "var2=%~2"
  28. for /f "tokens=* delims=0" %%a in ("!var1!")do set var1=%%a
  29. for /f "tokens=* delims=0" %%a in ("!var2!")do set var2=%%a
  30. for /l %%a in (0 1 9)do (set "var1=!var1:%%a= %%a !"
  31. set "var2=!var2:%%a= %%a !")
  32. for %%a in (!var1!) do set /a cs1w+=1
  33. for %%a in (!var2!) do set /a cs2w+=1
  34. for /l %%a in (1 1 10) do (set t=&set cs=%~2&set/a j=0
  35. for /l %%b in (1 1 !cs2w!) do (set /a a=%%a*!cs:~-1!+j
  36. set t=!a:~-1!!t!&set a=0!a!&set "j=!a:~-2,1!"
  37. set cs=!cs:~0,-1!&set cs%%a=&set "bj%%a=")
  38. if !j! neq 0 (set cs%%a=!lin!!j!!t!&set "bj%%a=!j!!t!"
  39. ) else set cs%%a=!lin!!t!&set "bj%%a=!t!"
  40. set "cs%%a=!cs%%a:~-%zongw%!")
  41. set var2=!lin!!var2: =!&set "var2=!var2:~-%zongw%!"
  42. set /a cswc=cs1w-cs2w&set "var1=!var1: =!"
  43. if !cswc! lss 0 (set cswc=!cswc:-=!&set/a flag=1
  44. for /l %%a in (1 1 !cswc!)do (
  45. if %%a leq 11 set sang=0!sang!&set /a x=cswc-1
  46. set "var1=!var1!0")
  47. set "sang=!sang:~0,1!.!sang:~1!")
  48. set ppp=!var1:~0,%cs2w%!&set "var1=!var1:~%cs2w%!"
  49. if !flag! equ 1 (set /a bul=1) else set /a bul=0
  50. goto cu2
  51. :cu1
  52. if not defined var1 (set "var1=0"
  53. if not defined ppp goto cu4
  54. if !flag! equ 0 (set sang=!sang!.&set /a flag=1))
  55. set/a bul=1&set ppp=!ppp!!var1:~0,1!&set "var1=!var1:~1!"
  56. :cu2
  57. if !x! geq %xiaosu% goto cu4
  58. set pvar1=!lin!!ppp!&set "pvar1=!pvar1:~-%zongw%!"
  59. if "!pvar1!" lss "!var2!" (
  60. if !bul! equ 1 (set sang=!sang!0&set /a bul=0
  61. if !flag! equ 1 set /a x+=1)
  62. if "!ppp:~0,1!"=="0" set "ppp="
  63. goto cu1)
  64. if !flag! equ 1 set /a x+=1
  65. set /a bul=0
  66. ::计算商
  67. for /l %%a in (1 1 10) do (
  68. if "!cs%%a!" equ "!pvar1!" (
  69. set "sang=!sang!%%a"&set "yu=!bj%%a!"&goto cu3)
  70. if "!cs%%a!" gtr "!pvar1!" (set /a s=%%a-1
  71. set "sang=!sang!!s!"&set yu=!t!&goto cu3)
  72. set "t=!bj%%a!")
  73. :cu3 计算差
  74. set cjs=!ppp!&set cj1=&set m=&set/a jjj=0
  75. for /l %%a in (0 1 9) do set "cjs=!cjs:%%a= %%a !"
  76. for %%a in (!cjs!) do set "cj1=%%a !cj1!"
  77. for %%a in (!cj1!) do (if "!yu!"=="" set/a yu=0
  78. set /a a=%%a-jjj,b=!yu:~-1!
  79. if !a! lss !b! (set /a a+=10,jjj=1)else set/a jjj=0
  80. set /a w=a-b&set m=!w!!m!&set yu=!yu:~0,-1!)
  81. for /f "tokens=* delims=0" %%m in ("!m!") do (
  82. if "%%m"=="" (set m=0) else set "m=%%m")
  83. if !m! equ 0 (
  84. if "!var1:0=!"=="" set sang=!sang!!var1!&goto cu4
  85. set ppp=&goto cu1) else set "ppp=!m!"
  86. goto cu2
  87. :cu4
  88. if "!sang:~0,1!"=="." set "sang=0!sang!"
  89. Endlocal&set %~3=%ff%%sang%&goto :EOF
  90. :jia 加法函数(封装)by @随风 @bbs.bathome.net
  91. ::计算任意位数的正整数加法
  92. setlocal enabledelayedexpansion&set f=&set "t="
  93. set var1=%~1&set var2=%~2&set /a j=0,n1=0,n2=0
  94. for /l %%a in (0 1 9) do (set vard1=&set "vard2="
  95. set var1=!var1:%%a= %%a !&set var2=!var2:%%a= %%a !)
  96. for %%a in (!var1!)do (set/a n1+=1&set vard1=%%a !vard1!)
  97. for %%a in (!var2!)do (set/a n2+=1&set vard2=%%a !vard2!)
  98. (if !n1! lss !n2! (set var1=%var2%&set "var2=%var1%"
  99. set vard1=%vard2%&set vard2=%vard1%))&set "var2=!var2: =!"
  100. for %%a in (!vard1!) do (if "!var2!"=="" set /a var2=0
  101. set /a a=%%a+!var2:~-1!+j&set t=!a:~-1!!t!&set "a=0!a!"
  102. set "j=!a:~-2,1!"&set var2=!var2:~0,-1!)
  103. if !j! neq 0 set "t=!j!!t!"
  104. Endlocal&set %~3=%t%&goto :EOF
复制代码

for /f "tokens=1 skip=1" %%c in ('wmic memorychip get capacity') do if %%c gtr 1 (
set /a nc=%%c+%nc%
call :jia %nc% %ok% ok
echo %ok%
)
这里挂了········

TOP

大数字就别折腾bat了,用其它脚本,给你写个例子
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=1 skip=1" %%c in ('wmic memorychip get capacity') do if %%c gtr 1 (
  4. call :cacl %%c + 100 result1
  5. echo %%c + 100 = !result1!
  6. call :cacl %%c - 100 result2
  7. echo %%c - 100 = !result2!
  8. call :cacl %%c * 100 result3
  9. echo %%c * 100 = !result3!
  10. call :cacl %%c / 100 result4
  11. echo %%c / 100 = !result4!
  12. )
  13. pause&exit
  14. :cacl
  15. REM 参数 call :cacl 数字1 运算符 数字2 保存结果的变量名
  16. set "cacl=mshta "javascript:var fso=new ActiveXObject("Scripting.FileSystemObject");fso.GetStandardStream(1).WriteLine(%~1%~2%~3);window.close()""
  17. for /f "delims=" %%i in ('%cacl%') do set "%~4=%%i"
  18. goto :eof
复制代码

TOP

本帖最后由 5haiji 于 2020-7-15 12:46 编辑

回复 3# went
谢谢哥 好用~~~~~~

TOP

返回列表