gpt4 book ai didi

c - 用 C 语言编写一个程序,应用 Luhn 算法进行信用卡验证

转载 作者:行者123 更新时间:2023-11-30 20:35:14 29 4
gpt4 key购买 nike

我需要我的程序提示用户输入并重新提示,以防输入不遵循信用卡格式(例如:负数或字母等),然后应用算法来查看是否number 是有效的信用卡号码,如果是的话,是 Visa、MasterCard 还是 AmEx。

我知道这个问题已经在这个网站上用不同的代码回答了,我发誓我读了我可能找到的所有内容(在这个网站和网上的其他地方),但我真的很难理解 C语法,我想尝试自己想出一些东西,而不是复制我从其他答案中不理解的代码。如果有人可以帮助我,看看我到目前为止所做的事情并告诉我我做错了什么,我将非常感激。另外,任何可以帮助我更好地理解 C 语法逻辑的提示都将非常感激。

我的程序正在编译,但是当我运行它时,它会以一种非常奇怪的方式运行:当我输入输入时,有时它会说它无效(即使它是一个有效的数字),有时它不会返回任何内容按下回车键后,无论按多少次回车键都不会停止运行。

这是迄今为止我的代码:


#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
printf("Please give me your credit card number:\n") ;

long long card_num ;

do
{
card_num = GetLongLong() ;
}
while (card_num < 1 || card_num > 9999999999999999) ;

// Make a copy of the card number to be used and modified throughout the process.

long long temp_num = card_num ;
int digit = 0 ;
int count = 0 ;
int sum_a = 0 ;
int sum_b = 0 ;

// Isolate every digit from the credit card number using a loop and the variable 'digit'.
// Keep track of the amount and position of each digit using variable 'count'.

while (card_num >= 0)
{
digit = card_num % 10 ;
count++ ;
temp_num = (card_num - digit) / 10 ;
break ;


// Apply Luhn's algorithm using two different 'for' loops depending on the position of each digit.

for (count = 0 ; count % 2 == 0 ; count++)
{
sum_a = sum_a + ((card_num % 10) * 2) ;

while ((card_num % 10) * 2 >= 10)
{
sum_a = (sum_a % 10) + 1 ;
}
}

for (count = 0 ; count % 2 != 0 ; count++)
{
sum_b = sum_b + digit ;
}

return sum_a ;
return sum_b ;
return count ;
}

// Checking the validity of the number according to Luhn's algorithm

int total_sum = sum_a + sum_b ;

if (total_sum % 10 != 0)
{
printf("This is an invalid number.\n") ;
}

// If the number entered doesn't have the right amount of digits according
// to variable 'count', declare the number as invalid.

if (count != 13 || count != 15 || count != 16)
{
printf("This is an invalid number.\n") ;
}

// Reset value of variable 'temp_num' and apply calculations that will isolate the first two digits.
// Store the results in a variable 'company_id'.

temp_num = card_num ;
int company_id ;

while (temp_num > 100)
{
temp_num = card_num - (card_num % 10) ;
company_id = temp_num / 10 ;
}

return company_id ;

// Print the type of credit card depending on the company ID and amount of digits.

if (company_id > 50 && company_id < 56 && count == 16)
{
printf("MASTERCARD\n") ;
}
else if ((company_id == 4) && (count == 13 || count == 16))
{
printf("VISA\n") ;
}
else if ((company_id == 34 || company_id == 37) && (count == 15))
{
printf("AMEX\n") ;
}
else
{
printf("This is an invalid number.\n") ;
}

return 0 ;

}

最佳答案

您的答案是无序的模仿,其中的部分与之前的内容不符合逻辑。

具体问题:

这个逻辑:

if (count != 13 || count != 15 || count != 16)

使每张卡无效, (||) 应为 (&&) 才能生效。

这个循环没有意义:

while (card_num >= 0)
{
digit = card_num % 10 ;
count++ ;
temp_num = (card_num - digit) / 10 ;
break ;
...
}

break 是无条件的,因此它退出循环并忽略接下来的二十行。

当您调用 return 五次时,您似乎从其他地方拼接了子例程,只有最后一次有效:

return sum_a ;
return sum_b ;
return count ;
return company_id ;
return 0 ;

在一些地方,当您应该使用 temp_num 时却使用了 card_num

一旦您知道该卡无效,您就无法退出程序,而是继续测试。您未能确认卡何时有效。

您计算卡号中的位数,但等到运行其他检查后才测试该位数计数是否有效。

以下是我对您的代码进行的修改,以解决上述问题和一些样式问题:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
printf("Please give me your credit card number: ") ;

long long card_num = 0LL;

while (card_num < 1LL || card_num > 9999999999999999LL)
{
card_num = GetLongLong();
}

// Make a copy of the card number to be used and modified throughout the process.

long long temp_num = card_num;

// Isolate every digit from the credit card number using a loop and the variable 'digit'.
// Keep track of the amount and position of each digit using variable 'count'.

int count = 0;

while (temp_num > 0LL)
{
temp_num = temp_num / 10LL;
count++;
}

// If the number entered doesn't have the right amount of digits according
// to variable 'count', declare the number as invalid.

if (count != 13 && count != 15 && count != 16)
{
printf("This is an invalid number (# of digits).\n");
return 1;
}

// Reset value of variable 'temp_num' and apply calculations that will isolate the first two digits.
// Store the results in a variable 'company_id'.

temp_num = card_num;

while (temp_num > 100LL)
{
temp_num = temp_num / 10LL;
}

int company_id = temp_num;

// Print the type of credit card depending on the company ID and amount of digits.

if (company_id > 50 && company_id < 56 && count == 16)
{
printf("MASTERCARD\n") ;
}
else if ((company_id == 34 || company_id == 37) && (count == 15))
{
printf("AMEX\n") ;
}
else if ((company_id / 10 == 4) && (count == 13 || count == 16 || count == 19))
{
printf("VISA\n") ;
}
else
{
printf("This card was issued by an unknown company.\n");
}

// Apply Luhn's algorithm.

int sum = 0;

temp_num = card_num;

for (int i = 1; i <= count; i++)
{
int digit = temp_num % 10LL;

if (i % 2 == 0)
{
digit *= 2;

if (digit > 9)
{
digit -= 9;
}
}

sum += digit;

temp_num /= 10LL;
}

// Checking the validity of the number according to Luhn's algorithm

if (sum % 10 != 0)
{
printf("This is an invalid number (Luhn's algorithm).\n");
return 1;
}

printf("This is a valid number.\n");

return 0;
}

这不是一个完成的程序——还需要进行错误检查和其他细节。当加倍的卡号大于 9 时,我没有将数字相加,而是使用了更简单的方法,即减去 9。

关于c - 用 C 语言编写一个程序,应用 Luhn 算法进行信用卡验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005990/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com