尝试为 cs50 pset 实现 luhn 算法:但测试时该值不正确

问题描述 投票:0回答:1
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    long num = get_long("What's your card number: " );
    long countNum = num;
    int count = 0;
    while (countNum != 0)
    {
        countNum /= 10;
        count++;
    }
    printf("%i", count);
    int sum = 0;
    long tempNum = num;
    if (count == 13)
    {
        for (int i = 0; i < 7; i ++)
        {
            int n = i;
            while (n != 0)
            {
                tempNum /= 100;
                n--;
            }

            if (2 * (tempNum % 10) > 10) //should not matter if correct or incorrect in this case
            {
                sum = sum + ((2 * (tempNum % 10)) / 10) + ((2 * (tempNum % 10)) % 10);
            }
            else
            {
                sum += 2 * (tempNum % 10);
            }
            tempNum = num;
        }

        printf("%i", sum);
    }
}

我只是为 13 位数字制作算法的这一部分,当我测试时:

4222222222222
(据说应该有效)我得到了
1332
的值。谁能告诉我我的代码哪里出了问题? - 我怀疑底部的循环可能出了问题。

c cs50 luhn
1个回答
0
投票

鉴于这个问题仍然悬而未决,我想我应该继续强调代码的主要问题,因为它不包括哈希总数检查中的所有数字。接下来是程序的重构版本,其中包括将其他数字添加到哈希总数中。

#include <stdio.h>

int main(void)
{
    /*  long num = get_long("What's your card number: " ); Do not have CS50 installed - using conventional input method */
    long num;
    printf("What's your card number: ");
    scanf("%ld", &num);

    long countNum = num;
    int count = 0;
    while (countNum != 0)
    {
        countNum /= 10;
        count++;
    }
    printf("%i\n", count);
    int sum = 0;
    long tempNum = num;
    if (count == 13)
    {
        for (int i = 0; i < 13; i++)    /* Evaluate every digit of the card number  */
        {
            tempNum = num % 10;
            printf("Digit is: %ld", tempNum);
            if (i % 2 != 0)             /* Parity checking - every other digit gets doubled and checked if over "9" */
            {
                tempNum *= 2;
                if (tempNum > 9)
                    tempNum -= 9;
            }
            sum += tempNum;
            printf("  Value after parity check: %ld  Running hash total: %d\n", tempNum, sum);
            num /= 10;
        }

        printf("%i ", sum);

        if ((sum % 10) == 0)
            printf("Hash total is divisible by \"10\" and therefore is valid\n");
        else
            printf("Hash total is not divisible by \"10\" so is invalid\n");
    }
}

需要注意的关键点如下:

  • 所有数字都将被评估并添加到“sum”哈希总变量中。
  • 使用奇偶校验(偶/奇数位检查),将数字值相加或将数字值加倍,检查该值是否大于“9”(如果是,则减去“9” ),然后添加到“sum”哈希总数中。

添加额外的打印语句只是为了调试和说明目的。接下来是对重构代码的测试。

craig@Vera:~/C_Programs/Console/CCLuhn/bin/Release$ ./CCLuhn 
What's your card number: 4222222222222
13
Digit is: 2  Value after parity check: 2  Running hash total: 2
Digit is: 2  Value after parity check: 4  Running hash total: 6
Digit is: 2  Value after parity check: 2  Running hash total: 8
Digit is: 2  Value after parity check: 4  Running hash total: 12
Digit is: 2  Value after parity check: 2  Running hash total: 14
Digit is: 2  Value after parity check: 4  Running hash total: 18
Digit is: 2  Value after parity check: 2  Running hash total: 20
Digit is: 2  Value after parity check: 4  Running hash total: 24
Digit is: 2  Value after parity check: 2  Running hash total: 26
Digit is: 2  Value after parity check: 4  Running hash total: 30
Digit is: 2  Value after parity check: 2  Running hash total: 32
Digit is: 2  Value after parity check: 4  Running hash total: 36
Digit is: 4  Value after parity check: 4  Running hash total: 40
40 Hash total is divisible by "10" and therefore is valid

作为额外的信息,您可能需要参考以下有关 Luhn 算法的维基百科链接:

“https://en.wikipedia.org/wiki/Luhn_algorithm”

这可能有助于使卡测试更加通用,因为信用卡号的长度可以不是 13 位数字。

© www.soinside.com 2019 - 2024. All rights reserved.