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 >
Wrap
Text File
|
1990-11-29
|
1KB
|
48 lines
FUNCTION IterativeCast9 (inputnumber : INTEGER) : INTEGER;
{ loops from right to left, totaling as it goes.
Keeps looping until the total is less than 10 }
VAR
total : INTEGER;
BEGIN
WHILE (inputnumber >= 10)
DO BEGIN
total := 0;
WHILE (inputnumber > 0)
DO BEGIN
{ cut off the rightmost digit }
total := total + (inputnumber MOD 10);
inputnumber := inputnumber DIV 10;
END;
inputnumber := total;
END;
IterativeCast9 := inputnumber;
END;
FUNCTION LookupCast9 (inputnumber : INTEGER) : INTEGER;
{ scans from right to left, keeping inputnumber running.
Casting is done via the look up table during the scan }
VAR check : INTEGER;
CONST
CastTable : ARRAY [0..9, 0..9] OF INTEGER =
(( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
( 1, 2, 3, 4, 5, 6, 7, 8, 9, 1),
( 2, 3, 4, 5, 6, 7, 8, 9, 1, 2),
( 3, 4, 5, 6, 7, 8, 9, 1, 2, 3),
( 4, 5, 6, 7, 8, 9, 1, 2, 3, 4),
( 5, 6, 7, 8, 9, 1, 2, 3, 4, 5),
( 6, 7, 8, 9, 1, 2, 3, 4, 5, 6),
( 7, 8, 9, 1, 2, 3, 4, 5, 6, 7),
( 8, 9, 1, 2, 3, 4, 5, 6, 7, 8),
( 9, 1, 2, 3, 4, 5, 6, 7, 8, 9));
BEGIN check := 0;
WHILE (inputnumber > 0)
DO BEGIN
{ cut off the rightmost digit and use for table lok up }
check := CastTable [check, (inputnumber MOD 10)];
inputnumber := inputnumber DIV 10;
END;
LookupCast9 := check;
END;