checksc50 是否有理由说我的代码在 pset1 信用中不能 100% 工作?

问题描述 投票:0回答:1

因此,由于某种原因,有些信用卡号码按预期工作,有些则不然(至少在 CS50 看来)对我来说,一切看起来都很好。 checkCS50 的内容如下:

:) credit.c exists

:) credit.c compiles

:( identifies 378282246310005 as AMEX
    expected "AMEX\n", not "INVALID\n"

:( identifies 371449635398431 as AMEX
    expected "AMEX\n", not "INVALID\n"

:( identifies 5555555555554444 as MASTERCARD
    expected "MASTERCARD\n", not "INVALID\n"

:( identifies 5105105105105100 as MASTERCARD
    expected "MASTERCARD\n", not "INVALID\n"

:) identifies 4111111111111111 as VISA

:( identifies 4012888888881881 as VISA
    expected "VISA\n", not "INVALID\n"

:) identifies 4222222222222 as VISA

:) identifies 1234567890 as INVALID (invalid length, checksum, identifying digits)

:) identifies 369421438430814 as INVALID (invalid identifying digits)

:) identifies 4062901840 as INVALID (invalid length)

:) identifies 5673598276138003 as INVALID (invalid identifying digits)

:) identifies 4111111111111113 as INVALID (invalid checksum)

:) identifies 4222222222223 as INVALID (invalid checksum)

:) identifies 3400000000000620 as INVALID (AMEX identifying digits, VISA/Mastercard length)

:) identifies 430000000000000 as INVALID (VISA identifying digits, AMEX length)

这是我的代码:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
long creditLong = get_long("What is your credit card number? "); // Get the number
char creditArray[20]; // Convert to array
snprintf(creditArray, sizeof(creditArray), "%ld", creditLong);

long creditLength = strlen(creditArray); // Get length
// printf("creditLength is %ld\n", creditLength);

int sumFirst = 0; // Checksum variables
int sumSecond = 0;

for (int i = creditLength - 2; i >= 0; i -= 2)
{                                     // For loops to get sum
    int digit = creditArray[i] - '0'; // ASCII to Int value ('0' = 48 in ASCII)
    sumFirst += digit * 2;
}

for (int j = creditLength - 1; j >= 0; j -= 2)
{
    int digit = creditArray[j] - '0';
    sumSecond += digit;
}
int checkSum = sumFirst + sumSecond;


char luhnTest[10]; // Convert to array
snprintf(luhnTest, sizeof(luhnTest), "%d", checkSum);
int testLength = strlen(luhnTest); // Get length of card number

if (luhnTest[testLength - 1] == '0')
{ // Start of If Statement

    switch (creditLength)
    {
        case 15:
            if (creditArray[0] == '3' && (creditArray[1] == '4' || creditArray[1] == '7'))
            {
                printf("AMEX\n");
            }
            else
            {
                printf("INVALID\n");
            }
            break;

        case 13:
            if (creditArray[0] == '4')
            {
                printf("VISA\n");
            }
            else
            {
                printf("INVALID\n");
            }
            break;

        case 16:
            if (creditArray[0] == '4')
            {
                printf("VISA\n");
            }
            else if (creditArray[0] == '5' && (creditArray[1] == '1' || creditArray[1] == '2' || creditArray[1] == '3' ||
                                               creditArray[1] == '4' || creditArray[1] == '5'))
            {
                printf("MASTERCARD\n");
            }
            else
            {
                printf("INVALID\n");
            }
            break;

        default:
            printf("INVALID\n");
    }
} // End of IF Statement
else
{
    printf("INVALID\n");
}
}
c computer-science cs50
1个回答
0
投票

我没有仔细阅读所有评论,但您问题的要点在于计算偶数位的哈希总量。

for (int i = creditLength - 2; i >= 0; i -= 2)
{                                     // For loops to get sum
    int digit = creditArray[i] - '0'; // ASCII to Int value ('0' = 48 in ASCII)
    sumFirst += digit * 2;
}

如果您仔细阅读了Luhn算法的方法论,您会注意到,如果被翻倍的数字的乘积大于“9”,则应该从被翻倍的数字中减去“9”的值。

仅供参考,以下是该算法的维基百科讨论的链接。

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

下面是重构的代码块。

for (int i = creditLength - 2; i >= 0; i -= 2)
{
    // For loops to get sum
    int digit = creditArray[i] - '0'; // ASCII to Int value ('0' = 48 in ASCII)
    if (digit > 4)                      /* As per the Luhn algorithm specs  */
        sumFirst += (digit * 2 - 9);
    else
        sumFirst += digit * 2;
}

通过一些重构,以下是对您的一些示例的测试。

craig@Vera:~/C_Programs/Console/CheckLuhn/bin/Release$ ./CheckLuhn 
What is your credit card number? 378282246310005
AMEX
craig@Vera:~/C_Programs/Console/CheckLuhn/bin/Release$ ./CheckLuhn 
What is your credit card number? 5555555555554444
MASTERCARD
craig@Vera:~/C_Programs/Console/CheckLuhn/bin/Release$ ./CheckLuhn 
What is your credit card number? 4012888888881881
VISA

您的校验和算法没有正确处理“5”或更高数字的条件加倍过程,这就是为什么许多测试错误地失败,但“4111111111111111”的示例通过了,因为所有数字都是“4”或少了。

这里的要点是在构建代码时彻底审查测试算法,以便代码与测试一致。

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