在C - buggy代码

时间:2018-04-11 13:25:09

标签: c algorithm debugging cs50 luhn

我目前正在完成CS50x的2018计划,我正在完成pset1的'信用'部分。我们应该执行Luhn的算法,但我不能让它工作。

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

而不是这个,我的代码只是在提交一个数字之后运行直到溢出。我知道我的逻辑在某处有缺陷,但我不知道我哪里出错了。我的代码如下:

//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");
    }

}

如果我出错了,我会很感激。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

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;}

    }