home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_show / gcd / integer.e next >
Text File  |  1999-06-05  |  7KB  |  224 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. expanded class INTEGER
  17.  
  18. inherit
  19.    INTEGER_REF
  20.       redefine
  21.          infix "+", infix "-", infix "*", infix "/", infix "\\", infix "//",
  22.          infix "<", prefix "-", prefix "+"
  23.       end;
  24.  
  25. feature {ANY}
  26.  
  27.    infix "+"(other: INTEGER): INTEGER is
  28.          -- Add `other' to Current.
  29.       external "SmallEiffel"
  30.       end;
  31.  
  32.    infix "-" (other : INTEGER): INTEGER is
  33.          -- Subtract `other' from Current.
  34.       external "SmallEiffel"
  35.       end;
  36.  
  37.    infix "*" (other : INTEGER) : INTEGER is
  38.          -- Multiply `other' by Current.
  39.       external "SmallEiffel"
  40.       end;
  41.  
  42.    infix "/" (other : INTEGER): INTEGER is
  43.          -- Divide Current by `other'.
  44.          -- Note : Integer division
  45.       external "SmallEiffel"
  46.       end;
  47.  
  48.    infix "//" (other : INTEGER) : INTEGER is
  49.          -- Divide Current by `other'.
  50.          -- Note : Integer division
  51.       external "SmallEiffel"
  52.       end;
  53.  
  54.    infix "\\" (other : INTEGER) : INTEGER is
  55.          -- Remainder of division of Current by `other'.
  56.       external "SmallEiffel"
  57.       end;
  58.  
  59.    infix "<" (other: INTEGER): BOOLEAN is
  60.          -- Is Current less than `other'?
  61.       external "SmallEiffel"
  62.       end;
  63.  
  64.    prefix "+": INTEGER is
  65.       do
  66.          Result := Current
  67.       end;
  68.  
  69.    prefix "-" : INTEGER is
  70.          -- Unary minus of Current
  71.       external "SmallEiffel"
  72.       end;
  73.  
  74.    to_string: STRING is
  75.          -- Convert the INTEGER into a new allocated STRING.
  76.          -- Note: see `append_in' to save memory.
  77.       do
  78.          !!Result.make(0);
  79.          append_in(Result);
  80.       end;
  81.  
  82.    append_in(str: STRING) is
  83.          -- Append the equivalent of `to_string' at the end of
  84.          -- `str'. Thus you can save memory because no other
  85.          -- STRING is allocate for the job.
  86.       require
  87.          str /= Void;
  88.       local
  89.          val, i: INTEGER;
  90.       do
  91.          if Current = 0 then
  92.             str.extend('0');
  93.          else
  94.             if Current < 0 then
  95.                str.extend('-');
  96.                (- Current).append_in(str);
  97.             else
  98.                from
  99.                   i := str.count + 1;
  100.                   val := Current;
  101.                until
  102.                   val = 0
  103.                loop
  104.                   str.extend((val \\ 10).digit);
  105.                   val := val // 10;
  106.                end;
  107.                from
  108.                   val := str.count;
  109.                until
  110.                   i >= val
  111.                loop
  112.                   str.swap(i,val);
  113.                   val := val - 1;
  114.                   i := i + 1;
  115.                end;
  116.             end;
  117.          end;
  118.       end;
  119.  
  120.    to_string_format(s: INTEGER): STRING is
  121.          -- Same as `to_string' but the result is on `s' character and the
  122.          -- number is right aligned.
  123.          -- Note: see `append_in_format' to save memory.
  124.       require
  125.          to_string.count <= s;
  126.       do
  127.          from
  128.             tmp_string.clear;
  129.             append_in(tmp_string);
  130.          until
  131.             tmp_string.count >= s
  132.          loop
  133.             tmp_string.add_first(' ');
  134.          end;
  135.          Result := clone(tmp_string);
  136.       ensure
  137.          Result.count = s;
  138.       end;
  139.  
  140.    append_in_format(str: STRING; s: INTEGER) is
  141.          -- Append the equivalent of `to_string_format' at the end of
  142.          -- `str'. Thus you can save memory because no other
  143.          -- STRING is allocate for the job.
  144.       do
  145.          from
  146.             tmp_string.clear;
  147.             append_in(tmp_string);
  148.          until
  149.             tmp_string.count >= s
  150.          loop
  151.             tmp_string.add_first(' ');
  152.          end;
  153.          str.append(tmp_string);
  154.       ensure
  155.          str.count >= (old str.count) + s;
  156.       end;
  157.  
  158.    digit: CHARACTER is
  159.          -- Gives the corresponding CHARACTER for range 0..9.
  160.       require
  161.          0 <= Current;
  162.          Current <= 9;
  163.       do
  164.          Result := ("0123456789").item(Current + 1);
  165.       ensure
  166.          ("0123456789").has(Result);
  167.          Result.value = Current;
  168.       end;
  169.  
  170.    gcd(other: INTEGER): INTEGER is
  171.          -- Great Common Divisor of `Current' and `other'.
  172.       require
  173.          Current > 0;
  174.          other > 0;
  175.       local
  176.          the_other: INTEGER;
  177.       do
  178.          from
  179.             Result := Current;
  180.             the_other := other;
  181.          invariant
  182.             Result > 0;
  183.             the_other > 0;
  184.             Result.gcd(the_other) = Current.gcd(other);
  185.          variant
  186.             Result.max(the_other)
  187.          until
  188.             Result = the_other
  189.          loop
  190.             if Result > the_other then
  191.                Result := Result - the_other;
  192.             else
  193.                the_other := the_other - Result;
  194.             end;
  195.          end;
  196.       ensure
  197.          Result = other.gcd(Current);
  198.       end;
  199.  
  200. feature {NONE}
  201.  
  202.    tmp_string: STRING is "0123456789";
  203.  
  204.    to_character_table : STRING is "%
  205.          %%/000/%/001/%/002/%/003/%/004/%/005/%/006/%/007/%
  206.          %%/008/%/009/%/010/%/011/%/012/%/013/%/014/%/015/%
  207.          %%/016/%/017/%/018/%/019/%/020/%/021/%/022/%/023/%
  208.          %%/024/%/025/%/026/%/027/%/028/%/029/%/030/%/031/%
  209.          %%/032/%/033/%/034/%/035/%/036/%/037/%/038/%/039/%
  210.          %%/040/%/041/%/042/%/043/%/044/%/045/%/046/%/047/%
  211.          %%/048/%/049/%/050/%/051/%/052/%/053/%/054/%/055/%
  212.          %%/056/%/057/%/058/%/059/%/060/%/061/%/062/%/063/%
  213.          %%/064/%/065/%/066/%/067/%/068/%/069/%/070/%/071/%
  214.          %%/072/%/073/%/074/%/075/%/076/%/077/%/078/%/079/%
  215.          %%/080/%/081/%/082/%/083/%/084/%/085/%/086/%/087/%
  216.          %%/088/%/089/%/090/%/091/%/092/%/093/%/094/%/095/%
  217.          %%/096/%/097/%/098/%/099/%/100/%/101/%/102/%/103/%
  218.          %%/104/%/105/%/106/%/107/%/108/%/109/%/110/%/111/%
  219.          %%/112/%/113/%/114/%/115/%/116/%/117/%/118/%/119/%
  220.          %%/120/%/121/%/122/%/123/%/124/%/125/%/126/%/127/%
  221.          %";
  222.  
  223. end -- INTEGER
  224.