Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/ISBN13-check-digit/Java/isbn13-check-digit-1.java
Normal file
38
Task/ISBN13-check-digit/Java/isbn13-check-digit-1.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
public static void main(String[] args) {
|
||||
String[] isbn13s = {
|
||||
"978-0596528126",
|
||||
"978-0596528120",
|
||||
"978-1788399081",
|
||||
"978-1788399083"
|
||||
};
|
||||
for (String isbn13 : isbn13s)
|
||||
System.out.printf("%s %b%n", isbn13, validateISBN13(isbn13));
|
||||
}
|
||||
|
||||
static boolean validateISBN13(String string) {
|
||||
int[] digits = digits(string.strip().replace("-", ""));
|
||||
return digits[12] == checksum(digits);
|
||||
}
|
||||
|
||||
static int[] digits(String string) {
|
||||
int[] digits = new int[13];
|
||||
int index = 0;
|
||||
for (char character : string.toCharArray()) {
|
||||
if (character < '0' || character > '9')
|
||||
throw new IllegalArgumentException("Invalid ISBN-13");
|
||||
/* convert ascii to integer */
|
||||
digits[index++] = Character.digit(character, 10);
|
||||
}
|
||||
return digits;
|
||||
}
|
||||
|
||||
static int checksum(int[] digits) {
|
||||
int total = 0;
|
||||
int index = 0;
|
||||
for (int digit : digits) {
|
||||
if (index == 12) break;
|
||||
if (index++ % 2 == 1) digit *= 3;
|
||||
total += digit;
|
||||
}
|
||||
return 10 - (total % 10);
|
||||
}
|
||||
18
Task/ISBN13-check-digit/Java/isbn13-check-digit-2.java
Normal file
18
Task/ISBN13-check-digit/Java/isbn13-check-digit-2.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public static void main(){
|
||||
System.out.println(isISBN13("978-1734314502"));
|
||||
System.out.println(isISBN13("978-1734314509"));
|
||||
System.out.println(isISBN13("978-1788399081"));
|
||||
System.out.println(isISBN13("978-1788399083"));
|
||||
}
|
||||
public static boolean isISBN13(String in){
|
||||
int pre = Integer.parseInt(in.substring(0,3));
|
||||
if (pre!=978)return false;
|
||||
String postStr = in.substring(4);
|
||||
if (postStr.length()!=10)return false;
|
||||
int post = Integer.parseInt(postStr);
|
||||
int sum = 38;
|
||||
for(int x = 0; x<10;x+=2)
|
||||
sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48));
|
||||
if(sum%10==0) return true;
|
||||
return false;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue