我有一个非常有趣的案例。我已经编写了源代码,并且可以正常工作,但是当我将代码添加到课程中进行审查时,出现了下一个问题:

:) credit.c exists.

:) credit.c compiles.

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

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

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

:) identifies 5105105105105100 as MASTERCARD

:) identifies 4111111111111111 as VISA

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

:( identifies 1234567890 as INVALID
    expected "INVALID\n", not "\nINVALID\n"

:) identifies 369421438430814 as INVALID

:( identifies 4062901840 as INVALID
    expected exit code 0, not 1

:) identifies 5673598276138003 as INVALID

:( identifies 4111111111111113 as INVALID
    expected exit code 0, not 1

:) rejects a non-numeric input of "foo"

:) rejects a non-numeric input of ""


下一部分是我的代码:

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

int main (void)
{
    long long card_num;


    // getting card number
    do
    {
        card_num = get_long("Number: \n");
    }
    while (card_num < 1 || card_num > 9999999999999999);

    for (int i = 0; i < card_num.length(); i++)
    {
        if (card_num[i] == '-')
            printf('foo\n');
            card_num[i] == '';
        else (card_num[i] == ' ')
            printf ('foo\n');
            card_num[i] == '';
    }

    long long temp_num = card_num;

    int count = 0;

    while (temp_num > 0)
    {
        temp_num = temp_num / 10;
        count++;
    }


    // umber volume cheking
    if (count != 13 && count != 15 && count != 16)
    {
        printf("INVALID\n");
        return 1;
    }

    // luna's algorithm checking
    int sum = 0;
    temp_num = card_num;
    for (int i = 1; i <= count; i++)
    {
        int digit = temp_num % 10;
        if (i % 2 == 0)
        {
            digit *= 2;
            if (digit > 9)
            {
                digit -= 9;
            }
        }

        sum += digit;
        temp_num /= 10;
    }

    if (sum % 10 != 0)
    {
        printf("INVALID\n");
        return 1;
    }

    // bank info cheking
    temp_num = card_num;
    while (temp_num > 100)
    {
        temp_num = temp_num / 10;
    }

    int company_id = temp_num;
    if (company_id > 50 && company_id < 56 && count == 16)
    {
        printf("MASTERCARD\n");
    }
    else if ((company_id == 34 || company_id == 37) && (count == 15))
    {
        printf("AMEX\n");
    }
    else if ((company_id / 10 == 4) && (count == 13 || count == 16))
    {
        printf("VISA\n");
    }
    else
    {
        printf("INVALID\n");
    }
    return 0;
}

最佳答案

好消息! CS50有自己的SE社区,您可以找到here

请密切注意错误告诉您的内容。如此处所示,在某处输出了额外的回车符(\n)::( identifies 378282246310005 as AMEX expected "AMEX\n", not "\nAMEX\n"。注意check50期望("AMEX\n")和程序输出("\nAMEX\n")之间的区别。一旦纠正,它将消除5个错误。好的开始。

查看该代码以查看其返回退出代码1的位置。这不是规范所必需的。遵守规格。有时更多不是更好:)。纠正该错误后,它将消除最后两个错误。

也许这些看起来像很小的细节,不应该影响评分。但是,如果老板或客户希望严格遵守规范,这些“小细节”可能会转化为真实的金钱。

09-15 13:51