Powered by GeSHi

Source code for add.bat

(view source code of add.bat as plain text)

  1. @ECHO OFF
  2. SETLOCAL ENABLEEXTENSIONS
  3. SETLOCAL ENABLEDELAYEDEXPANSION
  4.  
  5. SET _FIRST=%1
  6. SET _SECOND=%2
  7. CALL :ADD _FIRST _SECOND _ANSWER
  8. ECHO !_ANSWER!
  9.  
  10. ENDLOCAL
  11. ENDLOCAL
  12. GOTO :END
  13.  
  14.  
  15. :ADD
  16.    ::ADD NUMBERS LARGER THAN 32-BITS
  17.    ::SYNTAX: CALL ADD _VAR1 _VAR2 _VAR3
  18.    ::VER 1.2
  19.    ::hieyeque1@gmail.com - drop me a note telling me
  20.    ::if this has helped you.  Sometimes I don't know if anyone uses my stuff
  21.    ::Its free for the world to use.
  22.    ::I retain the rights to it though, you may not copyright this
  23.    ::to prevent others from using it, you may however copyright works
  24.    ::as a whole that use this code.
  25.    ::Just don't try to stop others from using this through some legal means.
  26.    ::CopyRight Brian Williams 8/29/2009
  27.    :: _VAR1 = VARIABLE FIRST NUMBER TO ADD
  28.    :: _VAR2 = VARIABLE SECOND NUMBER TO ADD
  29.    :: _VAR3 = VARIABLE NUMBER RETURNED
  30.    SET _RESULT=
  31.    SETLOCAL
  32.    SET _NUM1=!%1!
  33.    SET _NUM2=!%2!
  34.    IF NOT DEFINED _NUM1 SET _NUM1=0
  35.    IF NOT DEFINED _NUM2 SET _NUM2=0
  36.    FOR /L %%A IN (1,1,2) DO (
  37.       FOR /L %%B IN (0,1,9) DO (
  38.          SET _NUM%%A=!_NUM%%A:%%B=%%B !
  39.          )
  40.       )
  41.    FOR %%A IN (%_NUM1%) DO SET /A _NUM1CNT+=1 & SET _!_NUM1CNT!_NUM1=%%A
  42.    FOR %%A IN (%_NUM2%) DO SET /A _NUM2CNT+=1 & SET _!_NUM2CNT!_NUM2=%%A
  43.    SET _NUM1=%_NUM1: =%
  44.    SET _NUM2=%_NUM2: =%
  45.    SET /A _DIGITS=%_NUM1CNT% + %_NUM2CNT%
  46.    IF %_DIGITS% LEQ 8 (
  47.       SET /A _RESULT=%_NUM1% + %_NUM2%
  48.       )
  49.    IF DEFINED _RESULT (ENDLOCAL & SET %3=%_RESULT%& GOTO :EOF)
  50.    IF %_NUM1CNT% GEQ %_NUM2CNT% (SET _MAXOPS=%_NUM1CNT%) ELSE (SET _MAXOPS=%_NUM2CNT%)
  51.    SET /A _MAXOPS=%_MAXOPS% - 1
  52.    IF %_NUM1CNT% GTR %_NUM2CNT% (
  53.       SET /A _ZEROS=%_NUM1CNT% - %_NUM2CNT%
  54.       FOR /L %%A IN (1,1,!_ZEROS!) DO SET _ZERO=!_ZERO!0
  55.       SET _NUM2=!_ZERO!!_NUM2!
  56.       )
  57.  
  58.    IF %_NUM2CNT% GTR %_NUM1CNT% (
  59.       SET /A _ZEROS=%_NUM2CNT% - %_NUM1CNT%
  60.       FOR /L %%A IN (1,1,!_ZEROS!) DO SET _ZERO=!_ZERO!0
  61.       SET _NUM1=!_ZERO!!_NUM1!
  62.       )
  63.  
  64.    FOR /L %%A IN (!_MAXOPS!,-1,0) DO (
  65.       SET /A _TMP=!_NUM1:~%%A,1! + !_NUM2:~%%A,1! !_PLUS! !_CO!
  66.       SET _CO=
  67.       SET _PLUS=
  68.       IF !_TMP! GTR 9 SET _CO=!_TMP:~0,1!& SET _TMP=!_TMP:~-1!& SET _PLUS=+
  69.       SET _RETURN=!_TMP!!_RETURN!
  70.       SET _TMP=
  71.       )
  72.    IF DEFINED _CO SET _RETURN=%_CO%%_RETURN%
  73.    SET _RESULT=%_RETURN%
  74.    IF DEFINED _RESULT (ENDLOCAL&SET %3=%_RESULT%)
  75.    GOTO :EOF
  76.  
  77. :END
  78.