C 对字符串的每个数字进行排序
我试图用C语言实现Luhn算法。我的问题是我的循环根本不起作用 我试着把它除以10,对不同变量中的数字进行排序。 对于下面示例中的编号3782246310005,我需要获得: 5+0+1+6+2+8+8+3=33(奇数总数) 然后我将所有其他数字乘以2(即0+0+6+8+4+4+14),然后将它们的所有数字相加:0+0+6+8+4+4+1+4=27(总偶数) 最后我把偶数和奇数相加,再除以10。 在本例中,33+27=60,因此我的下一个测试((60%10)!=0)通过 有人能告诉我为什么不显示“第53行工作”吗 编辑:我改变了一些事情,总奇数似乎是正确的,但总偶数却不是C 对字符串的每个数字进行排序,c,loops,cs50,luhn,C,Loops,Cs50,Luhn,我试图用C语言实现Luhn算法。我的问题是我的循环根本不起作用 我试着把它除以10,对不同变量中的数字进行排序。 对于下面示例中的编号3782246310005,我需要获得: 5+0+1+6+2+8+8+3=33(奇数总数) 然后我将所有其他数字乘以2(即0+0+6+8+4+4+14),然后将它们的所有数字相加:0+0+6+8+4+4+1+4=27(总偶数) 最后我把偶数和奇数相加,再除以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");
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");
}
}
这更令人满意,因为总奇数似乎是正确的,但我想知道我的总偶数公式出了什么问题
我运行了另一个测试(在使输出更清晰的同时获得偶数的每个值),我看到程序看到了偶数(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_有效性一个值,它仍然希望它等于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)
{
长度++;
信用卡号/=10;
}
返回长度;
}
char Luhn_支票(长信用卡号)
{
整数总偶数=0;
整数总奇数=0;
整数偶数=0;
printf(“第43行已工作\n”);
int奇数=0;
int Luhn_sum=0;
char Luhn_check=0;
char-Luhn_有效性=0;
printf(“第49行已执行”);
长支票数字基础=信用卡号;
对于(int i=1;检查数字基础>0;i++)
{
如果((i%2)>0)
{
奇数=(检查数字基础%10);
总奇数=总奇数+奇数;
}
其他的
{
偶数=(检查数字基础%10);
如果(偶数>=5)
{
printf(“%i>5\n”,偶数);
int testouille=((2*偶数)%10);
printf(“%i testouille\n”,testouille);
总偶数=总偶数+((2*偶数)%10)+1);
printf(“%i测试总偶数”,总偶数);
}
其他的
{
printf(“%i您正试图将long
放入int
中。这种情况下不适合。请尝试在第32行更改的:
for (long long check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)
检查数字基础=(int)(3782246310005ll)
在我的系统上可能是负数(-1293252491
),在这种情况下(检查数字基础%2)
将为负数。因此,如果您有长信用卡号
,您看不到整数检查数字基础=信用卡号
有问题吗?不要将其视为数字;将其视为包含数字的字符串。谢谢。它显示“我”第53行"有几次循环似乎有效。你知道我的Luhn算法出了什么问题吗?它告诉我我的卡无效。我将添加一个循环,以确保用户提供正数。@Nocxy你似乎对两个不同的概念使用了检查数字基础
。我想你需要为奇数/偶数检查添加一个单独的变量。你真的在你的方法中有一个概念上的错误。首先,像这样的数据应该被视为数字数组,这样就容易多了……我编辑了我的消息,向你展示了我现在得到的东西。@marcolz你是对的,我做了另一个变量,以便我的检查更容易实现。尝试使用(2*偶数)+1
而不是+10
total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 10 );
#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");
}
}
for (long long check_digit_basis=credit_card_number ; check_digit_basis > 0 ; check_digit_basis /= 10)