Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,67 +1,67 @@
org 100h
jmp demo
;;; ---------------------------------------------------------------
;;; Check if the string at BC is a valid ISBN-13 code.
;;; Carry set if true, clear if not.
isbn13: lxi h,0 ; HL = accumulator
mov d,h ; D = 0 (such that if E=A, DE=A).
call isbngc ; Get first character
rnc ; Carry clear = invalid
dad d ; Add to running total once
call isbngc ; Get second character
rnc ; Carry clear = invalid
dad d ; Add to running total thrice
dad d
dad d
call isbngc ; Get third character
rnc ; Carry clear = invalid
dad d ; Add to running total once
ldax b ; Fourth character should be a dash '-'
inx b
cpi '-'
stc ; Clear carry w/o touching other flags
cmc
rnz ; If not equal, invalid.
push h ; Keep loop counter on stack
mvi l,5 ; 5 times 2 characters
isbnlp: xthl ; Accumulator in HL
call isbngc ; Get even character
jnc isbnex ; If invalid, stop
dad d ; Add to running total thrice
dad d
dad d
call isbngc ; Get odd character
jnc isbnex ; If invalid, stop
dad d ; Add to running total once
xthl ; Loop counter in (H)L
dcr l ; Done yet?
jnz isbnlp ; If not, do next two characters
pop h ; Get accumulator
lxi d,-10 ; Trial division by ten
isbndv: dad d ; Subtract 10
jc isbndv ; Until zero passed
mov a,l ; Move low byte to A
adi 10 ; Add ten back (the mod loop went one step too far)
rz ; If zero, return (carry will have been set)
ana a ; Otherwise, make sure carry is clear
ret ; And then return
isbnex: pop h ; Test failed - throw away accumulator and return
ret
isbngc: ldax b ; Get character from [BC]
inx b ; Increment BC
sui '0' ; Subtract ASCII '0' to get digit value
cpi 10 ; If 10 or higher (unsigned), invalid digit.
mov e,a ; Set (D)E = value
ret
;;; ---------------------------------------------------------------
;;; Demo: see if the CP/M command line contains a valid ISBN13
;;; code.
demo: lxi b,82h ; Start of command line argument, skipping first space
call isbn13 ; Is it valid?
mvi c,9 ; CP/M print string
lxi d,good ; If carry is set, then yes
jc 5
lxi d,bad ; Otherwise, no.
jmp 5
good: db 'good$'
bad: db 'bad$'
org 100h
jmp demo
;;; ---------------------------------------------------------------
;;; Check if the string at BC is a valid ISBN-13 code.
;;; Carry set if true, clear if not.
isbn13: lxi h,0 ; HL = accumulator
mov d,h ; D = 0 (such that if E=A, DE=A).
call isbngc ; Get first character
rnc ; Carry clear = invalid
dad d ; Add to running total once
call isbngc ; Get second character
rnc ; Carry clear = invalid
dad d ; Add to running total thrice
dad d
dad d
call isbngc ; Get third character
rnc ; Carry clear = invalid
dad d ; Add to running total once
ldax b ; Fourth character should be a dash '-'
inx b
cpi '-'
stc ; Clear carry w/o touching other flags
cmc
rnz ; If not equal, invalid.
push h ; Keep loop counter on stack
mvi l,5 ; 5 times 2 characters
isbnlp: xthl ; Accumulator in HL
call isbngc ; Get even character
jnc isbnex ; If invalid, stop
dad d ; Add to running total thrice
dad d
dad d
call isbngc ; Get odd character
jnc isbnex ; If invalid, stop
dad d ; Add to running total once
xthl ; Loop counter in (H)L
dcr l ; Done yet?
jnz isbnlp ; If not, do next two characters
pop h ; Get accumulator
lxi d,-10 ; Trial division by ten
isbndv: dad d ; Subtract 10
jc isbndv ; Until zero passed
mov a,l ; Move low byte to A
adi 10 ; Add ten back (the mod loop went one step too far)
rz ; If zero, return (carry will have been set)
ana a ; Otherwise, make sure carry is clear
ret ; And then return
isbnex: pop h ; Test failed - throw away accumulator and return
ret
isbngc: ldax b ; Get character from [BC]
inx b ; Increment BC
sui '0' ; Subtract ASCII '0' to get digit value
cpi 10 ; If 10 or higher (unsigned), invalid digit.
mov e,a ; Set (D)E = value
ret
;;; ---------------------------------------------------------------
;;; Demo: see if the CP/M command line contains a valid ISBN13
;;; code.
demo: lxi b,82h ; Start of command line argument, skipping first space
call isbn13 ; Is it valid?
mvi c,9 ; CP/M print string
lxi d,good ; If carry is set, then yes
jc 5
lxi d,bad ; Otherwise, no.
jmp 5
good: db 'good$'
bad: db 'bad$'

View file

@ -1,54 +1,54 @@
cpu 8086
bits 16
org 100h
section .text
jmp demo
isbn13: ;;; ---------------------------------------------------------------
;;; Check if the string at DS:SI is a valid ISBN-13 code.
;;; Carry set if true, clear if false.
xor dx,dx ; DX = running total
xor ah,ah ; Set AH=0 so that AX=AL
call .digit ; Get first digit and add to DX
call .digit ; Get second digit and add to DX
add dx,ax ; Add to DX twice more
add dx,ax
call .digit ; Get third digit and add to DX
lodsb ; Fourth character should be a '-'
cmp al,'-'
jne .fail ; If not equal, fail
mov cx,5 ; Then loop 5 times for the next 10 digits
.loop: call .digit ; Get even digit and add to DX
add dx,ax ; Add to running total twice more
add dx,ax
call .digit ; Get odd digit and add to DX
loop .loop ; Do this 5 times
mov ax,dx ; Divide running total by 10
mov dl,10
div dl
test ah,ah ; Is the remainder zero?
jnz .out ; If not, stop (TEST clears carry)
stc ; Otherwise, set carry and return
ret
.digit: lodsb ; Get first character
sub al,'0' ; Subtract ASCII 0 to get digit value
cmp al,9
ja .dfail
add dx,ax ; Add to ASCII
ret
.dfail: pop dx ; Remove return pointer for .digit from stack
.fail: clc ; Failure - clear carry
.out: ret
;;; ---------------------------------------------------------------
;;; Demo: see if the MS-DOS command line contains a valid ISBN13
;;; code.
demo: mov si,82h ; Start of command line argument skipping space
call isbn13 ; Is it valid?
mov ah,9 ; MS-DOS print string
mov dx,good ; If carry is set, it is good
jc .print
mov dx,bad ; Otherwise, it is bad
.print: int 21h
ret
section .data
good: db 'good$'
bad: db 'bad$'
cpu 8086
bits 16
org 100h
section .text
jmp demo
isbn13: ;;; ---------------------------------------------------------------
;;; Check if the string at DS:SI is a valid ISBN-13 code.
;;; Carry set if true, clear if false.
xor dx,dx ; DX = running total
xor ah,ah ; Set AH=0 so that AX=AL
call .digit ; Get first digit and add to DX
call .digit ; Get second digit and add to DX
add dx,ax ; Add to DX twice more
add dx,ax
call .digit ; Get third digit and add to DX
lodsb ; Fourth character should be a '-'
cmp al,'-'
jne .fail ; If not equal, fail
mov cx,5 ; Then loop 5 times for the next 10 digits
.loop: call .digit ; Get even digit and add to DX
add dx,ax ; Add to running total twice more
add dx,ax
call .digit ; Get odd digit and add to DX
loop .loop ; Do this 5 times
mov ax,dx ; Divide running total by 10
mov dl,10
div dl
test ah,ah ; Is the remainder zero?
jnz .out ; If not, stop (TEST clears carry)
stc ; Otherwise, set carry and return
ret
.digit: lodsb ; Get first character
sub al,'0' ; Subtract ASCII 0 to get digit value
cmp al,9
ja .dfail
add dx,ax ; Add to ASCII
ret
.dfail: pop dx ; Remove return pointer for .digit from stack
.fail: clc ; Failure - clear carry
.out: ret
;;; ---------------------------------------------------------------
;;; Demo: see if the MS-DOS command line contains a valid ISBN13
;;; code.
demo: mov si,82h ; Start of command line argument skipping space
call isbn13 ; Is it valid?
mov ah,9 ; MS-DOS print string
mov dx,good ; If carry is set, it is good
jc .print
mov dx,bad ; Otherwise, it is bad
.print: int 21h
ret
section .data
good: db 'good$'
bad: db 'bad$'

View file

@ -1,37 +0,0 @@
with Ada.Text_IO;
procedure ISBN_Check is
function Is_Valid (ISBN : String) return Boolean is
Odd : Boolean := True;
Sum : Integer := 0;
Value : Integer;
begin
for I in ISBN'Range loop
if ISBN (I) in '0' .. '9' then
Value := Character'Pos (ISBN (I)) - Character'Pos ('0');
if Odd then
Sum := Sum + Value;
else
Sum := Sum + 3 * Value;
end if;
Odd := not Odd;
end if;
end loop;
return Sum mod 10 = 0;
end Is_Valid;
procedure Show (ISBN : String) is
use Ada.Text_IO;
Valid : constant Boolean := Is_Valid (ISBN);
begin
Put (ISBN); Put (" ");
Put ((if Valid then "Good" else "Bad"));
New_Line;
end Show;
begin
Show ("978-0596528126");
Show ("978-0596528120");
Show ("978-1788399081");
Show ("978-1788399083");
end ISBN_Check;

View file

@ -4,15 +4,15 @@ validISBN?: function [isbn][
s: 0
loop.with:'i isbn 'n [
if? even? i -> s: s + n
else -> s: s + 3*n
switch even? i -> s: s + n
-> s: s + 3*n
]
checkDigit: 10 - s % 10
return currentCheck = checkDigit
]
tests: [
"978-0596528126" "978-0596528120"
"978-1734314502" "978-1734314509"
"978-1788399081" "978-1788399083"
]

View file

@ -1,5 +1,5 @@
ISBN13_check_digit(n){
for i, v in StrSplit(RegExReplace(n, "[^0-9]"))
sum += !Mod(i, 2) ? v*3 : v
return n "`t" (Mod(sum, 10) ? "(bad)" : "(good)")
for i, v in StrSplit(RegExReplace(n, "[^0-9]"))
sum += !Mod(i, 2) ? v*3 : v
return n "`t" (Mod(sum, 10) ? "(bad)" : "(good)")
}

View file

@ -1,6 +1,6 @@
output := ""
nums := ["978-0596528126","978-0596528120","978-1788399081","978-1788399083"]
for i, n in nums
output .= ISBN13_check_digit(n) "`n"
output .= ISBN13_check_digit(n) "`n"
MsgBox % output
return

View file

@ -2,21 +2,21 @@ arraybase 1
dim isbn = {"978-0596528126", "978-0596528120", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"}
for n = 1 to isbn[?]
sum = 0
isbnStr = isbn[n]
isbnStr = replace(isbnStr, "-", "")
isbnStr = replace(isbnStr, " ", "")
for m = 1 to length(isbnStr)
if m mod 2 = 0 then
num = 3 * int(mid(isbnStr, m, 1))
else
num = int(mid(isbnStr, m, 1))
end if
sum += num
next m
if sum mod 10 = 0 then
print isbn[n]; ": good"
else
print isbn[n]; ": bad"
end if
sum = 0
isbnStr = isbn[n]
isbnStr = replace(isbnStr, "-", "")
isbnStr = replace(isbnStr, " ", "")
for m = 1 to length(isbnStr)
if m mod 2 = 0 then
num = 3 * int(mid(isbnStr, m, 1))
else
num = int(mid(isbnStr, m, 1))
end if
sum += num
next m
if sum mod 10 = 0 then
print isbn[n]; ": good"
else
print isbn[n]; ": bad"
end if
next n

View file

@ -1,139 +0,0 @@
******************************************************************
* Author: Jay Moseley
* Date: November 10, 2019
* Purpose: Testing various subprograms/ functions.
* Tectonics: cobc -xj testSubs.cbl
******************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. testSubs.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC
FUNCTION validISBN13.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 IX PIC S9(4) COMP.
01 TEST-ISBNS.
02 FILLER PIC X(14) VALUE '978-0596528126'.
02 FILLER PIC X(14) VALUE '978-0596528120'.
02 FILLER PIC X(14) VALUE '978-1788399081'.
02 FILLER PIC X(14) VALUE '978-1788399083'.
01 TEST-ISBN REDEFINES TEST-ISBNS
OCCURS 4 TIMES
PIC X(14).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM
VARYING IX
FROM 1
BY 1
UNTIL IX > 4
DISPLAY TEST-ISBN (IX) ' ' WITH NO ADVANCING
END-DISPLAY
IF validISBN13(TEST-ISBN (IX)) = -1
DISPLAY '(bad)'
ELSE
DISPLAY '(good)'
END-IF
END-PERFORM.
GOBACK.
END PROGRAM testSubs.
******************************************************************
* Author: Jay Moseley
* Date: May 19, 2016
* Purpose: validate ISBN-13 (International Standard
* Book Number).
******************************************************************
IDENTIFICATION DIVISION.
FUNCTION-ID. validISBN13.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 PASSED-SIZE PIC S9(6) COMP-5.
01 IX PIC S9(4) COMP.
01 WORK-FIELDS.
02 WF-DIGIT PIC X.
02 WF-COUNT PIC 9(2).
88 WEIGHT-1 VALUE 1, 3, 5, 7, 9, 11, 13.
88 WEIGHT-3 VALUE 2, 4, 6, 8, 10, 12.
02 WF-SUM PIC S9(8) COMP.
LINKAGE SECTION.
01 PASSED-ISBN PIC X ANY LENGTH.
01 RETURN-VALUE PIC S9.
PROCEDURE DIVISION USING PASSED-ISBN
RETURNING RETURN-VALUE.
CALL 'C$PARAMSIZE'
USING 1
GIVING PASSED-SIZE
END-CALL.
COMPUTE-CKDIGIT.
INITIALIZE WORK-FIELDS.
PERFORM
VARYING IX
FROM 1
BY 1
UNTIL IX GREATER THAN PASSED-SIZE
MOVE PASSED-ISBN (IX:1) TO WF-DIGIT
IF WF-DIGIT IS NUMERIC
ADD 1 TO WF-COUNT
IF WEIGHT-1
ADD NUMVAL(WF-DIGIT) TO WF-SUM
ELSE
COMPUTE WF-SUM = WF-SUM +
(NUMVAL(WF-DIGIT) * 3)
END-COMPUTE
END-IF
END-IF
END-PERFORM.
IF MOD(WF-SUM, 10) = 0
MOVE +0 TO RETURN-VALUE
ELSE
MOVE -1 TO RETURN-VALUE
END-IF.
GOBACK.
* - - - - - - - - - - - - - - - - - - - - - - PROGRAM EXIT POINT
END FUNCTION validISBN13.

View file

@ -6,13 +6,13 @@ Sum:=0;
{Go througha chars, ignoring non-digits}
for I:=1 to Length(ISBN) do
if ISBN[I] in ['0'..'9'] then
begin
N:=StrToInt(ISBN[I]);
{Every other digit multiplied by 3}
if (I and 1)=1 then N:=N*3;
{Sum digits}
Sum:=Sum+N;
end;
begin
N:=StrToInt(ISBN[I]);
{Every other digit multiplied by 3}
if (I and 1)=1 then N:=N*3;
{Sum digits}
Sum:=Sum+N;
end;
{The sum must be an even multiple of 10}
Result:=(Sum mod 10)=0;
end;
@ -30,8 +30,8 @@ end;
procedure TestISBNSet(Memo: TMemo);
{Test supplied set of ISBN numbers}
begin
ValidateAndShow(Memo,'978-0596528126'); //(good)
ValidateAndShow(Memo,'978-0596528120'); //(bad)
ValidateAndShow(Memo,'978-1788399081'); //(good)
ValidateAndShow(Memo,'978-1788399083'); //(bad)
ValidateAndShow(Memo,'978-0596528126'); //(good)
ValidateAndShow(Memo,'978-0596528120'); //(bad)
ValidateAndShow(Memo,'978-1788399081'); //(good)
ValidateAndShow(Memo,'978-1788399083'); //(bad)
end;

View file

@ -1,24 +1,18 @@
func ISBN13check isbn$ .
for c$ in strchars isbn$
if c$ <> "-"
ndigs += 1
.
for c$ in strchars isbn$ : if c$ <> "-"
ndigs += 1
dig = number c$
if ndigs mod 2 = 0
dig *= 3
.
if ndigs mod 2 = 0 : dig *= 3
sum += dig
.
if sum mod 10 <> 0
return 0
.
if sum mod 10 <> 0 : return 0
return 1
.
codes$[] = [ "978-0596528126" "978-0596528120" "978-1788399081" "978-1788399083" ]
for code$ in codes$[]
if ISBN13check code$ = 1
print code$ & " is a valid ISBN"
print code$ & ": good"
else
print code$ & " is not a valid ISBN"
print code$ & ": bad"
.
.

View file

@ -1,31 +1,31 @@
Module CheckISBN (filename as string, feed as array) {
if len(feed)=0 then exit
link feed to isbn() ' just change interface
open filename for output as #f ' use "" for output to screen (f=-2)
for n = 0 to len(feed)-1
long sum = 0, k = 0
string isbnStr = filter$(isbn(n), "- ")
if len(isbnStr)=0 then continue
for m = 1 to len(isbnStr)
if m mod 2 = 0 then
num = 3 * val(mid$(isbnStr, m, 1))
else
num = val(mid$(isbnStr, m, 1))
end if
sum += num
k++
next m
print #f, isbn(n);
if k<>13 then
print #f, ": not an ISBN 13"
else.if sum mod 10 = 0 then
print #f, ": good"
else
print #f, ": bad (last digit should be:"+((10-(sum-num) mod 10) mod 10)+")"
end if
next n
close #f
if len(feed)=0 then exit
link feed to isbn() ' just change interface
open filename for output as #f ' use "" for output to screen (f=-2)
for n = 0 to len(feed)-1
long sum = 0, k = 0
string isbnStr = filter$(isbn(n), "- ")
if len(isbnStr)=0 then continue
for m = 1 to len(isbnStr)
if m mod 2 = 0 then
num = 3 * val(mid$(isbnStr, m, 1))
else
num = val(mid$(isbnStr, m, 1))
end if
sum += num
k++
next m
print #f, isbn(n);
if k<>13 then
print #f, ": not an ISBN 13"
else.if sum mod 10 = 0 then
print #f, ": good"
else
print #f, ": bad (last digit should be:"+((10-(sum-num) mod 10) mod 10)+")"
end if
next n
close #f
}
Flush
Data "978-0596528126", "978-0596528120", "978-1788399081"

View file

@ -1,16 +1,16 @@
isISBN13 = function(n)
n = n.replace("-","").replace(" ","")
s = 0
for i in range(0, n.len-1,2)
s += n[i].val
end for
for i in range(1, n.len-1,2)
s += n[i].val * 3
end for
return not (s % 10)
n = n.replace("-","").replace(" ","")
s = 0
for i in range(0, n.len-1,2)
s += n[i].val
end for
for i in range(1, n.len-1,2)
s += n[i].val * 3
end for
return not (s % 10)
end function
testValues = "978-0596528126 978-0596528120 978-1788399081 978-1788399083".split(" ")
for val in testValues
print val + " " + ["bad", "good"][isISBN13(val)]
print val + " " + ["bad", "good"][isISBN13(val)]
end for

View file

@ -1,85 +1,85 @@
program ISBNChecksum(output);
const
codeIndexMaximum = 17;
ISBNIndexMinimum = 1;
ISBNIndexMaximum = 13;
ISBNIndexRange = ISBNIndexMaximum - ISBNIndexMinimum + 1;
codeIndexMaximum = 17;
ISBNIndexMinimum = 1;
ISBNIndexMaximum = 13;
ISBNIndexRange = ISBNIndexMaximum - ISBNIndexMinimum + 1;
type
code = string(codeIndexMaximum);
codeIndex = 1..codeIndexMaximum value 1;
decimalDigit = '0'..'9';
decimalValue = 0..9;
ISBNIndex = ISBNIndexMinimum..ISBNIndexMaximum;
ISBN = array[ISBNIndex] of decimalDigit;
code = string(codeIndexMaximum);
codeIndex = 1..codeIndexMaximum value 1;
decimalDigit = '0'..'9';
decimalValue = 0..9;
ISBNIndex = ISBNIndexMinimum..ISBNIndexMaximum;
ISBN = array[ISBNIndex] of decimalDigit;
{ returns the integer value represented by a character }
function numericValue(protected c: decimalDigit): decimalValue;
begin
{ in Pascal result variable bears the same name as the function }
numericValue := ord(c) - ord('0')
{ in Pascal result variable bears the same name as the function }
numericValue := ord(c) - ord('0')
end;
{ determines whether an ISBN is technically valid (checksum correct) }
function isValidISBN(protected n: ISBN): Boolean;
var
sum: 0..225 value 0;
i: ISBNIndex;
sum: 0..225 value 0;
i: ISBNIndex;
begin
{ NB: in Pascal for-loop-limits are _inclusive_ }
for i := ISBNIndexMinimum to ISBNIndexMaximum do
begin
{ alternating scale factor 3^0, 3^1 based on Boolean }
sum := sum + numericValue(n[i]) * 3 pow ord(not odd(i))
end;
isValidISBN := sum mod 10 = 0
{ NB: in Pascal for-loop-limits are _inclusive_ }
for i := ISBNIndexMinimum to ISBNIndexMaximum do
begin
{ alternating scale factor 3^0, 3^1 based on Boolean }
sum := sum + numericValue(n[i]) * 3 pow ord(not odd(i))
end;
isValidISBN := sum mod 10 = 0
end;
{ transform '978-0-387-97649-5' into '9780387976495' }
function digits(n: code): code;
var
sourceIndex, destinationIndex: codeIndex;
sourceIndex, destinationIndex: codeIndex;
begin
for sourceIndex := 1 to length(n) do
begin
if n[sourceIndex] in ['0'..'9'] then
begin
n[destinationIndex] := n[sourceIndex];
destinationIndex := destinationIndex + 1
end
end;
{ to alter a strings length you need overwrite it completely }
digits := subStr(n, 1, destinationIndex - 1)
for sourceIndex := 1 to length(n) do
begin
if n[sourceIndex] in ['0'..'9'] then
begin
n[destinationIndex] := n[sourceIndex];
destinationIndex := destinationIndex + 1
end
end;
{ to alter a strings length you need overwrite it completely }
digits := subStr(n, 1, destinationIndex - 1)
end;
{ data type coercion }
function asISBN(protected n: code): ISBN;
var
result: ISBN;
result: ISBN;
begin
unpack(n[1..length(n)], result, 1);
asISBN := result
unpack(n[1..length(n)], result, 1);
asISBN := result
end;
{ tells whether a string value contains a proper ISBN representation }
function isValidISBNString(protected hyphenatedForm: code): Boolean;
var
digitOnlyForm: code;
digitOnlyForm: code;
begin
digitOnlyForm := digits(hyphenatedForm);
{ The Extended Pascal `and_then` Boolean operator allows us }
{ to first check the length before invoking `isValidISBN`. }
isValidISBNString := (length(digitOnlyForm) = ISBNIndexRange)
and_then isValidISBN(asISBN(digitOnlyForm))
digitOnlyForm := digits(hyphenatedForm);
{ The Extended Pascal `and_then` Boolean operator allows us }
{ to first check the length before invoking `isValidISBN`. }
isValidISBNString := (length(digitOnlyForm) = ISBNIndexRange)
and_then isValidISBN(asISBN(digitOnlyForm))
end;
{ === MAIN ============================================================= }
begin
writeLn(isValidISBNString('978-0596528126'));
writeLn(isValidISBNString('978-0596528120'));
writeLn(isValidISBNString('978-1788399081'));
writeLn(isValidISBNString('978-1788399083'))
writeLn(isValidISBNString('978-0596528126'));
writeLn(isValidISBNString('978-0596528120'));
writeLn(isValidISBNString('978-1788399081'));
writeLn(isValidISBNString('978-1788399083'))
end.

View file

@ -1,23 +1,14 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">check_isbn13</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">isbn</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">checksum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">isbn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">isbn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">-=</span> <span style="color: #008000;">'0'</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">></span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span> <span style="color: #000000;">checksum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">9</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">checksum</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">*</span><span style="color: #000000;">w</span>
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">-</span><span style="color: #000000;">w</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">checksum</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">checksum</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">gb</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">=</span><span style="color: #000000;">13</span> <span style="color: #008080;">and</span> <span style="color: #000000;">checksum</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #0000FF;">?</span> <span style="color: #008000;">"good"</span> <span style="color: #0000FF;">:</span> <span style="color: #008000;">"bad"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">isbn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">gb</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
with javascript_semantics
procedure check_isbn13(string isbn)
integer digits = 0, checksum = 0
for ch in filter(isbn,"out"," -") do
if ch<'0' or ch>'9' then checksum = 9 exit end if
checksum += (ch-'0')*iff(even(digits)?1:3)
digits += 1
end for
printf(1,"%s: %s\n",{isbn,iff(digits=13 and rmdr(checksum,10)=0 ? "good" : "bad")})
end procedure
<span style="color: #008080;">constant</span> <span style="color: #000000;">isbns</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"978-0596528126"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"978-0596528120"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"978-1788399081"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"978-1788399083"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"978-2-74839-908-0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"978-2-74839-908-5"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"978 1 86197 876 9"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">isbns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000000;">check_isbn13</span><span style="color: #0000FF;">(</span><span style="color: #000000;">isbns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--
constant isbns = {"978-0596528126", "978-0596528120", "978-1788399081", "978-1788399083",
"978-2-74839-908-0","978-2-74839-908-5","978 1 86197 876 9"}
for isbn in isbns do check_isbn13(isbn) end for

View file

@ -1,38 +0,0 @@
function Get-ISBN13 {
$codes = (
"978-0596528126",
"978-0596528120",
"978-1788399081",
"978-1788399083"
)
foreach ($line in $codes) {
$sum = $null
$codeNoDash = $line.Replace("-","")
for ($i = 0; $i -lt $codeNoDash.length; $i++) {
if (($i % 2) -eq 1) {
$sum += [decimal]$codeNoDash[$i] * 3
}else {
$sum += [decimal]$codeNoDash[$i]
}
}
if (($sum % 10) -eq 0) {
Write-Host "$line Good"
}else {
Write-Host "$line Bad"
}
}
}
Get-ISBN13