开发者

Isbn generate the check digit

开发者 https://www.devze.com 2023-01-14 00:53 出处:网络
This code verifies whether an ISBN is valid. For nine-digit inputs, I\'d like to form a valid ISBN by calculating and appending the check digit. For inputs less than nine digits, I\'d like it to retur

This code verifies whether an ISBN is valid. For nine-digit inputs, I'd like to form a valid ISBN by calculating and appending the check digit. For inputs less than nine digits, I'd like it to return the error message "Please enter a correct number". How should I go about this?

   public class isbn
    {   //attributes
         private string isbnNum;
         //method   
         public string GetIsbn()
         {
             return this.isbnNum;
         }
           //constructor
           public isbn()
           {
               Console.Write("Enter Your ISBN Number: ");
               this.isbnNum = Console.ReadLine();

           }//end default constructor

            //method
           public string displayISBN()
           {

               return  this.GetIsbn();

           }


       public static void Main(string[] args)
        {
            //create a new instance of the ISBN/book class

            isbn myFavoriteBook = new isbn();

            //contains the method for checking validity 
            bool isValid = CheckDigit.CheckIsbn(myFavoriteBook.GetIsbn());

            //print out the results of the validity.
            Console.WriteLine(string.Format("Your book {0} a valid ISBN",
                        开发者_如何学C               isValid ? "has" : "doesn't have"));

            Console.ReadLine();

        }

public static class CheckDigit
{       // attributes
    public static string NormalizeIsbn(string isbn)
    {
        return isbn.Replace("-", "").Replace(" ", "");
    }
   public static bool CheckIsbn(string isbn) // formula to check ISBN's validity
    {
        if (isbn == null)
            return false;

        isbn = NormalizeIsbn (isbn);
        if (isbn.Length != 10)
            return false;

        int result;
        for (int i = 0; i < 9; i++)
            if (!int.TryParse(isbn[i].ToString(), out result))
                return false;

        int sum = 0;
        for (int i = 0; i < 9; i++)
            sum += (i + 1) * int.Parse(isbn[i].ToString());

        int remainder = sum % 11;
        if (remainder == 10)
            return isbn[9] == 'X';
        else
            return isbn[9] == (char)('0' + remainder);
    }


Just change it to append the last character rather than checking that it's present. The above could be cleaned up a bit, but just changing it as required results in:

public static string MakeIsbn(string isbn) // string must have 9 digits
{
    if (isbn == null)
        throw new ArgumentNullException();

    isbn = NormalizeIsbn (isbn);
    if (isbn.Length != 9)
        throw new ArgumentException();

    int result;
    for (int i = 0; i != 9; i++)
        if (!int.TryParse(isbn[i].ToString(), out result))
            throw new ArgumentException()

    int sum = 0;
    for (int i = 0; i != 9; i++)
        sum += (i + 1) * int.Parse(isbn[i].ToString());

    int remainder = sum % 11;
    if (remainder == 10)
        return isbn + 'X';
    else
        return isbn + (char)('0' + remainder);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号