C# :: Aufgabe #63

9 Lösungen Lösungen öffentlich

ISBN-Nummern überprüfen

Anfänger - C# von bibir - 03.09.2014 um 09:10 Uhr
Eine ISBN-Nummer setzt sich immer aus vier Teilen zusammen, der Gruppenzahl, Verlagsnummer, Titelnummer und einer Prüfziffer. Diese vier Teile werden durch Bindestriche (oder seltener Leerzeichen) abgetrennt. Eine ISBN-Nummer ist stets zehnstellig.

Die Prüfziffer erlaubt, die Gültigkeit einer ISBN-Nummer festzustellen. Sie wird so gewählt, dass die ganze Nummer folgende mathematische Eigenschaft erfüllt:
Man multipliziere die erste Stelle mit 10, die zweite Stelle mit 9, die dritte mit 8, und so weiter bis zur neunten Stelle (mal 2), und addiere alle erhaltenen Produkte. Wenn man zu dieser Zahl die Prüfziffer dazuaddiert, so muss ein Vielfaches von 11 entstehen. Als Besonderheit kann die Prüfziffer auch den Buchstaben X annehmen, der dann als Zahlenwert 10 interpretiert wird.

Beispiel:
Die ISBN-Nummer lautet 3-8931-9301-4, also rechnen wir
3 * 10 + 8 * 9 + 9 * 8 + 3 * 7 + 1 * 6 + 9 * 5 + 3 * 4 + 0 * 3 + 1 * 2 + Prüfziffer 4 = 264.
Diese Zahl ist 24 * 11, also ist es eine gültige ISBN-Nummer.

Lösungen:

vote_ok
von pocki (4190 Punkte) - 07.09.2014 um 17:18 Uhr
Quellcode ausblenden C#-Code
void Main()
{
	System.Console.Write("ISBN-Nummer eingeben: ");
	string input = System.Console.ReadLine();
	
	//Unnötige Zeichen entfernen
	string clearedIn = input.ToUpper().Replace("-","").Replace(" ","").Trim();
	
	//Eingabe nach int[] parsen
	int[] numbers = clearedIn.ToCharArray().Select<char,int>(i => i == 'X' ? 10 : int.Parse(i.ToString())).ToArray();
	
	int sum = 0;
	for (int i = 0; i < 10; i++)
	{
		sum += numbers[i]*(10-i);	
	}
	
	if (sum % 11 == 0)
	{
		System.Console.WriteLine("ISBN-Nummer gültig!");
	}
	else
	{
		System.Console.WriteLine("ISBN-Nummer ungültig!");
	}
}
vote_ok
von Gisbert5020 (3120 Punkte) - 08.09.2014 um 12:31 Uhr
Quellcode ausblenden C#-Code
static void Main(string[] args)
        {
            Console.WriteLine("Geben Sie die IBN-Nummer ein #-####-####-#:");
            string nummer = Console.ReadLine();
            int j = 10, summe=0;
            for (int i = 0; i < 12; i++)
            {
                if (nummer[i] != '-' && nummer[i] != ' ')
                {
                    summe += nummer[i] * j;
                    j--;
                }
            }
            summe += nummer[12];
            int ergebnis = summe % 11;
            if (ergebnis ==0) 
                Console.WriteLine("Die ISBN-Nummer ist gültig");
            else
                Console.WriteLine("Die ISBN-Nummer ist falsch");
            Console.ReadLine();
        }
vote_ok
von Mexx (2370 Punkte) - 19.09.2014 um 11:28 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISBN_Nummer_Prüfen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string isbn = tbISBN.Text.Trim();
            if (isbn.Length == 13)
            {
                string[] splitt = isbn.Split('-');
                int prüfziffer = Convert.ToInt32(splitt[0]) * 10;
                int count = 9;
                int durchlauf = 1;
                for (int i = 0; i < 2; i++)
                {
                    foreach (char c in splitt[durchlauf])
                    {
                        int zahl = Convert.ToInt32(c.ToString());
                        prüfziffer += zahl * count;
                        count--;
                    }
                    durchlauf++;
                }
                if (splitt[3] == "x" || splitt[3] == "X")
                {
                    prüfziffer += 10;
                }
                else
                {
                    prüfziffer += Convert.ToInt32(splitt[3]);
                }
                double rest = prüfziffer % 11;
                if (rest == 0)
                {
                    lblISBNOK.ForeColor = Color.Green;
                    lblISBNOK.Text = "ISBN Nummer OK";
                }
                else
                {
                    lblISBNOK.ForeColor = Color.Red;
                    lblISBNOK.Text = "ISBN Nummer falsch";
                }
            }
            else
            {
                MessageBox.Show("Keine gültige ISBN Nummer!");
            }
        }
    }
}
vote_ok
von Sebastian89 (80 Punkte) - 27.02.2015 um 12:23 Uhr
Quellcode ausblenden C#-Code
void Main()
{
	string isbn = Console.ReadLine();
	Regex regex = new Regex(@"^(?<isbnValue>[0-9\-]{9,12})(?<checkSum>[0-9Xx]{1})$");
	
	Match match = regex.Match(isbn);
	if (match.Success)
	{
		string cleanedIsbn = isbn.Replace("-", string.Empty);
		int checkSumValue = 0;
		int multiply = 10;
		
		foreach (int value in cleanedIsbn.Select(entry => entry == 'X' ? 10 : Convert.ToInt32(entry) - '0'))
			checkSumValue += value * multiply--;
		
		Console.WriteLine(checkSumValue % 11 == 0 ? "Valid ISBN!" : "Invalid ISBN checksum!");
	}
	else 
	{
		Console.WriteLine("Invalid ISBN input!");
	}
}
vote_ok
von niknik (1230 Punkte) - 13.08.2015 um 14:17 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ISBNPrüfer
{
    class Program
    {

        public static int[] ReadIsbnZahl()
        {
            int[] isbnzahl = new int[10];
            bool valid = false;
            while(valid != true)
            {
                Console.Clear();
                Console.WriteLine("Geben Sie die ISBN-Zahl an: (ohne Bindestrich)");
                string eingabe = Console.ReadLine().ToUpper();
                if(eingabe.Length != 10)
                {
                    Console.WriteLine("Fehlerhafte Eingabe.");
                    Console.ReadLine();
                    return ReadIsbnZahl();
                }
                isbnzahl = new int[eingabe.Length];
                for (int i = 0; i < eingabe.Length; i++)
                {
                    if (i <= 8)
                    {
                        valid = int.TryParse(eingabe[i].ToString(), out isbnzahl[i]);
                        if (valid == false || isbnzahl[i] < 0 || isbnzahl[i] > 9)
                        {
                            Console.WriteLine("Fehlerhafte Eingabe.");
                            Console.ReadLine();
                            return ReadIsbnZahl();
                        }
                    }
                    if (i == 9)
                    {
                        if (eingabe[i] == 'X')
                        {
                            isbnzahl[i] = 10;
                        }
                        else
                        {
                            valid = int.TryParse(eingabe[i].ToString(), out isbnzahl[i]);
                            if (valid == false || isbnzahl[i] < 0 || isbnzahl[i] > 9)
                            {
                                Console.WriteLine("Fehlerhafte Eingabe.");
                                Console.ReadLine();
                                return ReadIsbnZahl();
                            }
                        }
                    }
                }
                valid = true;
            }
            return isbnzahl;
        }

        public static bool ISBNcheck(int[] eingabe)
        {
            int result = 0;
            for (int i = 0; i < 9; i++)
            {
                result += eingabe[i] * (10 - i);
            }
            result += eingabe[9];

            return (result % 11 == 0);
        }
        static void Main(string[] args)
        {
            int[] isbn = ReadIsbnZahl();

            /* Console.WriteLine("ISBN: ");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(isbn[i]);
            } */

            if (ISBNcheck(isbn))
            {
                Console.WriteLine("Gültige ISBN-Nummer");
            }
            else
            {
                Console.WriteLine("Ungültige ISBN-Nummer");
            }
            Console.ReadLine();
        }
    }
}
vote_ok
von DrizZle (360 Punkte) - 15.06.2016 um 13:29 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ISBNChecker
{
    class Program
    {
        static void Main(string[] args)
        {
		    while(true)
			{
		        Console.Write("ISBN:");
			    string ISBN = Console.ReadLine();
			    if(IsValid(ISBN))
			        Console.WriteLine(ISBN + " ist gültig!");
			    Console.WriteLine(ISBN + " ist ungültig!");
			}
		}
		static bool IsValid(string ISBN)
		{
		    int faktor = 10, summe = 0, temp = 0;
			if(ISBN[12].ToString() == "X")
			    ISBN = ISBN.Replace("X", "10");
			for(int i = 0; i < 12; i++)
			{
			    if(ISBN[i].ToString() != "-" && ISBN[i].ToString() != " ")
				{
				    summe+= ISBN[i] * faktor;
					faktor--;
				}
			}
			summe+= ISBN[12]; //add prüfziffer
			temp = summe % 11;
			if(temp == 0)
			    return true;
			return false;
		}
	}
}
vote_ok
von stbehl (1640 Punkte) - 14.02.2018 um 14:41 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrainYourProgrammer63
{
    class Program
    {
        static void Main(string[] args)
        {
            bool iSBN = false;
            int summe = 0;
            int multiplikator = 10;
            Console.Write("Geben Sie eine ISBN-Nummer ein, die auf Gültigkeit überprüft werden soll: ");
            string eingabe = Console.ReadLine();
            string eingabeModifiziert = eingabe.Replace("-", "");
            for (int i = 0; i<=eingabeModifiziert.Length-2; i++)
            {
                summe += eingabeModifiziert[i] * multiplikator;
                multiplikator--;
            }
            summe += eingabeModifiziert[eingabeModifiziert.Length - 1];

            if (summe % 11 == 0)
            {
                iSBN = true;
            }
            if (iSBN)
            {
                Console.WriteLine("Die ISBN-Nummer: {0} ist eine gültige ISBN-Nummer.", eingabe);
            }
            else
            {
                Console.WriteLine("Die Nummer {0} ist keine gültige ISBN-Nummer.", eingabe);
            }
            Console.ReadKey();
        }
    }
}
vote_ok
von stcalvin (970 Punkte) - 16.05.2018 um 08:40 Uhr
Quellcode ausblenden C#-Code
        static void ISBN(string ISBN_Nummer)
        {
            int mult = 10, sum = 0;

            for (int i = 0; i < ISBN_Nummer.Length; i++)
            {
                if (Int32.TryParse(ISBN_Nummer[i].ToString(), out int ziffer))
                {
                    sum += ziffer * mult;
                    mult--;
                }
            }

            if (sum % 11 == 0)
            {
                Console.WriteLine("Die ISBN ist gültig.");
            }
            else
            {
                Console.WriteLine("Die ISBN ist nicht gültig.");
            }
        }
vote_ok
von JKooP (18090 Punkte) - 02.05.2021 um 15:51 Uhr
NET 5.x; C# 9.x; VS-2019
Quellcode ausblenden C#-Code
using System;
using System.Linq;

var s = "3-8931-9301-4";
Console.WriteLine(IsIsbn10(s));

static bool IsIsbn10(string s)
{
    var i = 10;
    var r = s.ToUpper().Where(x => x is >= '0' and <= '9' or 'X').Select(x => x is 'X' ? 10 : x - '0');
    return (r.Take(9).Select(x => x * i--).Sum() + r.TakeLast(1).FirstOrDefault()) % 11 == 0;
}