Advertisement
Guest User

str_math.bat v2.0

a guest
May 23rd, 2016
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 17.89 KB | None | 0 0
  1. @ECHO Off
  2. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  3. if "%~3"=="" (
  4.     echo.
  5.     echo. STR_MATH.BAT v2.0
  6.     echo.
  7.     echo.    Large number and floating point work around script for Addition,
  8.     echo.    Subtraction, Division, Multiplaction, Modulus and exponents.
  9.     echo.    Floating point and negative numbers supported.
  10.     echo.
  11.     echo.    Usage:
  12.     echo.      STR_MATH.BAT {Number} {+-XM} {Number}
  13.     echo.      STR_MATH.BAT {Number} {/Y} {Number} [n max decimal places]
  14.     echo.
  15.     echo.    Division and certain negative exponents default to a maximum
  16.     echo.    of ten decimal places of output. An optional user defined max
  17.     echo.    can be input as the fourth number when applicable.
  18.     echo.
  19.     echo.    No rounding is performed on output of division, the number
  20.     echo.    is only truncated. For example a result of 0.1257 with three
  21.     echo.    places will return 0.125 NOT 0.126.
  22.     echo.
  23.     echo.    Square brackets can be used on negative base exponents instead
  24.     echo.    of parentheses if needed:
  25.     echo.       STR_MATH [-5] Y 10
  26.     echo.       STR_MATH -5 Y 10
  27.     echo.
  28.     echo.    Fractional exponents are not supported.
  29.     echo.
  30.     echo.   Results are not garanteed. Provided AS-IS.
  31.     echo.
  32.     echo.   Output Will be echo'd, a "for /f" loop can be used to
  33.     echo.   capture the output. An example is as follows, be sure
  34.     echo.   NOT to use the /A switch to SET if using a variable.
  35.     echo.    
  36.     echo.     FOR /F %%A IN ^(' STR_MATH.BAT 50.265 X 1.36 '^) DO SET RESULT=%%A
  37.     echo.
  38.     echo.   Variables must be expanded when passed to the script, like so:
  39.     echo.
  40.     echo.     FOR /F %%A IN ^(' STR_MATH.BAT %%number1%% / %%number2%% '^) DO SET RESULT=%%A
  41.     echo.
  42.     echo.   Judago  2011, 2015 ^& 2016.
  43.     goto :eof
  44. )
  45.    
  46.  
  47.  
  48.  
  49. set _INP_NUM1=
  50. set _INP_NUM2=
  51. set _INP_NEG1=
  52. set _INP_NEG2=
  53. set _INP_OP=
  54. set _RET_NUM=
  55. set _OUT_DP=
  56. set _OUT_NEG=
  57. set _INP_EXP_PAR=0
  58. set _INP_EXP_DIV=0
  59. set "_INP_NUM1=%~1"
  60. set "_INP_NUM2=%~3"
  61. set "_INP_OP=%~2"
  62. if /i "%~2"=="*" (
  63.     set _INP_OP=x
  64. )
  65. if /i "%~2"=="%%" (
  66.     set _INP_OP=m
  67. )
  68.  
  69.    
  70. rem Default number of digits for division
  71. set _DIV_DP=10
  72.  
  73.  
  74.  
  75. rem ***** Start Input validation *****
  76. for %%z in ("/" "y") do (
  77.     if /i "%~2"=="%%~z" if not "%~4"=="" (
  78.          for /f "delims=0123456789 tokens=1*" %%a in ("A0%~4") do (
  79.             if "%%b"=="" (
  80.                 for /f "tokens=1* delims=0" %%c in ("A0%~4") do (
  81.                     if "%%d"=="" (
  82.                         set _DIV_DP=0
  83.                     ) else set _DIV_DP=%%d
  84.                 )
  85.             ) else (
  86.                 1>&2 Echo ERROR: "%~4" not detected as a valid number of decimal places
  87.                 goto :eof
  88.             )
  89.         )
  90.         shift /4
  91.     )
  92. )
  93.      
  94. if not "%~4"=="" (
  95.     1>&2 echo ERROR: Too many arguments: "%~4"
  96.     goto :eof
  97. )
  98. if not defined _INP_NUM1 (
  99.     1>&2 echo ERROR: Invalid Input: "!_INP_NUM1!"
  100.     goto :eof
  101. )
  102. if not defined _INP_NUM2 (
  103.     1>&2 echo ERROR: Invalid Input: "!_INP_NUM2!"
  104.     goto :eof
  105. )
  106. if not defined _INP_OP (
  107.     1>&2 echo ERROR: Invalid Operator: "!_INP_OP!"
  108.     goto :eof
  109. )
  110. if not "!_INP_OP:~1!"=="" (
  111.     1>&2 echo ERROR: Invalid Operator: "!_INP_OP!"
  112.     goto :eof
  113. )
  114.  
  115.  
  116. rem **** check for brackets on base exponent ****
  117. if /i "!_INP_OP!"=="Y" (
  118.     for /f "tokens=1,2* delims=[]" %%a in ("A!_INP_NUM1!A]") do (
  119.         if "%%a%%c"=="AA]" (
  120.             set _INP_EXP_PAR=1
  121.             set _INP_NUM1=!_INP_NUM1:[=!
  122.             set _INP_NUM1=!_INP_NUM1:]=!
  123.         )
  124.     )
  125. )
  126.  
  127. rem ***** / ***** Start Negitive input ***** \ *****
  128. for %%a in (1 2) do (
  129. if "!_INP_NUM%%a:~0,1!"=="-" if not "!_INP_NUM%%a:~1!"=="" (
  130.         set _INP_NUM%%a=!_INP_NUM%%a:~1!
  131.         set _INP_NEG%%a=-
  132.     ) else (
  133.         1>&2 echo ERROR: Invalid input: "!_INP_NUM%%a!"
  134.         goto :eof
  135.     )
  136. )
  137.  
  138. if not defined _INPUT_NEG1 set _INP_EXP_PAR=0
  139.  
  140. rem ***** \ ***** End Negitive input ***** / *****
  141.  
  142.  
  143.  
  144.  
  145. rem ****** Bad characters ******
  146. for /f "tokens=1* delims=0123456789." %%a in ("A0!_INP_NUM1!!_INP_NUM2!") do (
  147.     if not "%%b"=="" (
  148.         1>&2 echo ERROR: Invalid input: "!_INP_NUM1!" **OR** "!_INP_NUM2!"
  149.         goto :eof
  150.     )
  151. )
  152.  
  153. rem ***** Fractional exponenets aren't supported ******
  154. for /f "tokens=1,2* delims=." %%a in ("0.!_INP_NUM1!!_INP_NUM2!") do (
  155.     if not "%%c"=="" (
  156.         1>&2 echo ERROR: Fractional exponents are not supported
  157.         goto :eof
  158.     )
  159. )
  160.  
  161. for %%a in (1 2) do (
  162.     for /f "tokens=1-3 delims=." %%b in ("0!_INP_NUM%%a!0") do (
  163.         if not "%%d"=="" (
  164.             1>&2 echo ERROR: Invalid input: "!_INP_NUM%%a!"
  165.             goto :eof
  166.         )
  167.     )
  168. )
  169. for /f "tokens=1* delims=/Xx+-mMYy" %%a in ("A+!_INP_OP!") do (
  170.     if not "%%b"=="" (
  171.         1>&2 echo ERROR: Invalid operator: "!_INP_OP!"
  172.         goto :eof
  173.     )
  174. )
  175.  
  176. rem ***** End input validation *****
  177.  
  178. rem ***** Start Remove leading zero's / Return for zero sums  *****
  179. for %%a in (1 2) do (
  180.     for /f "tokens=1* delims=0" %%b in ("A0!_INP_NUM%%a!") do (
  181.         if "%%c"=="" (
  182.             if /i "!_INP_OP!"=="x" echo 0
  183.             if "!_INP_OP!"=="/" (
  184.                 if %%a==2 (
  185.                     1>&2 Echo ERROR: Divide by zero
  186.                     goto :eof
  187.                 ) else echo 0
  188.             ) else if /i "!_INP_OP!"=="m" (
  189.                 if %%a==2 (
  190.                     1>&2 Echo ERROR: Divide by zero
  191.                     goto :eof
  192.                 ) else echo 0
  193.             ) else if "!_INP_OP!"=="-" (
  194.                 if %%a==1 (
  195.                     if defined _INP_NEG2 (
  196.                         echo !_INP_NUM2!
  197.                     ) else echo -!_INP_NUM2!
  198.                 ) else (
  199.                     echo !_INP_NEG1!!_INP_NUM1!
  200.                 )
  201.             ) else if "!_INP_OP!"=="+" (
  202.                 if %%a==1 (
  203.                     echo !_INP_NEG2!!_INP_NUM2!
  204.                 ) else echo !_INP_NEG1!!_INP_NUM1!
  205.             ) else if /i "!_INP_OP!"=="y" (
  206.                 if "%%a"=="1" (
  207.                     if "!_INP_NEG2!"=="-" (
  208.                         1>&2 echo ERROR: Negative exponents of zero are undefined.
  209.                     ) else (
  210.                         echo 0
  211.                     )
  212.                 ) else (
  213.                     if "!_INP_EXP_PAR!"=="1" (
  214.                         echo 1
  215.                     ) else (
  216.                         echo !_INP_NEG1!1
  217.                     )
  218.                 )
  219.             )
  220.             goto :eof
  221.         ) else set _INP_NUM%%a=%%c
  222.     )
  223. )
  224. rem ***** End Remove leading zero's / Return for zero sums *****
  225.  
  226. rem ***** Start Floating point normalisation *****
  227. for %%a in (1 2) do (
  228.     if "!_INP_NUM%%a:~0,1!"=="." set _INP_NUM%%a=0!_INP_NUM%%a!
  229.     if "!_INP_NUM%%a:~-1!"=="." set _INP_NUM%%a=!_INP_NUM%%a!0
  230.     for /l %%b in (0 1 9) do set _INP_NUM%%a=!_INP_NUM%%a:%%b=%%b !
  231.     for %%c in (!_INP_NUM%%a!) do set /a _INP_LEN%%a+=1
  232.     set _INP_NUM%%a=!_INP_NUM%%a: =!
  233.     if "!_INP_NUM%%a!"=="!_INP_NUM%%a:.=!" (
  234.         set _INP_DP%%a=0
  235.     ) else (
  236.         for /l %%d in (!_INP_LEN%%a! -1 1) do (
  237.             if not defined _INP_DP%%a if "!_INP_NUM%%a:~%%d,1!"=="." (
  238.                 set /a _INP_DP%%a=!_INP_LEN%%a! - %%d
  239.             )
  240.         )
  241.     )
  242.     set _INP_NUM%%a=!_INP_NUM%%a:.=!
  243. )
  244.  
  245. if !_INP_DP1! gtr !_INP_DP2! (
  246.     set /a _OUT_DP=_INP_DP1 - 1
  247. ) else set /a _OUT_DP=_INP_DP2 - 1
  248. for /l %%a in (!_INP_DP1! 1 !_OUT_DP!) do set _INP_NUM1=!_INP_NUM1!0
  249. for /l %%a in (!_INP_DP2! 1 !_OUT_DP!) do set _INP_NUM2=!_INP_NUM2!0
  250. rem ***** End Floating point normalisation *****
  251.  
  252. rem ***** Start Negitive output checking *****
  253. if /i "!_INP_OP!"=="x" (
  254.     if "!_INP_NEG1!!_INP_NEG2!"=="-" set _OUT_NEG=-
  255. ) else if "!_INP_OP!"=="+" (
  256.     if "!_INP_NEG1!!_INP_NEG2!"=="--" set _OUT_NEG=-
  257.     if defined _INP_NEG2 if not defined _INP_NEG1 (
  258.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  259.         if "!_GTR_RES!"=="3" (
  260.             set _INP_NUM1=%_INP_NUM2%
  261.             set _INP_NUM2=%_INP_NUM1%
  262.             set _OUT_NEG=-
  263.         )
  264.         set _INP_OP=-
  265.     )
  266.     if defined _INP_NEG1 if not defined _INP_NEG2 (
  267.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  268.         if "!_GTR_RES!"=="3" (
  269.             set _INP_NUM1=%_INP_NUM2%
  270.             set _INP_NUM2=%_INP_NUM1%
  271.         ) else if "!_GTR_RES!"=="1" set _OUT_NEG=-
  272.         set _INP_OP=-
  273.     )
  274. ) else if "!_INP_OP!"=="-" (
  275.     if "!_INP_NEG1!!_INP_NEG2!"=="" (
  276.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  277.         if "!_GTR_RES!"=="3" (
  278.             set _INP_NUM1=%_INP_NUM2%
  279.             set _INP_NUM2=%_INP_NUM1%
  280.             set _OUT_NEG=-
  281.         )
  282.     )
  283.     if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  284.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  285.         if "!_GTR_RES!"=="3" (
  286.             set _INP_NUM1=%_INP_NUM2%
  287.             set _INP_NUM2=%_INP_NUM1%
  288.         ) else if "!_GTR_RES!"=="1" set _OUT_NEG=-
  289.     )
  290.     if defined _INP_NEG2 if not defined _INP_NEG1 set _INP_OP=+
  291.     if defined _INP_NEG1 if not defined _INP_NEG2 (
  292.         set _OUT_NEG=-
  293.         set _INP_OP=+
  294.     )
  295. ) else if "!_INP_OP!"=="/" (
  296.     if "!_INP_NEG1!!_INP_NEG2!"=="--" set _OUT_NEG=
  297.     if "!_INP_NEG1!!_INP_NEG2!"=="-"  set _OUT_NEG=-
  298. ) else if /i "!_INP_OP!"=="M" (
  299.     if defined _INP_NEG1 set _OUT_NEG=-
  300. ) else if /i "!_INP_OP!"=="Y" (
  301.     if "!_INP_EXP_PAR!"=="0" (
  302.         if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  303.             set _OUT_NEG=-
  304.             set _INP_EXP_DIV=1
  305.         ) else if "!_INP_NEG2!"=="-" (
  306.             set _INP_EXP_DIV=1
  307.         ) else if "!_INP_NEG1!"=="-" (
  308.             set _OUT_NEG=-
  309.         )
  310.     ) else (
  311.         if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  312.             for %%z in (1 3 5 7 9) do (
  313.                 if "!_INP_NUM2:~-1!"=="%%z" set _OUT_NEG=-
  314.             )
  315.             set _INP_EXP_DIV=1
  316.         ) else if "!_INP_NEG2!"=="-" (
  317.             set _OUT_NEG=-
  318.             set _INP_EXP_DIV=1
  319.         ) else if "!_INP_NEG1!"=="-" (
  320.             for %%z in (1 3 5 7 9) do (
  321.                 if "!_INP_NUM2:~-1!"=="%%z" set _OUT_NEG=-
  322.             )
  323.         )
  324.     )
  325. )
  326.  
  327. rem ***** End Negitive output checking *****
  328.  
  329.  
  330. Rem Main calling routines
  331.  
  332.  
  333.  
  334. if "!_INP_OP!"=="-" (
  335.     call :subtract %_INP_NUM1% %_INP_NUM2%
  336. ) else if "!_INP_OP!"=="+" (
  337.     call :add %_INP_NUM1% %_INP_NUM2%
  338. ) else if /i "!_INP_OP!"=="X" (
  339.     set /a _OUT_DP=^(_OUT_DP * 2^) + 1
  340.     call :multiply %_INP_NUM1% %_INP_NUM2%
  341. ) else if /i "!_INP_OP!"=="m" (
  342.     call :divide %_INP_NUM1% %_INP_NUM2% 0 m
  343. ) else if "!_INP_OP!"=="/" (
  344.     call :divide %_INP_NUM1% %_INP_NUM2% %_DIV_DP%
  345.     set /a _OUT_DP=_DIV_DP - 1
  346. ) else if /i "!_INP_OP!"=="Y" (
  347.     call :EXPONENT !_INP_NUM1! !_INP_NUM2!
  348.     if "!_INP_EXP_DIV!"=="1" (
  349.         call :DIVIDE 1 !_RET_NUM! !_DIV_DP!
  350.         set /a _OUT_DP=_DIV_DP - 1
  351.     )
  352. ) else (
  353.     1>&2 echo ERROR: Unknown operator.
  354.     goto :eof
  355. )
  356.  
  357.  
  358.  
  359. rem finishing up.....
  360.  
  361. set _FIN_FLAG=
  362. if not defined _OUT_DP (
  363.     set _OUT_DP=0
  364. ) else set /a _OUT_DP+=1
  365. if not "!_OUT_DP!"=="0" (
  366.     for /l %%a in (1 1 !_OUT_DP!) do if "!_RET_NUM:~%_OUT_DP%!"=="" set _RET_NUM=0!_RET_NUM!
  367.     set _RET_NUM=!_RET_NUM:~0^,-%_OUT_DP%!.!_RET_NUM:~-%_OUT_DP%!
  368.     for /l %%a in (!_OUT_DP! -1 1) do (
  369.         if "!_RET_NUM:~-1!"=="0" set _RET_NUM=!_RET_NUM:~0^,-1!
  370.         if "!_RET_NUM:~-1!"=="." set _RET_NUM=!_RET_NUM:~0^,-1!
  371.     )
  372. )
  373. for /f "tokens=1* delims=0" %%a in ("A0!_RET_NUM!") do (
  374.     if "%%b"=="" (
  375.         set _RET_NUM=0
  376.     ) else set _RET_NUM=%%b
  377. )
  378. if "!_RET_NUM:~0,1!"=="." set _RET_NUM=0!_RET_NUM!
  379. IF NOT DEFINED _RET_NUM SET _RET_NUM=0
  380. echo %_OUT_NEG%%_RET_NUM%
  381.  
  382.  
  383.  
  384.  
  385. goto :eof
  386.  
  387. rem ***********************************************************************
  388. rem ****************************** Subroutines ****************************
  389. rem ***********************************************************************
  390.  
  391. :SUBTRACT
  392. set _SUB_NUM1=%~1
  393. set _SUB_NUM2=%~2
  394. for %%a in (CHA RES LEN1 LEN2 CAR TOT) do set _SUB_%%a=
  395. for %%a in (1 2) do (
  396.     for /l %%b in (0 1 9) do set _SUB_NUM%%a=!_SUB_NUM%%a:%%b=%%b !
  397.     for %%c in (!_SUB_NUM%%a!) do set /a _SUB_LEN%%a+=1
  398.     set _SUB_NUM%%a=!_SUB_NUM%%a: =!
  399. )
  400. if !_SUB_LEN1! gtr !_SUB_LEN2! (
  401.     set /a _SUB_LEN=_SUB_LEN1 - 1
  402. ) else set /a _SUB_LEN=_SUB_LEN2 - 1
  403. for /l %%b in (!_SUB_LEN1! 1 !_SUB_LEN!) do set _SUB_NUM1=0!_SUB_NUM1!
  404. for /l %%b in (!_SUB_LEN2! 1 !_SUB_LEN!) do set _SUB_NUM2=0!_SUB_NUM2!
  405. for /l %%a in (!_SUB_LEN! -1 0) do (
  406.     set /a _SUB_RES=!_SUB_NUM1:~%%a,1! - !_SUB_NUM2:~%%a,1!
  407.     if !_SUB_RES! lss 0 (
  408.         set _SUB_TAKE=
  409.         for /l %%b in (%%a -1 0) do if not defined _SUB_TAKE (
  410.             if not "%%b"=="%%a" (
  411.                 if not "!_SUB_NUM1:~%%b,1!"=="0" (
  412.                     set /a _SUB_CHA=!_SUB_NUM1:~%%b,1! - 1
  413.                     set /a _SUB_TAKE=%%b + 1
  414.                     for %%c in (!_SUB_TAKE!) do set _SUB_NUM1=!_SUB_NUM1:~0,%%b!!_SUB_CHA!!_SUB_NUM1:~%%c!
  415.                     set /a _SUB_RES=1!_SUB_NUM1:~%%a,1! - !_SUB_NUM2:~%%a,1!
  416.  
  417.                 ) else (
  418.                     set /a _SUB_CHA=%%b + 1
  419.                     for %%c in (!_SUB_CHA!) do set _SUB_NUM1=!_SUB_NUM1:~0,%%b!9!_SUB_NUM1:~%%c!
  420.                 )
  421.             )
  422.         )
  423.     )
  424.     set _SUB_TOT=!_SUB_RES!!_SUB_TOT!
  425. )
  426. for /f "tokens=1* delims=0" %%a in ("A0!_SUB_TOT!") do set _SUB_TOT=%%b
  427. if not defined _SUB_TOT set _SUB_TOT=0
  428. set _RET_NUM=%_SUB_TOT%
  429. goto :eof
  430.  
  431.  
  432. :ADD
  433. set _ADD_NUM1=%~1
  434. set _ADD_NUM2=%~2
  435. for %%a in (LEN1 LEN2 CAR TOT) do set _ADD_%%a=
  436. for %%a in (1 2) do (
  437.     for /l %%b in (0 1 9) do set _ADD_NUM%%a=!_ADD_NUM%%a:%%b=%%b !
  438.     for %%c in (!_ADD_NUM%%a!) do set /a _ADD_LEN%%a+=1
  439.     set _ADD_NUM%%a=!_ADD_NUM%%a: =!
  440. )
  441. if !_ADD_LEN1! gtr !_ADD_LEN2! (
  442.     set /a _ADD_LEN=_ADD_LEN1 - 1
  443. ) else set /a _ADD_LEN=_ADD_LEN2 - 1
  444. for /l %%b in (!_ADD_LEN1! 1 !_ADD_LEN!) do set _ADD_NUM1=0!_ADD_NUM1!
  445. for /l %%b in (!_ADD_LEN2! 1 !_ADD_LEN!) do set _ADD_NUM2=0!_ADD_NUM2!
  446. for /l %%a in (!_ADD_LEN! -1 0) do (
  447.     set /a _ADD_CAR=_ADD_CAR + !_ADD_NUM1:~%%a,1! + !_ADD_NUM2:~%%a,1!
  448.     set _ADD_TOT=!_ADD_CAR:~-1!!_ADD_TOT!
  449.     set _ADD_CAR=!_ADD_CAR:~0,-1!
  450.     if not defined _ADD_CAR set _ADD_CAR=0
  451. )
  452. if !_ADD_CAR! gtr 0 set _ADD_TOT=!_ADD_CAR!!_ADD_TOT!
  453. set _RET_NUM=%_ADD_TOT%
  454. goto :eof
  455.  
  456.  
  457. :MULTIPLY
  458. set _MUL_NUM1=%1
  459. set _MUL_NUM2=%2
  460. for %%a in (CAR TOT) do set _MUL_%%a=0
  461. for %%a in (X10 REV1 REV2) do set _MUL_%%a=
  462. for %%a in (1 2) do (
  463.     for /l %%b in (0 1 9) do set _MUL_NUM%%a=!_MUL_NUM%%a:%%b=%%b !
  464.     for %%c in (!_MUL_NUM%%a!) do set _MUL_REV%%a=%%c !_MUL_REV%%a!
  465. )
  466. for %%a in (!_MUL_REV1!) do (
  467.     set _MUL_ROW=!_MUL_X10!
  468.     for %%b in (!_MUL_REV2!) do (
  469.         set /a _MUL_CAR=^(%%a * %%b^) + _MUL_CAR
  470.         set _MUL_ROW=!_MUL_CAR:~-1!!_MUL_ROW!
  471.         set _MUL_CAR=!_MUL_CAR:~0,-1!
  472.         if not defined _MUL_CAR set _MUL_CAR=0
  473.     )
  474.     for /f "tokens=1* delims=0" %%c in ("A0!_MUL_CAR!!_MUL_ROW!") do (
  475.         if not "%%d"=="" (
  476.             call :ADD %%d !_MUL_TOT!
  477.             set _MUL_TOT=!_RET_NUM!
  478.         )
  479.     )
  480.     set _MUL_CAR=
  481.     set _MUL_X10=!_MUL_X10!0
  482. )
  483. set _RET_NUM=%_MUL_TOT%
  484. goto :eof
  485.  
  486. :DIVIDE
  487. for %%a in (LEN1 LEN2 X10 PAD) do set _DIV_%%a=
  488. if /i not "%4"=="m" (
  489.     for /l %%a in (1 1 %3) do set _DIV_PAD=0!_DIV_PAD!
  490. )
  491. set _DIV_NUM1=%1!_DIV_PAD!
  492. set _DIV_NUM2=%2
  493. set _DIV_TOT=0
  494. set _DIV_PRC=1
  495. for %%a in (1 2) do (
  496.     for /f "tokens=1* delims=0" %%d in ("A0!_DIV_NUM%%a!") do set _DIV_NUM%%a=%%e
  497.     for /l %%b in (0 1 9) do set _DIV_NUM%%a=!_DIV_NUM%%a:%%b=%%b !
  498.     for %%c in (!_DIV_NUM%%a!) do set /a _DIV_LEN%%a+=1
  499.     set _DIV_NUM%%a=!_DIV_NUM%%a: =!
  500. )
  501. set /a _DIV_LEN1-=1
  502. for /l %%a in (!_DIV_LEN2! 1 !_DIV_LEN1!) do set _DIV_X10=0!_DIV_X10!
  503.  
  504. :__DIVINL
  505.     call :ISGREATER %_DIV_NUM1% %_DIV_NUM2%%_DIV_X10%
  506.     if %_GTR_RES% leq 2 (
  507.         call :SUBTRACT %_DIV_NUM1% %_DIV_NUM2%%_DIV_X10%
  508.         set _DIV_NUM1=!_RET_NUM!
  509.         call :ADD %_DIV_TOT% 1%_DIV_X10%
  510.         set _DIV_TOT=!_RET_NUM!
  511.     ) else if defined _DIV_X10 (
  512.         set _DIV_X10=!_DIV_X10:~1!
  513.     ) else set _DIV_PRC=0
  514.     if "!_DIV_PRC!"=="1" goto :__DIVINL
  515. if /i "%4"=="m" set _DIV_TOT=!_DIV_NUM1!
  516. set _RET_NUM=%_DIV_TOT%
  517. Goto :eof
  518.  
  519. :EXPONENT
  520. set _EXP_SRL=
  521. set _EXP_SQU=%2
  522. set _EXP_TOT=%1
  523. set _EXP_PRC=1
  524. :__EXINL
  525.     call :DIVIDE !_EXP_SQU!0 2 0
  526.     set _EXP_SQU=%_RET_NUM%
  527.     if "%_EXP_SQU:~-1%"=="5" (
  528.         set _EXP_SRL=%_EXP_SRL% %_EXP_TOT%
  529.     )
  530.     set _EXP_SQU=%_EXP_SQU:~0,-1%
  531.     if "%_EXP_SQU%"=="" (
  532.         set _EXP_PRC=0
  533.     ) else (
  534.         call :MULTIPLY %_EXP_TOT% %_EXP_TOT%
  535.         set _EXP_TOT=!_RET_NUM!
  536.         if "%_EXP_SQU%"=="3" (
  537.             set _EXP_SRL=!_EXP_SRL! !_EXP_TOT!
  538.             set _EXP_SQU=2
  539.         )
  540.         if "!_EXP_SQU!"=="2" (
  541.             call :MULTIPLY !_EXP_TOT! !_EXP_TOT!
  542.             set _EXP_TOT=!_RET_NUM!
  543.             for %%z in (!_EXP_SRL!) do (
  544.                 call :MULTIPLY !_EXP_TOT! %%z
  545.                 set _EXP_TOT=!_RET_NUM!
  546.             )
  547.             set _EXP_PRC=0
  548.         )
  549.         if "!_EXP_SQU!"=="1" (
  550.             set _EXP_PRC=0
  551.         )
  552.     )
  553.     if "!_EXP_PRC!"=="1" goto __EXINL
  554. set _RET_NUM=!_EXP_TOT!
  555. goto :eof
  556.  
  557.  
  558. :ISGREATER
  559. set _GTR_RES=
  560. for /f "tokens=1* delims=0" %%a in ("A0%~1") do (
  561.     if "%%b"=="" (
  562.         set _GTR_NUM1=0
  563.     ) else set _GTR_NUM1=%%b
  564. )
  565. for /f "tokens=1* delims=0" %%a in ("A0%~2") do (
  566.     if "%%b"=="" (
  567.         set _GTR_NUM2=0
  568.     ) else set _GTR_NUM2=%%b
  569. )
  570. for %%a in (lEN1 lEN2) do set _GTR_%%a=
  571. for %%a in (1 2) do (
  572.     for /l %%b in (0 1 9) do set _GTR_NUM%%a=!_GTR_NUM%%a:%%b=%%b !
  573.     for %%c in (!_GTR_NUM%%a!) do set /a _GTR_lEN%%a+=1
  574.     set _GTR_NUM%%a=!_GTR_NUM%%a: =!
  575. )
  576. if !_GTR_lEN1! gtr !_GTR_lEN2! (
  577.     set _GTR_RES=1
  578. ) else if !_GTR_lEN2! gtr !_GTR_lEN1! (
  579.     set _GTR_RES=3
  580. ) else (
  581.     set /a _GTR_lEN1-=1
  582.     for /l %%a in (0 1 !_GTR_lEN1!) do (
  583.         if not defined _GTR_RES (
  584.             if !_GTR_NUM1:~%%a^,1! gtr !_GTR_NUM2:~%%a^,1! set _GTR_RES=1
  585.             if !_GTR_NUM2:~%%a^,1! gtr !_GTR_NUM1:~%%a^,1! set _GTR_RES=3
  586.         )
  587.     )
  588. )
  589. if not defined _GTR_RES set _GTR_RES=2
  590. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement