在 C 中执行 Luhn 算法 - 错误代码

Excecuting Luhn's Algorithm in C - buggy code

我目前正在完成 CS50x 的 2018 计划,并且正在完成 pset1 的 'credit' 部分。我们应该执行 Luhn 的算法,但我无法让它工作。

Luhn 算法是一种使用校验和来确定信用卡是否有效的算法。输入应该是信用卡号,或者可以是信用卡号的东西。然后它取数字,将每隔一个数字乘以 2(或我的代码中隐含的每个偶数),对它们求和,然后取剩余数字(奇数)并将它们与第一个和相加。该算法然后指出,如果求和值的最后一位为 0,则该数字有效(或者如果它完美地除以 10)。如果答案不是 0,那么它应该向用户打印 "INVALID"。如果它是 0,那么代码应该分析该号码是否是一张有效的卡。我们应该使用 American Express、Visa 和 MasterCard 作为我们的潜在卡。如果是 AMEX,它将是 15 位数字,以 34 或 37 开头,MASTERCARD 是 16 位数字,以 51、52、53、54 或 55 开头,VISA 是 13 或 16 位数字,以 4 开头。预期输出是卡公司之一或 "INVALID" 如果号码不符合该标准。

我的代码只是简单地运行,直到在提交数字后溢出。我知道我的逻辑一定有问题,但我不知道我哪里出错了。我的代码如下:

//Checks credit card no. to see if valid
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    //Get card no. from user
    long long n;
    do
    {
        n = get_long_long("Card Number: ");
    }
    while (n < 0);

    //Define variables for checksum process
    int odd = 0;
    int even = 0;
    long long temp_n = n;
    int sum_a = 0;
    int sum_b = 0;
    int counter = 0;

    //Execute checksum until all of n assessed
    while (temp_n >= 0)
    {
        //Take final digit, add up, then update temp no., increase digit counter by 1
        odd = temp_n % 10;
        sum_b = sum_b + odd;
        temp_n = (temp_n - odd) / 10;
        counter++;

        //Take final digit (which is an even digit of n), multiply by 2 and add up, update temp no., increase counter by 1
        even = temp_n % 10;
        sum_a = sum_a + 2 * even;
        temp_n = (temp_n - even) / 10;
        counter++;

    }

    //Validate checksum
    int test = (sum_a + sum_b) % 10;

    //Return results
    if (test == 0)
    {
        if (counter == 16 && odd == 5 && (even == 1 || even == 2 || even == 3 || even == 4 || even == 5))
        {
            printf("MASTERCARD\n");
        }
        else if ((counter == 16 || counter == 13) && odd == 4)
        {
            printf("VISA\n");
        }
        else if (counter == 15 && odd == 3 && (even == 4 || even == 7))
        {
            printf("AMEX\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }

}

如果我能就我出错的地方提供任何帮助,我将不胜感激。提前谢谢你。

while (temp_n >= 0)
    {
        //Take final digit, add up, then update temp no., increase digit counter by 1
        odd = temp_n % 10;
        sum_b = sum_b + odd;
        temp_n = (temp_n - odd) / 10;
        counter++;

        //Take final digit (which is an even digit of n), multiply by 2 and add up, update temp no., increase counter by 1
        even = temp_n % 10;
        sum_a = sum_a + 2 * even;
        temp_n = (temp_n - even) / 10;
        counter++;

    }

这假设输入总是有偶数位。取一个 bool flag = true; 并将其更改为:

while (temp_n >= 0)
    {
        //Take final digit, add up, then update temp no., increase digit counter by 1
      if(flag){
        odd = temp_n % 10;
        sum_b = sum_b + odd;
        temp_n = (temp_n - odd) / 10;
        counter++;
        flag = !flag;}

        //Take final digit (which is an even digit of n), multiply by 2 and add up, update temp no., increase counter by 1
      else{
        even = temp_n % 10;
        sum_a = sum_a + 2 * even;
        temp_n = (temp_n - even) / 10;
        counter++;
        flag = !flag;}

    }