首页 > 解决方案 > 关于 CS50 问题集 1 学分 - 所有输入都返回“无效”

问题描述

我首先自己尝试了代码,然后在 Youtube 上观看了 Deliberate Think 的视频后对其进行了调整。虽然我的代码可以编译,但所有信用卡号输入都返回“无效”,例如在 PayPal 上使用测试 AMEX 号码(378282246310005)时,check50 表示:“将 378282246310005 识别为 AMEX 预期的“AMEX\n”,而不是“INVALID\n ”。我认为在//实现Luhn的算法下可能有问题。

下面是我的代码:

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

int find_length(long long ccn);
bool check_luhn(long long ccn);
bool check_validity(long long ccn);
void print_card_brand(long long ccn);

//prompt user for credit card number; if invalid prompts again
int main(void)
{
    long long ccn;
    do
    {
        ccn = get_long_long("Credit Card Number: ");
    }
    while (ccn < 0);

    if (check_validity(ccn))
        print_card_brand(ccn);
    else
        printf("INVALID\n");
}

//find the length of the credit card number
int find_length(long long ccn)
{
    int length;
    for (length = 0; ccn!= 0; ccn /=10);
    length++;
    return length;
}

//implementing Luhn's algorithm
bool check_luhn(long long ccn)
{
    int sum = 0;
    for (int i = 0; ccn != 0; i++, ccn /= 10)
    {
        if (i % 2 == 0)
            sum += ccn % 10;
        else
        {
            int digit = 2 * (ccn % 10);
            sum += digit / 10 + digit % 10;
        }
    }
    return (sum % 10) == 0;
}

//check length and Luhn's algorithm
bool check_validity(long long ccn)
{
    int length = find_length(ccn);
    return (length == 13 || length == 15 || length == 16) && check_luhn(ccn);
}

//print credit card brand
void print_card_brand(long long ccn)
{
    if ( (ccn >= 34e13 && ccn < 35e13) || (ccn >= 37e13 && ccn < 38e13) )
        printf("AMEX\n");
    else if (ccn >= 51e14 && ccn < 56e14)
        printf("MASTERCARD\n");
    else if ( (ccn >= 4e12 && ccn < 5e12) || (ccn >= 4e15 && ccn < 5e15) )
        printf("VISA\n");
    else
        printf("INVALID\n");
}

提前感谢大家的帮助,我已经坚持了这么多天。以前从未做过编码,我不知道出了什么问题。

标签: ccs50luhn

解决方案


你有一些错误,在上面的评论中指出的是一个流氓分号。您对 luhn 的实现不太正确,因此我已尝试对其进行修复。我试图在进行最小更改的同时解决此问题。我无法访问 CS50.h,所以我添加了 stdbool 和一个狡猾的 scanf。我会撤消这两个更改。

#include <stdio.h>
#include <stdbool.h>

int find_length(long long ccn);
bool check_luhn(long long ccn);
bool check_validity(long long ccn);
void print_card_brand(long long ccn);

//prompt user for credit card number; if invalid prompts again
int main(void)
{
    long long ccn;
    do
    {
        scanf("Credit card number: %lld", &ccn);
    }
    while (ccn < 0);

    if (check_validity(ccn))
        print_card_brand(ccn);
    else
        printf("INVALID\n");
}

//find the length of the credit card number
int find_length(long long ccn)
{
    int length;
    for (length = 0; ccn!= 0; ccn /=10)
      length++;

    return length;
}

//implementing Luhn's algorithm
bool check_luhn(long long ccn)
{
    int sum = 0;
    long long temp_ccn = ccn;
    int length = find_length(ccn);

    for (int i = 1; i <= find_length(ccn); i++)
    {
        int rightDigit = temp_ccn % 10LL;

        if (i % 2 == 0)
        {
            rightDigit = rightDigit * 2;

            if (rightDigit > 9)
            {
                rightDigit -= 9;
            }
        }

        sum += rightDigit;
        temp_ccn /= 10LL;
    }

    return (sum % 10) == 0;
}

//check length and Luhn's algorithm
bool check_validity(long long ccn)
{
    int length = find_length(ccn);
    return (length == 13 || length == 15 || length == 16) && check_luhn(ccn);
}

//print credit card brand
void print_card_brand(long long ccn)
{
    if ( (ccn >= 34e13 && ccn < 35e13) || (ccn >= 37e13 && ccn < 38e13) )
        printf("AMEX\n");
    else if (ccn >= 51e14 && ccn < 56e14)
        printf("MASTERCARD\n");
    else if ( (ccn >= 4e12 && ccn < 5e12) || (ccn >= 4e15 && ccn < 5e15) )
        printf("VISA\n");
    else
        printf("INVALID\n");
}

推荐阅读