home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_01 / 2n01030a < prev    next >
Text File  |  1990-11-29  |  1KB  |  48 lines

  1.  
  2. FUNCTION IterativeCast9 (inputnumber : INTEGER) : INTEGER;
  3. { loops from right to left, totaling as it goes.
  4.    Keeps looping until the total is less than 10 }
  5. VAR
  6.  total : INTEGER;
  7. BEGIN
  8. WHILE (inputnumber >= 10)
  9. DO BEGIN
  10.    total := 0;
  11.    WHILE (inputnumber > 0)
  12.    DO BEGIN
  13.       { cut off the rightmost digit }
  14.       total := total + (inputnumber MOD 10);
  15.       inputnumber := inputnumber DIV 10;
  16.       END;
  17.    inputnumber := total;
  18.    END;
  19. IterativeCast9 := inputnumber;
  20. END;
  21.  
  22. FUNCTION LookupCast9 (inputnumber : INTEGER) : INTEGER;
  23. {  scans from right to left, keeping inputnumber running.  
  24. Casting is  done  via the look up table during the scan }
  25. VAR check : INTEGER;
  26. CONST
  27. CastTable : ARRAY [0..9, 0..9] OF INTEGER =
  28.     (( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  29.      ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 1),
  30.      ( 2, 3, 4, 5, 6, 7, 8, 9, 1, 2),
  31.      ( 3, 4, 5, 6, 7, 8, 9, 1, 2, 3),
  32.      ( 4, 5, 6, 7, 8, 9, 1, 2, 3, 4),
  33.      ( 5, 6, 7, 8, 9, 1, 2, 3, 4, 5),
  34.      ( 6, 7, 8, 9, 1, 2, 3, 4, 5, 6),
  35.      ( 7, 8, 9, 1, 2, 3, 4, 5, 6, 7),
  36.      ( 8, 9, 1, 2, 3, 4, 5, 6, 7, 8),
  37.      ( 9, 1, 2, 3, 4, 5, 6, 7, 8, 9));
  38. BEGIN check := 0;
  39. WHILE (inputnumber > 0)
  40. DO BEGIN
  41.    { cut off the rightmost digit and use for table lok up }
  42.    check := CastTable [check, (inputnumber MOD 10)];
  43.    inputnumber := inputnumber DIV 10;
  44.    END;
  45. LookupCast9 := check;
  46. END;
  47.  
  48.