Verify ISBN Checksums... 
Author Message
 Verify ISBN Checksums...

(*

    I don't recall if I might have posted this code here before. It
    verifies ISBN (International Standard Book Number) checksums.

    [BTW, if you're in Canada and are looking for an online source
    for computer books and software, check out   http://www.*-*-*.com/
    Their prices are competitive and delivery is free.]

*)
(************************* ISBN.MOD *********************************)
MODULE ISBN;
(*
**  Filename: ISBN.MOD  (Modula-2)
**
**  Purpose:  Verify International Standard Book Number (ISBN)
**            checksum.
**
**  System:   FST Modula-2
**
**  Author:   Gregory S. Vigneault
**             http://www.*-*-*.com/ ~telic
**
**  Edit:     March 12, 1999.
**
*)
FROM InOut IMPORT ReadString, Write, WriteLn, WriteString;

IMPORT ASCII, Strings, System;

TYPE

  UNSIGNED  = CARDINAL;     (* unsigned 16-bit integer *)
  CharSet   = SET OF CHAR;

VAR

  ch        : CHAR;
  cs, j, k  : UNSIGNED;
  isbn      : ARRAY [0..15] OF CHAR;

PROCEDURE Value (chr :CHAR) :UNSIGNED;
  (* return the ordinal value of a character *)
  BEGIN
    RETURN ORD(chr) - ORD('0');
  END Value;

BEGIN (*ISBN*)

  (* prompt for and accept an ISBN... *)
  WriteLn;
  WriteString("Enter an ISBN: ");
  ReadString(isbn);
  WriteLn;

  (* filter out any non-alphanumeric characters... *)
  j := 0;
  k := 0;
  WHILE (j <= HIGH(isbn)) AND (isbn[j] <> ASCII.nul) DO
    ch := CAP(isbn[j]);
    IF ch IN CharSet{'0'..'9','X'} THEN
      isbn[k] := ch;
      INC(k);
    END;
    INC(j);
  END;

  IF k <= HIGH(isbn) THEN  (* terminate string with '\0' *)
    isbn[k] := ASCII.nul;
  END;

  IF Strings.Length(isbn) < 10 THEN
    WriteString("Missing characters!");
    System.Terminate(1);  (* exit(1) *)
  END;

  (* calculate the checksum... *)
  cs := 0;
  FOR k := 1 TO Strings.Length(isbn) - 1 DO
    cs := cs + k * Value(isbn[k-1]);
  END;

  ch := isbn[Strings.Length(isbn) - 1];
  IF ch = 'X' THEN
    k := 10;
  ELSE
    k := Value(ch);
  END;

  IF cs MOD 11 = k THEN
    WriteString("+++ Okay +++");
  ELSE
    WriteString("!!! INVALID !!!");
    Write(ASCII.bel); (*beep*)
  END;

  WriteLn;

END ISBN.
(************************* ISBN.MOD *********************************)
(*
  Greg_

  http://www.*-*-*.com/ ~telic
  March 12, 1999, Toronto, Canada.
*)



Wed, 29 Aug 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. ISBN checksum digit

2. UPC & ISBN Checksums?

3. OO-Testing books and Goldberg isbn?

4. ISBN Number Please

5. ISBN for B.O.N. Book

6. ISBN to EAN Conversion

7. Forth and ISBN/ISSNs

8. Validate ISBN

9. Validating ISBN: Regex question

10. Wanted: ISBN for Intel manuals

11. COBOL Source for ISBN #: 0-13-081513-6

12. ISBN numbers, please help!

 

 
Powered by phpBB® Forum Software