The algorithm works as follows:
- Each of the first 12 digits starting from the left to right, is multiplied by 1 or 3 alternatively.
- The sum of the products modulo 10 gives us either zero or a number between 1 to 9.
- Subtract the number from 10 and it gives us the checksum.
REM ISBN-13 Validator program
REM T Street
REM 2015-03-19
INSTALL @lib$+"stringlib"
REM main program loop
REPEAT
PRINT '"ISBN-13 validator."
PRINT "------------------"'
REPEAT
INPUT "Enter a 13 digit ISBN number : " isbn$
isbn$ = FN_convertInput( isbn$ )
UNTIL LEN(isbn$)=13
IF FN_validISBN13( isbn$ ) THEN
PRINT "VALID ISBN-13 - opening your browser..."
SYS "ShellExecute",@hwnd%,0,"https://www.google.co.uk/search?q=ISBN "+isbn$, 0, "", 1
ELSE
PRINT "INVALID ISBN-13 :-("
ENDIF
UNTIL FALSE
DEFFN_validISBN13( num$ )
REM validates an isbn number
REM pass a 13 digit string as parameter
REM returns true if the string represents a valid ISBN-13
REM
REM Each digit, starting from the left to right, is multiplied by 1 or 3 alternatively.
REM The sum of the products modulo 10 gives us either zero or a number between 1 to 9.
REM Subtract the number from 10 and it gives us the checksum.
LOCAL i% : REM iterator
LOCAL calcCheck% : REM running calculation
LOCAL checksum% : REM checksum
LOCAL multiplier% : multiplier% = 3
i% = 1
WHILE i%<13
calcCheck% = calcCheck% + VAL(MID$(num$, i%, 1))
i% += 1
calcCheck% = calcCheck% + VAL(MID$(num$, i%, 1)) * multiplier%
i% += 1
ENDWHILE
calcCheck% = calcCheck% MOD 10
checksum% = 10 - calcCheck%
IF checksum% = VAL(RIGHT$(num$,1)) THEN
= TRUE
ENDIF
= FALSE
DEFFN_convertInput( astring$ )
REM pass an ISBN-13 number as a string
REM removes any spaces or dashes
REM which are sometimes located
REM in ISBN numbers
REM returns the string with dash/space removed
LOCAL a$ : a$ = astring$
IF FN_findreplacei( a$, "-", "", 0)
IF FN_findreplacei( a$, " ", "", 0)
= a$
#isbn #bbcbasicforwindows #algoithms