Advertisement
Guest User

STR_MATH.BAT 2.1 JUDAGO

a guest
Dec 20th, 2016
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 17.92 KB | None | 0 0
  1. @ECHO Off
  2. SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  3. if "%~3"=="" (
  4.     echo.
  5.     echo. STR_MATH.BAT v2.1
  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. if /i "!_INP_OP!"=="y" (
  155.     for /f "tokens=1,2* delims=." %%a in ("0.!_INP_NUM1!!_INP_NUM2!") do (
  156.         if not "%%c"=="" (
  157.             1>&2 echo ERROR: Fractional exponents are not supported
  158.             goto :eof
  159.         )
  160.     )
  161. )
  162.  
  163. for %%a in (1 2) do (
  164.     for /f "tokens=1-3 delims=." %%b in ("0!_INP_NUM%%a!0") do (
  165.         if not "%%d"=="" (
  166.             1>&2 echo ERROR: Invalid input: "!_INP_NUM%%a!"
  167.             goto :eof
  168.         )
  169.     )
  170. )
  171. for /f "tokens=1* delims=/Xx+-mMYy" %%a in ("A+!_INP_OP!") do (
  172.     if not "%%b"=="" (
  173.         1>&2 echo ERROR: Invalid operator: "!_INP_OP!"
  174.         goto :eof
  175.     )
  176. )
  177.  
  178. rem ***** End input validation *****
  179.  
  180. rem ***** Start Remove leading zero's / Return for zero sums  *****
  181. for %%a in (1 2) do (
  182.     for /f "tokens=1* delims=0" %%b in ("A0!_INP_NUM%%a!") do (
  183.         if "%%c"=="" (
  184.             if /i "!_INP_OP!"=="x" echo 0
  185.             if "!_INP_OP!"=="/" (
  186.                 if %%a==2 (
  187.                     1>&2 Echo ERROR: Divide by zero
  188.                     goto :eof
  189.                 ) else echo 0
  190.             ) else if /i "!_INP_OP!"=="m" (
  191.                 if %%a==2 (
  192.                     1>&2 Echo ERROR: Divide by zero
  193.                     goto :eof
  194.                 ) else echo 0
  195.             ) else if "!_INP_OP!"=="-" (
  196.                 if %%a==1 (
  197.                     if defined _INP_NEG2 (
  198.                         echo !_INP_NUM2!
  199.                     ) else echo -!_INP_NUM2!
  200.                 ) else (
  201.                     echo !_INP_NEG1!!_INP_NUM1!
  202.                 )
  203.             ) else if "!_INP_OP!"=="+" (
  204.                 if %%a==1 (
  205.                     echo !_INP_NEG2!!_INP_NUM2!
  206.                 ) else echo !_INP_NEG1!!_INP_NUM1!
  207.             ) else if /i "!_INP_OP!"=="y" (
  208.                 if "%%a"=="1" (
  209.                     if "!_INP_NEG2!"=="-" (
  210.                         1>&2 echo ERROR: Negative exponents of zero are undefined.
  211.                     ) else (
  212.                         echo 0
  213.                     )
  214.                 ) else (
  215.                     if "!_INP_EXP_PAR!"=="1" (
  216.                         echo 1
  217.                     ) else (
  218.                         echo !_INP_NEG1!1
  219.                     )
  220.                 )
  221.             )
  222.             goto :eof
  223.         ) else set _INP_NUM%%a=%%c
  224.     )
  225. )
  226. rem ***** End Remove leading zero's / Return for zero sums *****
  227.  
  228. rem ***** Start Floating point normalisation *****
  229. for %%a in (1 2) do (
  230.     if "!_INP_NUM%%a:~0,1!"=="." set _INP_NUM%%a=0!_INP_NUM%%a!
  231.     if "!_INP_NUM%%a:~-1!"=="." set _INP_NUM%%a=!_INP_NUM%%a!0
  232.     for /l %%b in (0 1 9) do set _INP_NUM%%a=!_INP_NUM%%a:%%b=%%b !
  233.     for %%c in (!_INP_NUM%%a!) do set /a _INP_LEN%%a+=1
  234.     set _INP_NUM%%a=!_INP_NUM%%a: =!
  235.     if "!_INP_NUM%%a!"=="!_INP_NUM%%a:.=!" (
  236.         set _INP_DP%%a=0
  237.     ) else (
  238.         for /l %%d in (!_INP_LEN%%a! -1 1) do (
  239.             if not defined _INP_DP%%a if "!_INP_NUM%%a:~%%d,1!"=="." (
  240.                 set /a _INP_DP%%a=!_INP_LEN%%a! - %%d
  241.             )
  242.         )
  243.     )
  244.     set _INP_NUM%%a=!_INP_NUM%%a:.=!
  245. )
  246.  
  247. if !_INP_DP1! gtr !_INP_DP2! (
  248.     set /a _OUT_DP=_INP_DP1 - 1
  249. ) else set /a _OUT_DP=_INP_DP2 - 1
  250. for /l %%a in (!_INP_DP1! 1 !_OUT_DP!) do set _INP_NUM1=!_INP_NUM1!0
  251. for /l %%a in (!_INP_DP2! 1 !_OUT_DP!) do set _INP_NUM2=!_INP_NUM2!0
  252. rem ***** End Floating point normalisation *****
  253.  
  254. rem ***** Start Negitive output checking *****
  255. if /i "!_INP_OP!"=="x" (
  256.     if "!_INP_NEG1!!_INP_NEG2!"=="-" set _OUT_NEG=-
  257. ) else if "!_INP_OP!"=="+" (
  258.     if "!_INP_NEG1!!_INP_NEG2!"=="--" set _OUT_NEG=-
  259.     if defined _INP_NEG2 if not defined _INP_NEG1 (
  260.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  261.         if "!_GTR_RES!"=="3" (
  262.             set _INP_NUM1=%_INP_NUM2%
  263.             set _INP_NUM2=%_INP_NUM1%
  264.             set _OUT_NEG=-
  265.         )
  266.         set _INP_OP=-
  267.     )
  268.     if defined _INP_NEG1 if not defined _INP_NEG2 (
  269.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  270.         if "!_GTR_RES!"=="3" (
  271.             set _INP_NUM1=%_INP_NUM2%
  272.             set _INP_NUM2=%_INP_NUM1%
  273.         ) else if "!_GTR_RES!"=="1" set _OUT_NEG=-
  274.         set _INP_OP=-
  275.     )
  276. ) else if "!_INP_OP!"=="-" (
  277.     if "!_INP_NEG1!!_INP_NEG2!"=="" (
  278.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  279.         if "!_GTR_RES!"=="3" (
  280.             set _INP_NUM1=%_INP_NUM2%
  281.             set _INP_NUM2=%_INP_NUM1%
  282.             set _OUT_NEG=-
  283.         )
  284.     )
  285.     if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  286.         call :isgreater !_INP_NUM1! !_INP_NUM2!
  287.         if "!_GTR_RES!"=="3" (
  288.             set _INP_NUM1=%_INP_NUM2%
  289.             set _INP_NUM2=%_INP_NUM1%
  290.         ) else if "!_GTR_RES!"=="1" set _OUT_NEG=-
  291.     )
  292.     if defined _INP_NEG2 if not defined _INP_NEG1 set _INP_OP=+
  293.     if defined _INP_NEG1 if not defined _INP_NEG2 (
  294.         set _OUT_NEG=-
  295.         set _INP_OP=+
  296.     )
  297. ) else if "!_INP_OP!"=="/" (
  298.     if "!_INP_NEG1!!_INP_NEG2!"=="--" set _OUT_NEG=
  299.     if "!_INP_NEG1!!_INP_NEG2!"=="-"  set _OUT_NEG=-
  300. ) else if /i "!_INP_OP!"=="M" (
  301.     if defined _INP_NEG1 set _OUT_NEG=-
  302. ) else if /i "!_INP_OP!"=="Y" (
  303.     if "!_INP_EXP_PAR!"=="0" (
  304.         if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  305.             set _OUT_NEG=-
  306.             set _INP_EXP_DIV=1
  307.         ) else if "!_INP_NEG2!"=="-" (
  308.             set _INP_EXP_DIV=1
  309.         ) else if "!_INP_NEG1!"=="-" (
  310.             set _OUT_NEG=-
  311.         )
  312.     ) else (
  313.         if "!_INP_NEG1!!_INP_NEG2!"=="--" (
  314.             for %%z in (1 3 5 7 9) do (
  315.                 if "!_INP_NUM2:~-1!"=="%%z" set _OUT_NEG=-
  316.             )
  317.             set _INP_EXP_DIV=1
  318.         ) else if "!_INP_NEG2!"=="-" (
  319.             set _OUT_NEG=-
  320.             set _INP_EXP_DIV=1
  321.         ) else if "!_INP_NEG1!"=="-" (
  322.             for %%z in (1 3 5 7 9) do (
  323.                 if "!_INP_NUM2:~-1!"=="%%z" set _OUT_NEG=-
  324.             )
  325.         )
  326.     )
  327. )
  328.  
  329. rem ***** End Negitive output checking *****
  330.  
  331.  
  332. Rem Main calling routines
  333.  
  334.  
  335.  
  336. if "!_INP_OP!"=="-" (
  337.     call :subtract %_INP_NUM1% %_INP_NUM2%
  338. ) else if "!_INP_OP!"=="+" (
  339.     call :add %_INP_NUM1% %_INP_NUM2%
  340. ) else if /i "!_INP_OP!"=="X" (
  341.     set /a _OUT_DP=^(_OUT_DP * 2^) + 1
  342.     call :multiply %_INP_NUM1% %_INP_NUM2%
  343. ) else if /i "!_INP_OP!"=="m" (
  344.     call :divide %_INP_NUM1% %_INP_NUM2% 0 m
  345. ) else if "!_INP_OP!"=="/" (
  346.     call :divide %_INP_NUM1% %_INP_NUM2% %_DIV_DP%
  347.     set /a _OUT_DP=_DIV_DP - 1
  348. ) else if /i "!_INP_OP!"=="Y" (
  349.     call :EXPONENT !_INP_NUM1! !_INP_NUM2!
  350.     if "!_INP_EXP_DIV!"=="1" (
  351.         call :DIVIDE 1 !_RET_NUM! !_DIV_DP!
  352.         set /a _OUT_DP=_DIV_DP - 1
  353.     )
  354. ) else (
  355.     1>&2 echo ERROR: Unknown operator.
  356.     goto :eof
  357. )
  358.  
  359.  
  360.  
  361. rem finishing up.....
  362.  
  363.  
  364. if not defined _OUT_DP (
  365.     set _OUT_DP=0
  366. ) else set /a _OUT_DP+=1
  367. if not "!_OUT_DP!"=="0" (
  368.     for /l %%a in (1 1 !_OUT_DP!) do if "!_RET_NUM:~%_OUT_DP%!"=="" set _RET_NUM=0!_RET_NUM!
  369.     set _RET_NUM=!_RET_NUM:~0^,-%_OUT_DP%!.!_RET_NUM:~-%_OUT_DP%!
  370.     for /l %%a in (!_OUT_DP! -1 1) do (
  371.         if "!_RET_NUM:~-1!"=="0" set _RET_NUM=!_RET_NUM:~0^,-1!
  372.         if "!_RET_NUM:~-1!"=="." set _RET_NUM=!_RET_NUM:~0^,-1!
  373.     )
  374. )
  375. for /f "tokens=1* delims=0" %%a in ("A0!_RET_NUM!") do (
  376.     if "%%b"=="" (
  377.         set _RET_NUM=0
  378.     ) else set _RET_NUM=%%b
  379. )
  380. if "!_RET_NUM:~0,1!"=="." set _RET_NUM=0!_RET_NUM!
  381. IF NOT DEFINED _RET_NUM SET _RET_NUM=0
  382. echo %_OUT_NEG%%_RET_NUM%
  383.  
  384.  
  385.  
  386.  
  387. goto :eof
  388.  
  389. rem ***********************************************************************
  390. rem ****************************** Subroutines ****************************
  391. rem ***********************************************************************
  392.  
  393. :SUBTRACT
  394. set _SUB_NUM1=%~1
  395. set _SUB_NUM2=%~2
  396. for %%a in (CHA RES LEN1 LEN2 CAR TOT) do set _SUB_%%a=
  397. for %%a in (1 2) do (
  398.     for /l %%b in (0 1 9) do set _SUB_NUM%%a=!_SUB_NUM%%a:%%b=%%b !
  399.     for %%c in (!_SUB_NUM%%a!) do set /a _SUB_LEN%%a+=1
  400.     set _SUB_NUM%%a=!_SUB_NUM%%a: =!
  401. )
  402. if !_SUB_LEN1! gtr !_SUB_LEN2! (
  403.     set /a _SUB_LEN=_SUB_LEN1 - 1
  404. ) else set /a _SUB_LEN=_SUB_LEN2 - 1
  405. for /l %%b in (!_SUB_LEN1! 1 !_SUB_LEN!) do set _SUB_NUM1=0!_SUB_NUM1!
  406. for /l %%b in (!_SUB_LEN2! 1 !_SUB_LEN!) do set _SUB_NUM2=0!_SUB_NUM2!
  407. for /l %%a in (!_SUB_LEN! -1 0) do (
  408.     set /a _SUB_RES=!_SUB_NUM1:~%%a,1! - !_SUB_NUM2:~%%a,1!
  409.     if !_SUB_RES! lss 0 (
  410.         set _SUB_TAKE=
  411.         for /l %%b in (%%a -1 0) do if not defined _SUB_TAKE (
  412.             if not "%%b"=="%%a" (
  413.                 if not "!_SUB_NUM1:~%%b,1!"=="0" (
  414.                     set /a _SUB_CHA=!_SUB_NUM1:~%%b,1! - 1
  415.                     set /a _SUB_TAKE=%%b + 1
  416.                     for %%c in (!_SUB_TAKE!) do set _SUB_NUM1=!_SUB_NUM1:~0,%%b!!_SUB_CHA!!_SUB_NUM1:~%%c!
  417.                     set /a _SUB_RES=1!_SUB_NUM1:~%%a,1! - !_SUB_NUM2:~%%a,1!
  418.  
  419.                 ) else (
  420.                     set /a _SUB_CHA=%%b + 1
  421.                     for %%c in (!_SUB_CHA!) do set _SUB_NUM1=!_SUB_NUM1:~0,%%b!9!_SUB_NUM1:~%%c!
  422.                 )
  423.             )
  424.         )
  425.     )
  426.     set _SUB_TOT=!_SUB_RES!!_SUB_TOT!
  427. )
  428. for /f "tokens=1* delims=0" %%a in ("A0!_SUB_TOT!") do set _SUB_TOT=%%b
  429. if not defined _SUB_TOT set _SUB_TOT=0
  430. set _RET_NUM=%_SUB_TOT%
  431. goto :eof
  432.  
  433.  
  434. :ADD
  435. set _ADD_NUM1=%~1
  436. set _ADD_NUM2=%~2
  437. for %%a in (LEN1 LEN2 CAR TOT) do set _ADD_%%a=
  438. for %%a in (1 2) do (
  439.     for /l %%b in (0 1 9) do set _ADD_NUM%%a=!_ADD_NUM%%a:%%b=%%b !
  440.     for %%c in (!_ADD_NUM%%a!) do set /a _ADD_LEN%%a+=1
  441.     set _ADD_NUM%%a=!_ADD_NUM%%a: =!
  442. )
  443. if !_ADD_LEN1! gtr !_ADD_LEN2! (
  444.     set /a _ADD_LEN=_ADD_LEN1 - 1
  445. ) else set /a _ADD_LEN=_ADD_LEN2 - 1
  446. for /l %%b in (!_ADD_LEN1! 1 !_ADD_LEN!) do set _ADD_NUM1=0!_ADD_NUM1!
  447. for /l %%b in (!_ADD_LEN2! 1 !_ADD_LEN!) do set _ADD_NUM2=0!_ADD_NUM2!
  448. for /l %%a in (!_ADD_LEN! -1 0) do (
  449.     set /a _ADD_CAR=_ADD_CAR + !_ADD_NUM1:~%%a,1! + !_ADD_NUM2:~%%a,1!
  450.     set _ADD_TOT=!_ADD_CAR:~-1!!_ADD_TOT!
  451.     set _ADD_CAR=!_ADD_CAR:~0,-1!
  452.     if not defined _ADD_CAR set _ADD_CAR=0
  453. )
  454. if !_ADD_CAR! gtr 0 set _ADD_TOT=!_ADD_CAR!!_ADD_TOT!
  455. set _RET_NUM=%_ADD_TOT%
  456. goto :eof
  457.  
  458.  
  459. :MULTIPLY
  460. set _MUL_NUM1=%1
  461. set _MUL_NUM2=%2
  462. for %%a in (CAR TOT) do set _MUL_%%a=0
  463. for %%a in (X10 REV1 REV2) do set _MUL_%%a=
  464. for %%a in (1 2) do (
  465.     for /l %%b in (0 1 9) do set _MUL_NUM%%a=!_MUL_NUM%%a:%%b=%%b !
  466.     for %%c in (!_MUL_NUM%%a!) do set _MUL_REV%%a=%%c !_MUL_REV%%a!
  467. )
  468. for %%a in (!_MUL_REV1!) do (
  469.     set _MUL_ROW=!_MUL_X10!
  470.     for %%b in (!_MUL_REV2!) do (
  471.         set /a _MUL_CAR=^(%%a * %%b^) + _MUL_CAR
  472.         set _MUL_ROW=!_MUL_CAR:~-1!!_MUL_ROW!
  473.         set _MUL_CAR=!_MUL_CAR:~0,-1!
  474.         if not defined _MUL_CAR set _MUL_CAR=0
  475.     )
  476.     for /f "tokens=1* delims=0" %%c in ("A0!_MUL_CAR!!_MUL_ROW!") do (
  477.         if not "%%d"=="" (
  478.             call :ADD %%d !_MUL_TOT!
  479.             set _MUL_TOT=!_RET_NUM!
  480.         )
  481.     )
  482.     set _MUL_CAR=
  483.     set _MUL_X10=!_MUL_X10!0
  484. )
  485. set _RET_NUM=%_MUL_TOT%
  486. goto :eof
  487.  
  488. :DIVIDE
  489. for %%a in (LEN1 LEN2 X10 PAD) do set _DIV_%%a=
  490. if /i not "%4"=="m" (
  491.     for /l %%a in (1 1 %3) do set _DIV_PAD=0!_DIV_PAD!
  492. )
  493. set _DIV_NUM1=%1!_DIV_PAD!
  494. set _DIV_NUM2=%2
  495. set _DIV_TOT=0
  496. set _DIV_PRC=1
  497. for %%a in (1 2) do (
  498.     for /f "tokens=1* delims=0" %%d in ("A0!_DIV_NUM%%a!") do set _DIV_NUM%%a=%%e
  499.     for /l %%b in (0 1 9) do set _DIV_NUM%%a=!_DIV_NUM%%a:%%b=%%b !
  500.     for %%c in (!_DIV_NUM%%a!) do set /a _DIV_LEN%%a+=1
  501.     set _DIV_NUM%%a=!_DIV_NUM%%a: =!
  502. )
  503. set /a _DIV_LEN1-=1
  504. for /l %%a in (!_DIV_LEN2! 1 !_DIV_LEN1!) do set _DIV_X10=0!_DIV_X10!
  505.  
  506. :__DIVINL
  507.     call :ISGREATER %_DIV_NUM1% %_DIV_NUM2%%_DIV_X10%
  508.     if %_GTR_RES% leq 2 (
  509.         call :SUBTRACT %_DIV_NUM1% %_DIV_NUM2%%_DIV_X10%
  510.         set _DIV_NUM1=!_RET_NUM!
  511.         call :ADD %_DIV_TOT% 1%_DIV_X10%
  512.         set _DIV_TOT=!_RET_NUM!
  513.     ) else if defined _DIV_X10 (
  514.         set _DIV_X10=!_DIV_X10:~1!
  515.     ) else set _DIV_PRC=0
  516.     if "!_DIV_PRC!"=="1" goto :__DIVINL
  517. if /i "%4"=="m" set _DIV_TOT=!_DIV_NUM1!
  518. set _RET_NUM=%_DIV_TOT%
  519. Goto :eof
  520.  
  521. :EXPONENT
  522. set _EXP_SRL=
  523. set _EXP_SQU=%2
  524. set _EXP_TOT=%1
  525. set _EXP_PRC=1
  526. :__EXINL
  527.     call :DIVIDE !_EXP_SQU!0 2 0
  528.     set _EXP_SQU=%_RET_NUM%
  529.     if "%_EXP_SQU:~-1%"=="5" (
  530.         set _EXP_SRL=%_EXP_SRL% %_EXP_TOT%
  531.     )
  532.     set _EXP_SQU=%_EXP_SQU:~0,-1%
  533.     if "%_EXP_SQU%"=="" (
  534.         set _EXP_PRC=0
  535.     ) else (
  536.         call :MULTIPLY %_EXP_TOT% %_EXP_TOT%
  537.         set _EXP_TOT=!_RET_NUM!
  538.         if "%_EXP_SQU%"=="3" (
  539.             set _EXP_SRL=!_EXP_SRL! !_EXP_TOT!
  540.             set _EXP_SQU=2
  541.         )
  542.         if "!_EXP_SQU!"=="2" (
  543.             call :MULTIPLY !_EXP_TOT! !_EXP_TOT!
  544.             set _EXP_TOT=!_RET_NUM!
  545.             for %%z in (!_EXP_SRL!) do (
  546.                 call :MULTIPLY !_EXP_TOT! %%z
  547.                 set _EXP_TOT=!_RET_NUM!
  548.             )
  549.             set _EXP_PRC=0
  550.         )
  551.         if "!_EXP_SQU!"=="1" (
  552.             set _EXP_PRC=0
  553.         )
  554.     )
  555.     if "!_EXP_PRC!"=="1" goto __EXINL
  556. set _RET_NUM=!_EXP_TOT!
  557. goto :eof
  558.  
  559.  
  560. :ISGREATER
  561. set _GTR_RES=
  562. for /f "tokens=1* delims=0" %%a in ("A0%~1") do (
  563.     if "%%b"=="" (
  564.         set _GTR_NUM1=0
  565.     ) else set _GTR_NUM1=%%b
  566. )
  567. for /f "tokens=1* delims=0" %%a in ("A0%~2") do (
  568.     if "%%b"=="" (
  569.         set _GTR_NUM2=0
  570.     ) else set _GTR_NUM2=%%b
  571. )
  572. for %%a in (lEN1 lEN2) do set _GTR_%%a=
  573. for %%a in (1 2) do (
  574.     for /l %%b in (0 1 9) do set _GTR_NUM%%a=!_GTR_NUM%%a:%%b=%%b !
  575.     for %%c in (!_GTR_NUM%%a!) do set /a _GTR_lEN%%a+=1
  576.     set _GTR_NUM%%a=!_GTR_NUM%%a: =!
  577. )
  578. if !_GTR_lEN1! gtr !_GTR_lEN2! (
  579.     set _GTR_RES=1
  580. ) else if !_GTR_lEN2! gtr !_GTR_lEN1! (
  581.     set _GTR_RES=3
  582. ) else (
  583.     set /a _GTR_lEN1-=1
  584.     for /l %%a in (0 1 !_GTR_lEN1!) do (
  585.         if not defined _GTR_RES (
  586.             if !_GTR_NUM1:~%%a^,1! gtr !_GTR_NUM2:~%%a^,1! set _GTR_RES=1
  587.             if !_GTR_NUM2:~%%a^,1! gtr !_GTR_NUM1:~%%a^,1! set _GTR_RES=3
  588.         )
  589.     )
  590. )
  591. if not defined _GTR_RES set _GTR_RES=2
  592. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement