用户定义的函数为什么会重复两次?

huangapple go评论48阅读模式
英文:

Why is my user defined function repeating twice?

问题

I am programming a piece of code for the credit problem in problem set 1 of CS50. This code already works, but during some debugging I noticed that my first function, which checks whether a credit card number complies with Luhn's algorithm, was repeating twice. The program still checks card numbers successfully, but I want to understand why the function is repeating twice.

The function is supposed to run via updating a sum variable with a new value as it combs through the credit card number entered, executing each step in Luhn's algorithm. It then returns a return value of either 0 or 1 to be processed by the next function, which prints the card type, to determine if the card number complies with the algorithm. Instead, the function runs twice, producing the same sum value twice, before the next function runs. I have tried playing around with the format of how I have structured my functions, as well as how the first function returns, but I have not been able to determine why it is repeating.

英文:

I am programming a piece of code for the credit problem in problem set 1 of CS50. This code already works, but during some debugging I noticed that my first function, which checks whether a credit card number complies with Luhn's algorithm, was repeating twice. The program still checks card numbers successfully, but I want to understand why the function is repeating twice.

The function is supposed to run via updating a sum variable with a new value as it combs through the credit card number entered, executing each step in Luhn's algorithm. It then returns a return value of either 0 or 1 to be processed by the next function, which prints the card type, to determine if the card number complies with the algorithm. Instead the function runs twice, producing the same sum value twice, before the next function runs. I have tried playing around with the format of how I have structured my functions, as well as how the first function returns, but I have not been able to determine why it is repeating.

//prompts and checks for valid credit card number
#include <cs50.h>
#include <stdio.h>

int checksum(long num);
void digit_checker(long num);

int main(void)
{
    //prompts for input
    long card_number = get_long("Number: ");
    checksum(card_number);
    digit_checker(card_number);
}

//creates checksum fuction
int checksum(long num)
{
    long digit_div1 = 100, digit_div2 = 10, digit_div3 = 10, digit_div4 = 1, digit_add = 0, num_i = 0, num_j = 0;
    int sum = 0;
    do
    {
        // seperates digits and mutiplies by 2
        num_i = num % digit_div1;
        num_i -= num_i % digit_div2;
        num_i = num_i / digit_div2;
        digit_add = num_i * 2;

        // adds digits of numbers
        if (digit_add > 9)
        {
            long pre_sum = digit_add - digit_add % 10;
            sum += pre_sum / 10;
            sum += digit_add % 10;
        }
        else
        {
            sum += digit_add;
        }

        // reassigns variable values to target next digit
        digit_div1 = digit_div1 * 100;
        digit_div2 = digit_div2 * 100;
    }
    while (digit_div1 / 10 < num);

    do
    {
        // adds other digits
        num_j = num % digit_div3;
        num_j -= num_j % digit_div4;
        num_j = num_j / digit_div4;
        sum += num_j;

        //reassigns variable values
        digit_div3 = digit_div3 * 100;
        digit_div4 = digit_div4 * 100;
    }
    while (digit_div3 / 10 < num);

    //checks and prints if valid
    if (sum % 10 == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void digit_checker(long num)
{
    if (!checksum(num))
    {
        //did not pass sum check
        printf("INVALID\n");
        return;
    }
    int len = 0;
    int first_dig = 0, sec_dig = 0;
    //get first digits and number length
    while (num != 0)
    {
        sec_dig = first_dig;
        first_dig = num % 10;
        num /= 10;
        len++;
    }
    //length & digit checker
    if (len == 13 && first_dig == 4)
    {
        printf("VISA\n");
    }
    else if (len == 16)
    {
        if (first_dig == 4)
        {
            printf("VISA\n");
        }
        else if (first_dig == 5)
        {
            if (sec_dig == 1 || sec_dig == 2 || sec_dig == 3 || sec_dig == 4 || sec_dig == 5)
            {
                printf("MASTERCARD\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else
        {
            printf("INVALID\n");
        }

    }
    else if (len == 15 && first_dig == 3)
    {
        if (sec_dig == 4 || sec_dig == 7)
        {
            printf("AMEX\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        //did not pass digit check
        printf("INVALID\n");
    }
    return;
}

答案1

得分: 3

The function digit_checker calls the function checksum

void digit_checker(long num)
{
    if (!checksum(num))
    //...

one more time,:)

checksum(card_number);
digit_checker(card_number);

It seems you need to remove this statement

checksum(card_number);

It has no effect.

英文:

The function digit_checker calls the function checksum

void digit_checker(long num)
{
    if (!checksum(num))
    //...

one more time,:)

checksum(card_number);
digit_checker(card_number);

It seems you need to remove this statement

checksum(card_number);

It has no effect.

huangapple
  • 本文由 发表于 2023年5月14日 04:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定