Advertisement
Guest User

Judago

a guest
Mar 11th, 2010
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. @ECHO Off
  2. ::Bigmath.bat
  3.  
  4. ECHO %~1|FIND "?"||if not "%~2"=="" GOTO BEGIN
  5. :help
  6. echo.&echo "%~nx0" A NT Command script to circumvent large number limitations and the lack of
  7. echo floating point returned by set /a in situations where speed is not critical.&echo.
  8. echo Usage:&echo "%~nx0" number1 operator number2 [outputvariable]&echo.
  9. echo Where numberx is your input numbers to be calculated and operator is a valid operator
  10. echo.&echo Valid operators are:&echo - Subtract.&echo + Add.&echo * Multiply.&echo / Divide.
  11. echo %% Modulo&echo BD - Bin to decimal&echo DB - Devimal to bin&echo.
  12. echo Output variable is optional - if present the output will be set to the variable name
  13. echo provided rather than be printed to screen.&echo.&echo.
  14. pause
  15. goto :eof
  16. :begin
  17. SETLOCAL ENABLEDELAYEDEXPANSION
  18. set optr=%~2
  19. set inpnum1=%1
  20. set inpnum2=0
  21. set dplace=
  22. set "varout=%~3"
  23. if /i not "%~2"=="bd" IF /I NOT "%~2"=="DB" (
  24. IF "%~3"=="" ENDLOCAL&GOTO HELP
  25. SET "VAROUT=%~4"
  26. set inpnum2=%3
  27. )
  28. if "%inpnum1%"=="%inpnum1:.=%" if "%inpnum2%"=="%inpnum2:.=%" goto FLOATSKIP
  29. if "%inpnum1:~0,1%"=="." set inpnum1=0%inpnum1%
  30. if "%inpnum2:~0,1%"=="." set inpnum2=0%inpnum2%
  31. if "%inpnum1:~-1%"=="." set inpnum1=%inpnum1%0
  32. if "%inpnum2:~-1%"=="." set inpnum2=%inpnum2%0
  33. SET DP1=0
  34. SET DP2=0
  35. FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM1%") DO (
  36. SET DPT=%%A
  37. FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP1=%%B&&GOTO BL1
  38. )
  39. :BL1
  40. FOR /F "TOKENS=2 DELIMS=." %%A IN ("%INPNUM2%") DO (
  41. SET DPT=%%A
  42. FOR /L %%B IN (0 1 500) DO IF "!DPT:~%%B!"=="" SET /A DP2=%%B&&GOTO BL2
  43. )
  44. :BL2
  45. SET DPLACE=%DP1%
  46. IF NOT !DP1! EQU !DP2! (
  47. IF !DP1! LSS !DP2! (
  48. SET DPLACE=%dp2%
  49. SET /A DPT=DP2-DP1
  50. FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM1=!INPNUM1!0
  51. ) ELSE (
  52. set dlace=%dp1%
  53. SET /A DPT=DP1-DP2
  54. FOR /L %%A IN (1 1 !DPT!) DO SET INPNUM2=!INPNUM2!0
  55. )
  56. )
  57.  
  58. SET INPNUM1=%INPNUM1:.=%
  59. SET INPNUM2=%INPNUM2:.=%
  60. :FLOATSKIP
  61. :dzer01
  62. if not "!inpnum1:~1!"=="" if "!inpnum1:~0,1!"=="0" (
  63. set inpnum1=!inpnum1:~1!
  64. goto dzer01
  65. )
  66. :dzer02
  67. if not "!inpnum2:~1!"=="" if "!inpnum2:~0,1!"=="0" (
  68. set inpnum2=!inpnum2:~1!
  69. goto dzer02
  70. )
  71.  
  72.  
  73. if "!optr!"=="-" goto subtract
  74. if "!optr!"=="+" goto add
  75. if "!optr!"=="*" goto multiply
  76. if "!optr!"=="/" (
  77. if !inpnum2! lss 1000000 (
  78. goto fastdiv
  79. ) else (
  80. goto divide
  81. )
  82. )
  83. IF /I "!optr!"=="BD" GOTO BINTODEC
  84. IF /I "!optr!"=="DB" GOTO DECTOBIN
  85. if "!optr!"=="%%" goto mod
  86. goto help
  87.  
  88. :subtract
  89. for %%a in (stotal stten SUB1 SUB2 neg) do set %%a=
  90. if /I "%~1%~2"=="internal" (
  91. set sub1=!%3!
  92. set sub2=!%4!
  93. ) else (
  94. set sub1=!inpNUM1!
  95. set sub2=!inpNUM2!
  96. )
  97.  
  98. set subsub1=!sub2!
  99. set subsub2=!sub1!
  100. :sloop
  101. set sc1=
  102. set sc2=
  103. set sstrore=
  104. if defined sub1 (
  105. if defined sub2 (
  106. set sc1=%sub1:~-1%
  107. set sc2=%sub2:~-1%
  108. set sub1=%sub1:~0,-1%
  109. set sub2=%sub2:~0,-1%
  110. ) else (
  111. if defined stten (
  112. set sub2=!stten!
  113. set stten=
  114. set sc1=!sub1:~-1!
  115. set sc2=!sub2:~-1!
  116. set sub1=!sub1:~0,-1!
  117. set sub2=!sub2:~0,-1!
  118. ) else (
  119. set stotal=%sub1%%stotal%
  120. call :finish stotal
  121. goto soutput
  122. )
  123. )
  124. ) else (
  125. for %%a in (sub2 stten) do (
  126. if defined %%a (
  127. set sub1=%subsub1%
  128. set sub2=%subsub2%
  129. set neg=-
  130. set stotal=
  131. set stten=
  132. goto sloop
  133. )
  134. )
  135. call :finish stotal
  136. goto soutput
  137. )
  138. set sazc=
  139. set sazcnt=
  140. :asttend
  141. if !sc1!==0 if !sc2! gtr 0 if defined sub1 (
  142. set sazc=!sub1:~-1!
  143. set /a sazcnt+=1
  144. set sub1=!sub1:~0,-1!
  145. if !sazc!==0 goto asttend
  146. set /a sazc-=1
  147. set sc1=10
  148. set sub1=!sub1!!sazc!
  149. for /l %%a in (2,1,!sazcnt!) do set sub1=!sub1!9
  150. )
  151. if defined stten (
  152. set /a sc1-=1
  153. if !sc1! lss 0 (
  154. set /a stten+=1
  155. set /a sc1+=10
  156. ) else (
  157. set stten=
  158. )
  159. )
  160. set /a sstrore=%sc1%-%sc2%
  161. if !sstrore! lss 0 (
  162. set stten=1
  163. set /a sstrore+=10
  164. set stten=!stten:-=!
  165. )
  166. set stotal=%sstrore%%stotal%
  167. goto sloop
  168. :soutput
  169. if /I "%~1%~2"=="internal" (
  170. set %3=%stotal%
  171. goto :eof
  172. ) else (
  173. if not defined varout (
  174. echo %stotal%
  175. ENDLOCAL
  176. ) else (
  177. endlocal&set %varout%=%stotal%
  178. )
  179. )
  180. goto :eof
  181.  
  182. :add
  183. if /I "%~1%~2"=="internal" (
  184. set add1=!%3!
  185. set add2=!%4!
  186. ) else (
  187. set add1=!inpNUM1!
  188. set add2=!inpNUM2!
  189. )
  190. set atotal=
  191. set astore=
  192. set acarry=
  193. :aloop
  194. if not defined add1 (
  195. if defined acarry (
  196. set add1=%acarry%
  197. set acarry=
  198. ) else (
  199. set atotal=%add2%%atotal%
  200. call :finish atotal
  201. goto aoutput
  202. )
  203. )
  204. if not defined add2 (
  205. if defined acarry (
  206. set add2=%acarry%
  207. set acarry=
  208. ) else (
  209. set atotal=%add1%%atotal%
  210. call :finish atotal
  211. goto aoutput
  212. )
  213. )
  214. if not defined acarry (
  215. set /a astore=%add1:~-1%+%add2:~-1%
  216. ) else (
  217. set /a astore=%add1:~-1%+%add2:~-1%+%acarry%
  218. set acarry=
  219. )
  220. if !astore! lss 10 (
  221. set atotal=%astore%%atotal%
  222. ) else (
  223. set atotal=%astore:~-1%%atotal%
  224. set acarry=%astore:~0,-1%
  225. )
  226. set astore=
  227. set add1=%add1:~0,-1%
  228. set add2=%add2:~0,-1%
  229. goto aloop
  230. :aoutput
  231. if /I "%~1%~2"=="internal" (
  232. set %3=%atotal%
  233. ) else (
  234. if not defined varout (
  235. echo %atotal%
  236. ENDLOCAL
  237. ) else (
  238. endlocal&set %varout%=%atotal%
  239. )
  240. )
  241. goto :eof
  242.  
  243.  
  244. :MULTIPLY
  245. SET MTOTAL=0
  246. set /a ph=dplace*2
  247. set dplace=
  248. set mten=
  249. set mult1=!inpnum1!
  250. set mult2=!inpnum2!
  251. IF !mult1! equ 0 set mtotal=0&&goto moutput
  252. IF !mult2! equ 0 set mtotal=0&&goto moutput
  253. IF !mult2! equ 1 (
  254. set mtotal=%mult1%
  255. set dplace=!ph!
  256. call :finish mtotal
  257. goto moutput
  258. ) else (
  259. IF !mult1! equ 1 (
  260. set mtotal=%mult2%
  261. set dplace=!ph!
  262. call :finish mtotal
  263. goto moutput
  264. )
  265. )
  266. set m1=!mult1!
  267. for /l %%a in (2,1,9) do (
  268. call :add inte rnal m1 mult1
  269. set m%%a=!m1!
  270. )
  271. set m1=!mult1!
  272. :MPLY
  273. if defined mult2 (
  274. set mby=!mult2:~-1!
  275. set mult2=!mult2:~0,-1!
  276. ) else (
  277. set dplace=!ph!
  278. call :finish mtotal
  279. goto moutput
  280. )
  281. if !mby!==0 (
  282. set mten=%mten%0
  283. goto mply
  284. )
  285. set madvar=!m%mby%!%mten%
  286. call :add inte rnal mtotal madvar
  287. set mten=%mten%0
  288. goto mply
  289. :moutput
  290. if not defined varout (
  291. echo %mtotal%
  292. ENDLOCAL
  293. ) else (
  294. endlocal&set %varout%=%mtotal%
  295. )
  296. goto :eof
  297.  
  298. :divide
  299. if !inpnum2! equ 1 (
  300. set dtotal=%inpnum1%
  301. call :finish dtotal
  302. goto doutput
  303. )
  304. for %%a in (din1 din2 diff dten DCNT) do set %%a=
  305. set dtotal=0
  306. set dplace=10
  307. set inpnum1=!inpnum1!0000000000
  308. set div1=!inpnum1!
  309. set div2=!inpnum2!
  310. if !div2!==0 echo Divide by 0^^!>&2&&exit /b 2
  311. :din1
  312. if defined inpnum1 (
  313. set /a din1+=1
  314. set inpnum1=%inpnum1:~1%
  315. goto din1
  316. )
  317. :din2
  318. if defined inpnum2 (
  319. set /a din2+=1
  320. set inpnum2=%inpnum2:~1%
  321. goto din2
  322. )
  323. if !din2! gtr !din1! (
  324. set dtotal=0
  325. goto doutput
  326. )
  327. set /a diff=din1-din2
  328. for /l %%a in (1,1,%diff%) do set dten=!dten!0
  329. set ph=!dplace!
  330. set dplace=
  331. :div
  332. set dtmp=%div1%
  333. set daddvar=%div2%%dten%
  334. call :subtract inte rnal div1 daddvar
  335. if defined neg (
  336. set div1=!dtmp!
  337. set neg=
  338. if defined dten (
  339. set dten=!dten:~1!
  340. goto div
  341. ) else (
  342. set dplace=!ph!
  343. call :finish dtotal
  344. goto doutput
  345. )
  346. ) else (
  347. set daddvar=1%dten%
  348. call :add inte rnal dtotal daddvar
  349. if !div1! equ 0 (
  350. set dplace=!ph!
  351. call :finish dtotal
  352. goto doutput
  353. )
  354. goto div
  355. )
  356. :doutput
  357. if not defined varout (
  358. echo %dtotal%
  359. ENDLOCAL
  360. ) else (
  361. endlocal&set %varout%=%dtotal%
  362. )
  363. goto :eof
  364.  
  365. :fastdiv
  366. set fccnt=
  367. set dplace=10
  368. set fdiv=%inpnum1%0000000000
  369. set ftotal=
  370. :fsplit
  371. if not defined fdiv goto floop
  372. if !fdiv! gtr 999 (
  373. set /a fccnt+=1
  374. set fchunk!fccnt!=%fdiv:~-4%
  375. set fdiv=%fdiv:~0,-4%
  376. goto fsplit
  377. ) else (
  378. set /a fccnt+=1
  379. set fchunk!fccnt!=!Fdiv!
  380. )
  381. :floop
  382. if defined fchunk%fccnt% (
  383. if "!fchunk%fccnt%:~0,1!"=="0" (
  384. set fchunk%fccnt%=!fchunk%fccnt%:~1!
  385. goto floop
  386. )
  387. ) else (
  388. set fchunk%fccnt%=0
  389. goto fpad
  390. )
  391. set fvcnt=0
  392. :fsubloop
  393. If !fchunk%fccnt%! geq !inpnum2! (
  394. If !fchunk%fccnt%! geq !inpnum2!000 (
  395. set /a fchunk%fccnt%-=!inpnum2!000
  396. set /a fvcnt+=1000
  397. goto fsubloop
  398. ) else (
  399. If !fchunk%fccnt%! geq !inpnum2!00 (
  400. set /a fchunk%fccnt%-=!inpnum2!00
  401. set /a fvcnt+=100
  402. goto fsubloop
  403. ) else (
  404. If !fchunk%fccnt%! geq !inpnum2!0 (
  405. set /a fchunk%fccnt%-=!inpnum2!0
  406. set /a fvcnt+=10
  407. goto fsubloop
  408. ) else (
  409. set /a fchunk%fccnt%-=!inpnum2!
  410. set /a fvcnt+=1
  411. goto fsubloop
  412. )
  413. )
  414. )
  415. )
  416. :fpad
  417. if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
  418. if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
  419. if "!fvcnt:~3,1!"=="" set fvcnt=0!fvcnt!
  420. set ftotal=%ftotal%%Fvcnt%
  421. set fvcnt=0
  422. set ftmpcnt=%fccnt%
  423. set /a fccnt-=1
  424. if !fchunk%ftmpcnt%! gtr 0 (
  425. set fcarry=!fchunk%ftmpcnt%!
  426. set fchunk!fccnt!=!fcarry!!fchunk%fccnt%!
  427. )
  428. if %fccnt% equ 0 (
  429. call :finish ftotal
  430. goto foutput
  431. )
  432. goto floop
  433. :foutput
  434. if not defined varout (
  435. echo %ftotal%
  436. ENDLOCAL
  437. ) else (
  438. endlocal&set %varout%=%ftotal%
  439. )
  440. goto :eof
  441.  
  442.  
  443. :dectobin
  444. IF "!INPNUM1:0=!"=="" (
  445. if defined varout (endlocal&set "%varout%=0") else ECHO 0
  446. endlocal
  447. GOTO :EOF
  448. )
  449. IF DEFINED DPLACE (
  450. 1>&2 ECHO ONLY WHOLE NUMBERS ARE SUPPOTED FOR BINARY CONVERSION.
  451. PAUSE
  452. ENDLOCAL
  453. EXIT /B 1
  454. )
  455. set dB=%inpnum1%
  456. set DBbin=
  457. :DBloop
  458. call "%~F0" %DB% / 2 DBtest
  459. for /f "tokens=1,2 delims=." %%a in ("%DBtest%") do (
  460. set dB=%%a
  461. if "%%b"=="" (
  462. set DBbin=0%DBbin%
  463. ) else (
  464. set DBbin=1%DBbin%
  465. )
  466. )
  467. if not %DB%==0 goto DBloop
  468. if defined varout (
  469. endlocal&set "%varout%=%Dbbin%"
  470. ) else (
  471. echo %dbbin%
  472. endlocal
  473. )
  474. exit /b
  475. GOTO :EOF
  476.  
  477.  
  478. :BINTODEC
  479. set BDbin=%INPNUM1%
  480. set bdtest=%bdbin:0=%
  481. if defined bdtest if not "%bdtest:1=%"=="" (
  482. 1>&2 echo bad input for a binary number - "!bdbin!"
  483. endlocal
  484. exit /b 1
  485. )
  486. set BD=0
  487. set BDbase=1
  488. :BDloop
  489. if not defined BDbin (
  490. if defined varout (
  491. endlocal&set "%varout%=%BD%"
  492. ) else (
  493. echo %bd%
  494. endlocal
  495. )
  496. exit /b
  497. )
  498. if "%BDbin:~-1%"=="1" call :ADD INTE RNAL BD BDBase
  499. call "%~F0" %BDbase% * 2 BDbase
  500. set BDbin=%BDbin:~0,-1%
  501. goto BDloop
  502.  
  503.  
  504. :finish
  505. if !dplace! equ 0 set dplace=
  506. if defined dplace if "!%1:~%dplace%,1!"=="" set %1=0!%1!&&goto finish
  507. if defined dplace set %1=!%1:~0,-%dplace%!.!%1:~-%dplace%!
  508. :killzerodec
  509. if defined dplace (
  510. if "!%1:~-1!"=="0" (
  511. set %1=!%1:~0,-1!
  512. goto killzerodec
  513. ) else (
  514. if "!%1:~-1!"=="." (
  515. set %1=!%1:~0,-1!
  516. )
  517. )
  518. )
  519. :killzerowhle
  520. if "!%1:~0,1!"=="0" (
  521. set %1=!%1:~1!
  522. goto killzerowhle
  523. ) else (
  524. if "!%1:~0,1!"=="." set %1=0!%1!
  525. )
  526. IF NOT DEFINED %1 SET %1=0
  527. set %1=%neg%!%1!
  528. goto :eof
  529.  
  530.  
  531. :mod
  532. if "!inpnum2!"=="0" (
  533. echo modulo by zero
  534. goto :eof
  535. )
  536. if "!inpnum1!"=="!inpnum2!" (
  537. if defined varout (
  538. endlocal&set "%varout%=0"
  539. ) else (
  540. echo 0
  541. endlocal
  542. )
  543. goto :eof
  544. )
  545. if not "!inpnum1:-=!"=="!inpnum1!" (
  546. echo Negitive Modulo not supported
  547. goto :eof
  548. )
  549. if not "!inpnum2:-=!"=="!inpnum2!" (
  550. echo Negitive Modulo not supported
  551. goto :eof
  552. )
  553. set holder=!dplace!
  554. set dplace=
  555. set neg=
  556. set mtotal=0
  557. set mod1=!inpnum1!
  558. set mod2=!inpnum2!
  559. set 10x=0000000000000000000000000000000000
  560.  
  561. :mdloop
  562. set mtotal=%mod1%
  563. set mdsub=!mod2!!10x!
  564. call :subtract inte rnal mod1 mdsub
  565. if not defined neg goto mdloop
  566.  
  567. if not "!10x!"=="" (
  568. set neg=
  569. set 10x=!10x:~1!
  570. set mod1=!mtotal!
  571. goto mdloop
  572. )
  573.  
  574.  
  575. set dplace=!holder!
  576. set neg=
  577. call :finish mtotal
  578. if defined varout (
  579. endlocal&set "%varout%=%mtotal%"
  580. ) else (
  581. echo %mtotal%
  582. endlocal
  583. )
  584. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement