CS50 Pset1信用...有人能请解释为什么程序不工作,我到处找,没有解决方案,是使用函数

wgeznvg7  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(121)

CS50 PSET 1信用-https://cs50.harvard.edu/x/2022/psets/1/credit/众所周知的问题,在CS50社区,所以不打算张贴在这里的完整问题。
我打算用不同的函数来解决这个问题,而不是把所有的东西都写在main里面,这是我几个小时来一直在寻找的。
如果任何人有这个问题的解决方案,使用不同的功能,为每一步,请分享答案。

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

long get_credit_number(void);
int checksum(long cn);


// Driver code
int main(void)
{
   int valid = 1;
   long cn = get_credit_number();
   int cm = checksum(cn);


   long ccn = cn;
   int length = 0;
   long divisor = 10;
   int first_digit; int first_twodigit;

    if (cm == valid)
    {
        // Check length of credit card number
        while (ccn > 0)
       {
            ccn = ccn % 10;
            length++;
        }
        // Check first two digits of credit card number
        for (int i = 0; i < length - 2; i++)
        {
            divisor = divisor * 10;
        }
        first_digit = ccn/divisor;
        first_twodigit = ccn / (divisor / 10);

        // check whether card is AMEX
        if (length == 15 && (first_twodigit == 34 || first_twodigit == 37))
        {
            printf("AMEX\n");
        }
        // check whether card is MASTERCARD
        else if (length == 16 && (first_twodigit > 50 && first_twodigit == 56))
        {
            printf("MASTERCARD\n");
        }
        //check whether card id VISA
        else if ((length == 13 || length == 16) && first_digit == 4)
        {
            printf("VISA");
        }
    }
    else
    {
        printf("INVALID\n");
    }

}

// Prompt user to enter the credit card number
long get_credit_number(void)
{
    long cna;
    do
    {
       cna = get_long("Number: ");
    }
    while (cna <= 0);
    return cna;
}

// Checksum by Luhm algorithm
int checksum(long cn)
{
    int sum = 0;
    long cna;
    long ccn = cn;

    while (ccn > 0)
    {
        cna = cn % 10;
        sum = sum + cna ;
        ccn = ccn / 100;
    }

    ccn = ccn/10;
    long cnb;

    while (ccn > 0)
    {
        cna = ccn % 10;
        cnb = 2 * cna;
        sum = sum + (cnb%10) + (cnb/10);
        ccn = ccn / 100;
    }

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

https://cs50.harvard.edu/x/2022/psets/1/credit/

mzsu5hc0

mzsu5hc01#

把这个解决方案贴在这里给将来的cs50学生。我把这个问题分成三个不同的函数来解决,完全避免了使用全局变量。希望这对将来的人有所帮助。

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

// Functions Declaration
long get_credit_number(void);
long checksum(long credit_number);
void finalcheck(long check_sum, long credit_number);

// Driver code
int main(void)
{
    // Calling the Functions
    long credit_number = get_credit_number();
    long check_sum = checksum(credit_number);
    finalcheck(check_sum, credit_number);
}


// Prompt user to enter the credit card number
long get_credit_number(void)
{
    long credit_num;
    do
    {
        credit_num = get_long("Number: ");
    }
    while (credit_num <= 0);
    return credit_num;
}

// Checksum by Luhm algorithm
long checksum(long credit_number)
{
    long sum = 0;
    long last_digit;
    long credit_card_number = credit_number;
    // Luhm's algorithm Case 1
    while (credit_card_number > 0)
    {
        last_digit = credit_card_number % 10;
        sum = sum + last_digit ;
        credit_card_number = credit_card_number / 100;
    }

    credit_card_number = credit_number / 10;
    long two_times;
    // Luhm's algorithm Case 2
    while (credit_card_number > 0)
    {
        last_digit = credit_card_number % 10;
        two_times = 2 * last_digit;
        sum = sum + (two_times % 10) + (two_times / 10);
        credit_card_number = credit_card_number / 100;
    }
    sum = sum % 10;
    // Check whether the last digit of sum is 0 or not
    if (sum == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

//Final check whether the card is Visa, Amex, Mastercard or Invalid
void finalcheck(long check_sum, long credit_number)
{
    long valid_cc = credit_number;
    int length = 0;
    long divisor = 10;
    int first_digit;
    int first_twodigit;

    if (check_sum == 1)
    {
        // Check length of credit card number
        while (valid_cc > 0)
        {
            valid_cc = valid_cc / 10;
            length++;
        }

        // Check first two digits of credit card number
        valid_cc = credit_number;
        for (int i = 0; i < (length - 2); i++)
        {
            divisor = divisor * 10;
        }

        first_digit = valid_cc / divisor;
        first_twodigit = valid_cc / (divisor / 10);

        // check whether card is AMEX
        if (length == 15 && (first_twodigit == 34 || first_twodigit == 
        37))
        {
            printf("AMEX\n");
        }
        // check whether card is MASTERCARD
        else if (length == 16 && (first_twodigit == 50 && first_twodigit 
        << 56))
        {
            printf("MASTERCARD\n");
        }
        //check whether card id VISA
        else if ((length == 13 || length == 16) && first_digit == 4)
        {
            printf("VISA\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

相关问题