Luhn算法无法正常运行c ++

时间:2015-11-22 17:43:29

标签: c++ algorithm

作为初学者,我一直在编写一个程序来使用Luhn算法验证一组信用卡号。它部分有效,但不完全正确。如果有人能向我解释为什么我会非常感激

应该输出到控制台

 Card : 4686006570307405 is valid
 Card : 4686006570307407 is invalid
 Card : 4093650457937474 is invalid
 Card : 4340423439668810 is valid
 Card : 1234567812345670 is valid
 Card : 5509415774265347 is invalid
 Card : X234567812345670 is invalid
 Card : 4539281167952835 is valid
 Card : 4532528637398511 is invalid
 Card : 4653549906803760 is valid
Press any key to continue . . .

而是输出

 Card : 4686006570307405 is invalid
 Card : 4686006570307407 is invalid
 Card : 4093650457937474 is invalid
 Card : 4340423439668810 is valid
 Card : 1234567812345670 is valid
 Card : 5509415774265347 is invalid
 Card : X234567812345670 is invalid
 Card : 4539281167952835 is invalid
 Card : 4532528637398511 is invalid
 Card : 4653549906803760 is invalid
Press any key to continue . . .

我的代码如下

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;


int convertChartoInt(const char digit)
{
    int numericalDigit = digit - '0';

    if (numericalDigit < 0 || numericalDigit > 9)
    {
        throw(0); //not a numerical digit - throw an exception
    }
    return (numericalDigit);
}

bool validateCardNumber(const string number) {

    bool cardStatus = false;
    int evenCount = 0, oddCount = 0,
        calculatedCheckDigit = 0, checkDigit = 0;
    int reverseNumber[15];

    try {

        checkDigit = convertChartoInt(number.at(15));

        for (int i = 0; i < 15; i++) //Reverse digits
        {
            reverseNumber[14 - i] = convertChartoInt(number.at(i));

            for (int i = 0; i < 15; i = i + 2) //calculate the multiple by 2 of the odd numbers
            {
                int doubledigit = 0;
                doubledigit = 2 * reverseNumber[i];
                if (doubledigit > 9)
                {
                    doubledigit = doubledigit - 9;
                }
                evenCount = evenCount + doubledigit;
            }
        }
        for (int i = 1; i < 15; i = i + 2) //calculate the sum of the even numbers
        {
            oddCount = oddCount + reverseNumber[i];
        }
        calculatedCheckDigit = (evenCount + oddCount) % 10; //calculate the check digit
        cardStatus = (calculatedCheckDigit == checkDigit);
    }
    catch (...) {
        cardStatus = false;
    }
    return(cardStatus);
}

int main()
{
    const string testCard[] = { "4686006570307405",
                            "4686006570307407",
                            "4093650457937474",
                            "4340423439668810",
                            "1234567812345670",
                            "5509415774265347",
                            "X234567812345670",
                            "4539281167952835",
                            "4532528637398511",
                            "4653549906803760" };

    int numberOfCards = sizeof(testCard) / sizeof(testCard[0]);

    for (int i = 0; i < 10; i++)
    {
        if (validateCardNumber(testCard[i]))
        {
            cout << " Card : " << testCard[i].c_str() << " is valid" << endl;
        }
        else {
            cout << " Card : " << testCard[i].c_str() << " is invalid" << endl;
        }

        }
    }

1 个答案:

答案 0 :(得分:0)

我不熟悉您尝试实现的算法,但似乎问题的根源是reverseNumber数组未初始化。

validateCardNumber功能中,您要设置reverseNumber[14 - i]值,然后阅读reverseNumber[i]。对于i = 0(最外层循环的第一次迭代),这意味着你正从内存中读回一些垃圾。