Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/ISBN13-check-digit/Modula-2/isbn13-check-digit.mod2
Normal file
44
Task/ISBN13-check-digit/Modula-2/isbn13-check-digit.mod2
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
MODULE ISBN;
|
||||
FROM InOut IMPORT WriteString, WriteLn;
|
||||
FROM Strings IMPORT Length;
|
||||
|
||||
PROCEDURE validISBN(s: ARRAY OF CHAR): BOOLEAN;
|
||||
VAR total, i, length: CARDINAL;
|
||||
BEGIN
|
||||
total := 0;
|
||||
length := Length(s);
|
||||
IF (length # 14) OR (s[3] # '-') THEN
|
||||
RETURN FALSE;
|
||||
END;
|
||||
FOR i := 0 TO length-1 DO
|
||||
IF i # 3 THEN
|
||||
IF (s[i] < '0') OR (s[i] > '9') THEN
|
||||
RETURN FALSE;
|
||||
ELSIF (i < 3) AND (i MOD 2 = 1) OR (i > 3) AND (i MOD 2 = 0) THEN
|
||||
total := total + 3 * (ORD(s[i]) - ORD('0'));
|
||||
ELSE
|
||||
total := total + ORD(s[i]) - ORD('0');
|
||||
END;
|
||||
END;
|
||||
END;
|
||||
RETURN total MOD 10 = 0;
|
||||
END validISBN;
|
||||
|
||||
PROCEDURE check(s: ARRAY OF CHAR);
|
||||
BEGIN
|
||||
WriteString(s);
|
||||
WriteString(': ');
|
||||
IF validISBN(s) THEN
|
||||
WriteString('good');
|
||||
ELSE
|
||||
WriteString('bad');
|
||||
END;
|
||||
WriteLn;
|
||||
END check;
|
||||
|
||||
BEGIN
|
||||
check('978-1734314502');
|
||||
check('978-1734314509');
|
||||
check('978-1788399081');
|
||||
check('978-1788399083');
|
||||
END ISBN.
|
||||
Loading…
Add table
Add a link
Reference in a new issue