为什么处理某些卡号时没有输出? cs50信用问题

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

该程序要求用户提供信用卡号,然后使用 luhns 算法和每张卡的要求(通过 printf)报告它是否是有效的 American Express、MasterCard 或 Visa 卡号。测试代码时,有些卡号就是没有输出。源码及测试结果如下:

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

bool validation(long number) {
    long i = 10;
    long j = 1;
    int totalMultiplied = 0;
    int totalNotMultiplied = 0;

    while (number / i != 0){
        totalMultiplied += (((number / i) % 10) * 2) % 10 + (((number / i) % 10) * 2) / 10;
        i *= 100;
    }
    while (number / j != 0) {
        totalNotMultiplied += (number / j) % 10;
        j *= 100;
    }

    return ((totalMultiplied + totalNotMultiplied) % 10 == 0);
}

int main(void) {
    long creditCardNo;
    do {
        creditCardNo = get_long("Enter credit card number: ");
    }
    while (creditCardNo < 0);

    int count = 0;
    long startNumbers = creditCardNo;
    long n = creditCardNo;

    do {
        n /= 10;
        count++;
    }
    while (n != 0);

    while (startNumbers >= 100) {
        startNumbers /= 10;
    }



    if (validation(creditCardNo)) {
        if (count == 15 && ( startNumbers == 34 || startNumbers == 37)) {
            printf("AMEX\n");
        }
        else if (count == 16 && ( startNumbers == 51 || startNumbers == 52 || startNumbers == 53 || startNumbers == 54 || startNumbers == 55)) {
            printf("MASTERCARD\n");
        }
        else if ((count == 13 || count == 16) && ( startNumbers / 10 == 4)) {
            printf("VISA\n");
        }
    }else {
        printf("INVALID\n");

    }


}



测试结果:

Results for cs50/problems/2024/x/credit generated by check50 v3.3.11
:) credit.c exists
:) credit.c compiles
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:) identifies 4222222222222 as VISA
:) identifies 1234567890 as INVALID (invalid length, checksum, identifying digits)
:( identifies 369421438430814 as INVALID (invalid identifying digits)
    expected "INVALID\n", not ""
:( identifies 4062901840 as INVALID (invalid length)
    expected "INVALID\n", not ""
:( identifies 5673598276138003 as INVALID (invalid identifying digits)
    expected "INVALID\n", not ""
:) identifies 4111111111111113 as INVALID (invalid checksum)
:) identifies 4222222222223 as INVALID (invalid checksum)
:( identifies 3400000000000620 as INVALID (AMEX identifying digits, VISA/Mastercard length)
    expected "INVALID\n", not ""
:( identifies 430000000000000 as INVALID (VISA identifying digits, AMEX length)
    expected "INVALID\n", not ""

正如您所看到的,某些卡号的输出只是“”一个空字符串,我不知道为什么。

c cs50
1个回答
0
投票

你这个块的逻辑有一个漏洞:

if (validation(creditCardNo)) {
    if (count == 15 && ( startNumbers == 34 || startNumbers == 37)) {
        printf("AMEX\n");
    }
    else if (count == 16 && ( startNumbers == 51 || startNumbers == 52 || startNumbers == 53 || startNumbers == 54 || startNumbers == 55)) {
        printf("MASTERCARD\n");
    }
    else if ((count == 13 || count == 16) && ( startNumbers / 10 == 4)) {
        printf("VISA\n");
    }
}else {
    printf("INVALID\n");

}

从逻辑上讲,您的validation()有可能返回true,然后无法满足该分支内的其他条件。

您拥有所有源代码,添加一些断点并逐步完成一些测试。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.