首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试isbn号以查看其是否有效

问测试isbn号以查看其是否有效
EN

Stack Overflow用户
提问于 2019-10-18 04:13:31
回答 2查看 482关注 0票数 1

我在一个家庭作业中遇到了问题,因为我需要检查ISBN代码是否有效。这是一个初学者的C++类,我对所有这些都是新手。现在,无论我输入什么ISBN,它都会告诉我它们都是有效的。如果有人可以帮助我指出正确的方向,以确定我的公式在我的for()语句中是否正确。

我没有问题的外部循环,它只是转换我认为我做错了。

由于这是一个入门的c++类,我们仍然只使用基本的函数,所以教授建议在ISBN中读取并将其存储在字符变量下。

代码语言:javascript
复制
**#include <iostream>
using namespace std;
int main()*
{
 int Counter;
 int WeightedSum;

char ISBN[11] = { 0 };
char Choice;
char Again{};
int Sum = 0;
const char X = 10;
int IterationCounter = 0;

do // begininng of do/while loop
{
    cout << "Would you like to check if your ISBN is valid or not? ";
    cout << "Press Y/y for yes or N/n for no and to end program: ";
    cin >> Choice; //choice input
    cout << endl << endl; // blank line 
    while (Choice != 'Y' && Choice != 'y' && Choice != 'N' && Choice != 'n') // beginning of while loop. checks user input to see if valid y/n choice
    {
        cout << "Invalid choice! Enter either Y/y or N/n"; // displays when anything other than y/n is entered 
        cout << "Press Y/y for yes or N/n for no and to end program: "; //gives user another chance to enter y/n
        cin >> Choice;
        cout << endl << endl;
    }
    if (Choice == 'Y' || Choice == 'y')
    {
        cout << "Enter the ISBN you wish to convert: " << endl;
        cin >> ISBN;
        cout << ISBN;
        cout << endl << endl;
        for (Counter = 0; Counter < 10; Counter++)
        {
            WeightedSum = ISBN[10] * (10 - Counter);
            Sum = WeightedSum + Sum;
        }
        cout << Sum;
        if (Sum % 11 == 0)
        {
            cout << Sum;
            cout << " Is a valid ISBN " << endl;
        }
        else 
        {
            cout << Sum;
            cout << " Is invalid " << endl;
        }*
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-18 13:53:19

ISBN是一个10位的number.The,它的前9位可以取0到9之间的任何值,但后几位有时可能取10。当最后一位等于"10“时,ISBN有11位数字。为了方便起见,可以将其写为“X”。

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

您应该首先检查ISBN的长度。

下面是我的代码:

代码语言:javascript
复制
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;
    cin >> ISBN;
    if (isValidISBN(ISBN))
        cout << "Valid\n";
    else
        cout << "Invalid\n";


    system("pause");

    return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-18 04:35:47

问题是这一行

代码语言:javascript
复制
WeightedSum = ISBN[10] * (10 - Counter);

你总是对位置11的字符执行数学运算,因为你把它乘以10 - Counter,从0到9,你基本上是把它乘以1到10的和,或者55。因为55可以被11整除,所以最终if (Sum % 11 == 0)的值总是为true。最坏的情况是,你甚至可能遇到访问冲突,因为你在访问之前没有检查ISBN的长度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58440111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档