ISBN/EAN (Zeichenketten)
Schreiben Sie eine Methode, welche die Prüfziffern von ISBN (Internationale Standardbuchnummer) verifizieren kann. Dabei wird ein ISBN ohne die Prüfziffer (als String) eingegeben, von welcher die Prüfziffer als Rückgabewert ausgegeben wird. Prototyp:
berechnePruefziffer(isbnOhnePruefziffer: string): integer
Daraus leiten Sie eine weitere Methode ab, die von einer Zeichenkette, diesmal inklusive Prüfziffer, ausgibt, ob die Prüfziffer korrekt ist (true/false):
pruefePruefziffer(isbnMitPruefziffer: string): boolean
Der Algorithmus zur ISBN-10-Prüfziffer ist wie folgt:
- Alle Trennzeichen (Leerschläge und Bindestriche) sind zu entfernen.
Es bleiben neun Ziffern (z1, z2, z3, ... , z9). - s := z1 + 2*z2 + 3*z3 + 4*z4 + … + 9*z9
- p := s MODULO 11
- Falls 10 = p: Prüfziffer := "X", ansonsten: Prüfziffer := p
Bemerkung: ISBN Nummern haben den selben Aufbau wie die «European Article Number»: EAN.
Zusatzaufgabe: Informationen zur Berechnung der Prüfziffer zur ISBN-13 (bzw. EAN-13) finden Sie im Internet. Programmieren Sie zusätzlich eine Prüfung zum ISBN-13.
0 Kommentare
6 Lösung(en)
public class Main {
public static void main(String[] args) {
String str = "3-86680-193";
String str2 = str + "-0";
System.out.println( str );
System.out.println( checksum10( str ) );
System.out.println( str2 + " : " + checksum10valid( str2 ) );
}
public static char checksum10( String str ) {
int i, sum = 0, count = 0;
char c;
for( i = 0; i < str.length(); i++ ) {
c = str.charAt( i );
if ( !Character.isDigit( c ) )
continue;
count++;
if ( count > 9 )
break;
sum += count * Character.getNumericValue( c );
}
if ( ( i < str.length( )-1 ) || ( count != 9 ) )
System.out.println( "Error" );
sum %= 11;
return( sum == 10 ? 'X' : Character.forDigit( sum, 10 ) );
}
public static boolean checksum10valid( String str ) {
char c = str.charAt( str.length()-1 );
return( c == checksum10( str.substring( 0, str.length()-1 ) ) );
}
public static char checksum13( String str ) {
int i, sum = 0, count = 0;
char c;
for( i = 0; i < str.length(); i++ ) {
c = str.charAt( i );
if ( !Character.isDigit( c ) )
continue;
count++;
if ( count > 12 )
break;
if ( count % 2 == 0 )
sum += 3 * Character.getNumericValue( c );
else
sum += Character.getNumericValue( c );
}
if ( ( i < str.length( )-1 ) || ( count != 12 ) )
System.out.println( "Error" );
sum = ( 10 - sum%10 ) % 10;
return( Character.forDigit( sum, 10 ) );
}
public static boolean checksum13valid( String str ) {
char c = str.charAt( str.length()-1 );
return( c == checksum13( str.substring( 0, str.length()-1 ) ) );
}
}
def checkISBN10(s):
l = s.split('-')
ss = ''.join(l)
print ss
sum =0
for i in range (len(ss)):
sum+=(i+1)*int(ss[i])
p = sum %11
if p == 10:
return 'x'
return str(p)
def checkISBN13(s):
l = s.split('-')
ss = ''.join(l)
print ss
sum =0
for i in range (6):
sum+=int(ss[i])
sum+=3*int(ss[i])
p = sum %10
return str(p)
print checkISBN10('0-13-110362')
print checkISBN10('3-86680-193')
print checkISBN10('3-446-13878')
print checkISBN13('978-1-56619-909-4')
char berechnePruefziffer(String isbn10OhnePruefziffer) {
int sum = 0;
int factor = 1;
for(char c : isbn10OhnePruefziffer.toCharArray()) {
int ziffernWert = c - '0';
sum = sum + factor * ziffernWert;
factor = factor + 1;
}
int p = sum % 11;
if(10 == p) {
return 'X';
} else {
char ziffer = (char) (p + '0');
return ziffer;
}
}
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
function isbnChecksum(input) {
input = input.replace(/[ -]/g, '').trim().split('');
for (let i in input) input[i] = parseInt(input[i]);
switch (input.length) {
case 9 : return getCs10();
case 10: return checkCs10();
case 12: return getCs13();
case 13: return checkCs13();
}
function getCs10() {
let c = 0;
for (i = 0; i <= 8; i++) c += (i+1) * input[i];
c = c % 11;
return (c == 10) ? 'X' : c;
}
function checkCs10() {
return input[input.length-1] == getCs10();
}
function getCs13() {
let c = 0;
for (let i = 1; i <= 11; i = i+2) c += input[i];
c *= 3;
for (i = 0; i <= 10; i += 2) c += input[i];
return (10-(c % 10)) % 10;
}
function checkCs13() {
return input[input.length-1] == getCs13();
}
}
// ausgabe
console.log( isbnChecksum("0-13-110362") );
console.log( isbnChecksum("3-89864-117-1") );
console.log( isbnChecksum("978-3-86680-192") );
console.log( isbnChecksum("978-3-86680-192-9") );
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
using System.Text.RegularExpressions;
static bool IsIsbn(string s) {
s = s.Replace("-", "").Replace(" ", "").ToUpper();
if (new Regex(@"\b\d{9}[0-9X]\b").IsMatch(s)) {
var i = 10;
var r = s.Select(x => x is 'X' ? 10 : x - '0');
return (r.Take(9).Select(x => x * i--).Sum() + r.TakeLast(1).FirstOrDefault()) % 11 == 0;
}
if (new Regex(@"\b\d{13}\b").IsMatch(s)) {
var sum = Enumerable.Range(0, s.Length - 1).Select(x => (s[x] - '0') * Math.Pow(3, x % 2)).Sum();
return (int)Math.Ceiling(sum / 10) * 10 - sum == s[^1] - '0';
}
return false;
}
Console.WriteLine(IsIsbn("3-8931-9301-4"));
Console.WriteLine(IsIsbn("40 03 30 10 18 39 8"));
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <vector>
#include <algorithm>
enum class ISBN_Type { isbn_10 = 8, isbn_13 = 11 };
struct ISBN { std::string num; ISBN_Type typ; };
const auto get_isbn_check_num(const ISBN& isbn) {
auto sum{ 0 }, counter{ 0 };
for (size_t i{ 0 }; i < isbn.num.length(); ++i) {
if (counter > int(isbn.typ)) return -1;
if (std::isdigit(isbn.num[i]))
sum += (isbn.num[i] - '0') * (isbn.typ == ISBN_Type::isbn_10 ? ++counter : pow(3, counter++ % 2));
}
return (isbn.typ == ISBN_Type::isbn_10 ? (sum % 11 == 10 ? 'X' : sum % 11) : ((sum + 9) / 10) * 10 - sum);
}
const auto is_isbn_num(const ISBN& isbn) {
const auto cn{ std::find_if(isbn.num.rbegin(), isbn.num.rend(), [](auto x) {return std::isdigit(x); }) };
if (cn != isbn.num.rend())
return (*cn - '0') == get_isbn_check_num({isbn.num.substr(0, isbn.num.length()-2), isbn.typ});
return false;
}
int main() {
// Prüfziffern ermitteln
const std::vector<ISBN>isbn_1 {
{"0-13-110362", ISBN_Type::isbn_10}, // 8
{"3-89864-117", ISBN_Type::isbn_10}, // 1
{"978381582086", ISBN_Type::isbn_13}, // 5
{"97 83 82 73 17 10", ISBN_Type::isbn_13}, // 0
{"4003301018392", ISBN_Type::isbn_13} // -1 (keine ISBN!)
};
for (const auto& i : isbn_1)
std::cout << i.num << " - > " << get_isbn_check_num(i) << "\n";
// Prüfen, ob es sich um eine ISBN-Nummer handelt (inkl. Prüfziffer)
ISBN isbn_2{ "0-13-110362-8", ISBN_Type::isbn_10 };
std::cout << isbn_2.num << " -> " << (is_isbn_num(isbn_2) ? "Wahr" : "Falsch\n");
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Hier einige Testdaten zu ISBN-10 (nach dem Pfeil steht die Prüfziffer):
- 0-13-110362 -> 8
- 3-89864-117 -> 1
- 3-446-13878 -> 1
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 1 |
Schwierigkeit: | Mittel |
Webcode: | gg7a-xaou |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |