我正在尝试在C中实现Luhn的算法。我的问题是我的循环根本不起作用。
我尝试将其除以10以对不同变量中的数字进行排序。 对于下面示例中的数字378282246310005,我需要: 5 + 0 + 1 + 6 + 2 + 8 + 8 + 3 = 33(total_odd_numbers)。
然后我将所有其他的乘以2(这给我0 + 0 + 6 + 8 + 4 + 4 + 14)并添加所有数字:0 + 0 + 6 + 8 + 4 + 4 + 1 + 4 = 27(total_even_numbers)。
最后,我将偶数和奇数相加,并将总数除以10。 在这种情况下,33 + 27 = 60,所以我的下一次测试((60%10)!= 0)通过。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
for(int check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
{
if ((check_digit_basis % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i", even_number);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
}
}
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我得到:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
INVALID
有人能告诉我为什么“53号线工作”没有显示?
编辑:我改变了一些东西,total_odd_numbers似乎是正确的,但total_even_number不是。#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
printf("line 53 worked\n");
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
printf("%i total odd\n", total_odd_numbers);
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
printf("%i test total even\n", total_even_numbers);
}
else
{
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
我明白了:
Provide your credit card number:
378282246310005
15
Line 43 worked
line 49 executed
line 53 worked
5 total odd
0 test total even
line 53 worked
5 total odd
0 test total even
line 53 worked
6 total odd
6 test total even
line 53 worked
12 total odd
14 test total even
line 53 worked
14 total odd
18 test total even
line 53 worked
22 total odd
22 test total even
line 53 worked
30 total odd
36 test total even
line 53 worked
33 total odd
INVALID
这更令人满意,因为total_odd_numbers似乎是正确的,但我想知道我的total_even_number公式有什么问题。
我运行了另一个测试(为了获得每个单独的值even_number,同时使输出更清晰),我看到程序看到我的偶数(0,0,3,4,2,2,7)但是添加不是正确...
问题出在这一行:
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
我似乎无法将数字分成22 + 1 + 5而不是我得到的22 + 14。
编辑2:
经过几次测试后,我设法看到了问题所在。在这里,我看不出我做错了什么,因为我给Luhn_validity一个值,它仍然希望它等于60。
#include<stdio.h>
#include<cs50.h>
// Provides the length of credit card number
int length_ccn(long long credit_card_number)
{
int length = 0;
while (credit_card_number > 0)
{
length++;
credit_card_number /= 10 ;
}
return length;
}
char Luhn_check(long long credit_card_number)
{
int total_even_numbers = 0;
int total_odd_numbers = 0;
int even_number = 0;
printf("Line 43 worked\n");
int odd_number = 0;
int Luhn_sum = 0;
char Luhn_check = 0;
char Luhn_validity = 0;
printf("line 49 executed\n");
long long check_digit_basis = credit_card_number;
for(int i =1 ; check_digit_basis > 0 ; i++)
{
if ((i % 2) > 0)
{
odd_number = (check_digit_basis % 10);
total_odd_numbers = total_odd_numbers + odd_number;
}
else
{
even_number = (check_digit_basis % 10);
if(even_number >= 5)
{
printf("%i >5\n", even_number);
int testouille = (( 2 * even_number) % 10);
printf("%i testouille\n", testouille);
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 1 );
printf("%i test total even\n", total_even_numbers);
}
else
{
printf("%i <5\n", even_number);
total_even_numbers = total_even_numbers + (2 * even_number);
printf("%i test total even\n", total_even_numbers);
}
}
check_digit_basis = (check_digit_basis / 10);
}
Luhn_sum = total_even_numbers + total_odd_numbers;
printf("%i", Luhn_sum);
if ((Luhn_sum % 10 ) == 0 )
{
Luhn_validity = 1;
printf("%c\n", Luhn_validity);
}
else
{
Luhn_validity = 0;
}
return Luhn_check;
}
int main(void)
{
printf("Provide your credit card number:\n");
long long credit_card_number = get_long_long();
int length = length_ccn(credit_card_number);
printf("%i\n", length);
char Luhn_validity = Luhn_check(credit_card_number);
if(Luhn_validity)
{
printf("pass\n");
}
else
{
printf("INVALID\n");
}
}
提前致谢。
答案 0 :(得分:0)
您正尝试将long long
纳入int
。这不适合这种情况。尝试更改第32行的for
:
for (long long check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
我的系统上的 check_digit_basis = (int)(378282246310005LL)
可能是否定的(-1293252491
),在这种情况下(check_digit_basis % 2)
将为负数。