用于检查ISBN的程序, 亚马逊ISBN, 亚马逊ASIN, Program to check for ISBN

ISBN-1
ISBN-1
用于检查ISBN的程序, 亚马逊ISBN, 亚马逊ASIN, Program to check for ISBN[/caption]

 

一个ISBN(国际标准书号)是用来标识一本书的10位数字。

ISBN号的前九位用于表示书的标题,出版商和组,最后一位用于检查ISBN是否正确。

它的前9位可以取0到9之间的任何值,但最后的数字有时可能取值等于10; 这是通过将其写为’X’来完成的。

要验证ISBN,请计算第一个数字的10倍,再加上第二个数字的9倍,再加上第三个数字的8倍,依此类推,直到我们将最后一个数字加1次为止。如果最终数字除以11时没有余数,则代码是有效的ISBN。

 

例子 :

Input : 007462542X
Output : Valid
007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 
        5*2 + 4*5 + 3*4 + 2*2 + 1*10 = 176
Since 55 leaves no remainder when divided 
by 11, hence it is a valid ISBN.

Input : 0112112425
Output : Invalid
0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 +
           5*1 + 4*1 + 3*4 + 2*2 + 1*5 = 71
Since 71 is not divisible by 11, given number
is not a valid ISBN.

 

现在,我们设计了一个程序来接受用户的十位数代码,然后我们将检查一个数字是否是ISBN。显示适当的消息。

 

 

C++

// CPP program to check if a 
// given ISBN is valid or not 
#include <bits/stdc++.h> 
using namespace std; 

bool isValidISBN(string& isbn) 
{ 
	// length must be 10 
	int n = isbn.length(); 
	if (n != 10) 
		return false; 

	// Computing weighted sum 
	// of first 9 digits 
	int sum = 0; 
	for (int i = 0; i < 9; i++) 
	{ 
		int digit = isbn[i] - '0'; 
		if (0 > digit || 9 < digit) 
			return false; 
		sum += (digit * (10 - i)); 
	} 

	// Checking last digit. 
	char last = isbn[9]; 
	if (last != 'X' && (last < '0' || 
						last > '9')) 
		return false; 

	// If last digit is 'X', add 10 
	// to sum, else add its value. 
	sum += ((last == 'X') ? 10 : 
				(last - '0')); 

	// Return true if weighted sum 
	// of digits is divisible by 11. 
	return (sum % 11 == 0); 
} 

// Driver code 
int main() 
{ 
	string isbn = "007462542X"; 
	if (isValidISBN(isbn)) 
		cout << "Valid"; 
	else
		cout << "Invalid"; 
	return 0; 
} 

 

JAVA

// Java program to check if 
// a given ISBN isvalid or not 

class GFG 
{ 
	static boolean isValidISBN(String isbn) 
	{ 
		// length must be 10 
		int n = isbn.length(); 
		if (n != 10) 
			return false; 

		// Computing weighted sum 
		// of first 9 digits 
		int sum = 0; 
		for (int i = 0; i < 9; i++) 
		{ 
			int digit = isbn.charAt(i) - '0'; 
			if (0 > digit || 9 < digit) 
				return false; 
			sum += (digit * (10 - i)); 
		} 

		// Checking last digit. 
		char last = isbn.charAt(9); 
		if (last != 'X' && (last < '0' || 
							last > '9')) 
			return false; 

		// If last digit is 'X', add 10 
		// to sum, else add its value 
		sum += ((last == 'X') ? 10 : (last - '0')); 

		// Return true if weighted sum 
		// of digits is divisible by 11. 
		return (sum % 11 == 0); 
	} 

	// Driver code 
	public static void main(String[] args) 
	{ 
		String isbn = "007462542X"; 
		if (isValidISBN(isbn)) 
			System.out.print("Valid"); 
		else
			System.out.print("Invalid"); 
	} 
} 

// This code is contributed 
// by Anant Agarwal. 

 

Python3

# Python code to check if a 
# given ISBN is valid or not. 

def isValidISBN(isbn): 

	# check for length 
	if len(isbn) != 10: 
		return False
	
	# Computing weighted sum 
	# of first 9 digits 
	_sum = 0
	for i in range(9): 
		if 0 <= int(isbn[i]) <= 9: 
			_sum += int(isbn[i]) * (10 - i) 
		else: 
			return False
		
	# Checking last digit 
	if(isbn[9] != 'X' and
	0 <= int(isbn[9]) <= 9): 
		return False
	
	# If last digit is 'X', add 
	# 10 to sum, else add its value. 
	_sum += 10 if isbn[9] == 'X' else int(isbn[9]) 
	
	# Return true if weighted sum of 
	# digits is divisible by 11 
	return (_sum % 11 == 0) 

# Driver Code 
isbn = "007462542X"
if isValidISBN(isbn): 
	print('Valid') 
else: 
	print("Invalid") 
	
# This code is contributed 
# by "Abhishek Sharma 44" 

 

C#

// C# program to check if 
// two strings are identical 
using System; 

class GFG { 

	// Driver code 
	public static void Main() 
	{ 

		// Get the strings which is to be checked 
		String string1 = Console.ReadLine(); 
		Console.WriteLine("Enter the first string: "
						+ string1); 

		// Get the strings which is to be checked 
		String string2 = Console.ReadLine(); 
		Console.WriteLine("Enter the second string :"
						+ string2); 

		// Check if both strings are equal 
		Console.WriteLine("\nAre both strings same: "); 

		if (string1.Equals(string2) == true) { 
			Console.WriteLine("Yes"); 
		} 
		else { 
			Console.WriteLine("No"); 
		} 
	} 
} 

/* This code contributed by PrinciRaj1992 */

 

PHP

<?php 
// PHP program to check if a 
// given ISBN is valid or not. 

function isValidISBN($isbn) 
{ 
	// length must be 10 
	$n = strlen($isbn); 
	if ($n != 10) 
		return -1; 

	// Computing weighted sum 
	// of first 9 digits 
	$sum = 0; 
	for ($i = 0; $i < 9; $i++) 
	{ 
		$digit = $isbn[$i] - '0'; 
		if (0 > $digit || 9 < $digit) 
			return -1; 
		$sum += ($digit * (10 - $i)); 
	} 

	// Checking last digit. 
	$last = $isbn[9]; 
	if ($last != 'X' && ($last < '0' || 
						$last > '9')) 
		return -1; 

	// If last digit is 'X', add 10 
	// to sum, else add its value. 
	$sum += (($last == 'X') 
	? 10 : ($last - '0')); 

	// Return true if weighted sum of 
	// digits is divisible by 11. 
	return ($sum % 11 == 0); 
} 

// Driver code 
$isbn = "007462542X"; 
if (isValidISBN($isbn)) 
	echo "Valid"; 
else
	echo "Invalid"; 

// This code is contributed by ajit. 
?> 

 

Output :

Valid

The above code checks for ISBN 10. The recent version of ISBN is ISBN 13 (in 2005).

 

 

本文:用于检查ISBN的程序, 亚马逊ISBN, 亚马逊ASIN, Program to check for ISBN

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.