- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要我的程序提示用户输入并重新提示,以防输入不遵循信用卡格式(例如:负数或字母等),然后应用算法来查看是否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/
我想了解其中的逻辑,以便我可以在 java 中实现这个算法。我想计算有效的十六进制 IMEI 号码的校验位。例如 - 6C4BFFC0000004请帮我算法。我试图在谷歌中找到解决方案,但我无法使用这
enter image description here 谁能解释一下这个程序。这个程序是我们老师给出的,他说这是关于 luhn 算法的,我找到了它是什么,但我无法理解该程序。 最佳答案 读完维基百科
大家好,我是编程界的新手。对于学校练习题,我得到了以下文本,我想将其转换为代码。我已经花了几个小时在上面,但似乎仍然无法弄清楚,但我决心学习这个。我目前遇到错误 line 7, in if i
我对编程非常陌生。我根据以下说明实现了信用卡验证程序。 让输入成为输入。 反向输入。 将输入的所有奇数位置(即索引 1、3、5 等)乘以 2。如果这些相乘项中的任何一项大于 9,则减去 9。 将输入的
排除校验位,luhn 算法的最小长度是多少? 我的想法是,它适用于任何大于 2 位的数字(同样,不包括校验位)。 我问的原因是:如果我从右到左遍历数字中的所有数字。这会导致我的 luhn 验证中的 i
在我的应用程序中,我想检查用户是否输入了我使用 LUHN 算法的有效卡号。我已将其创建为方法并在主 Activity 中调用。但即使我提供有效的卡号,它也会显示无效。在输入卡号时,我在中间留了空格,我
一切看起来都很好,似乎遵循 Luhn 的算法,但是当我输入我自己的信用卡号或这个应该有效的样本号:4388576018410707 时,它仍然无效... 谁能找到问题所在? #include int
我正在尝试用 JAVA 制作一个应用程序来检查用户输入的信用卡号是否有效。 以下是模数 10 检查的步骤: 第一步:从右到左每隔两个数字加倍。如果数字加倍得到两位数,则将这两位数相加得到一位数。 第
我一直在试图找出验证信用卡的 Luhns 方法,但我似乎无法弄清楚。我需要使用方法而不能使用数组,所以我完全被难住了。 这是 Luhns 检查: 信用卡号必须包含 13 到 16 位数字。它必须从以下
它应该使用 luhn 检查来告诉我一张卡是有效还是无效4388576018402626 无效4388576018410707 有效但它一直告诉我一切都是无效的:/任何关于做什么或去哪里寻找的建议都会很
我昨天完成了将这些函数组合在一起,它基本上是 Luhn 算法的实现。第一个函数应该接受一个整数和数组作为输入,然后将整数拆分为数字以填充数组。第二个应该检查数字是否有效并返回 1 或 0。我的问题是,
我希望有人能帮助我解决这个问题。我是一个彻底的 C 新手。 这是关于 C 类(class)的学校作业(只是普通的旧 C,而不是 C# 或 C++),教授坚持认为我们唯一允许使用的编译器是 Borlan
我有这段代码,由于某些原因,我在 for 循环的每次迭代中都会得到一个非常大的数字,但我无法弄清楚其中的逻辑。 #include #include #include #include int
我使用以下脚本来验证在表单中输入的卡详细信息。我想为此添加一个功能,以便提醒使用激光卡的访客我们不接受它们。 Laser的起始数字是6304,6706,6771&6709 function Calcu
我正在尝试使用此代码在 Java 中测试示例代码是否为有效的信用卡号(使用 Luhn algorithm )。我哪里做错了?它接受一个由 16 个一位数组成的数组。任何帮助将非常感激。谢谢! priv
我试图让某人输入信用卡号并通过 Luhn Check 验证它是否有效。我希望能够检查他们是否将整个卡号作为一个大字符串输入,或者是否在其中放置了空格。在我的函数验证中,尽管我不断收到一条错误消息,指出
我编写了一个工作代码来使用 luhn 算法检查信用卡是否有效: class CreditCard def initialize(num) @@num_arr = num.to_s.spli
我有一个 luhn 算法,我正在尝试按照维基百科上的算法步骤进行操作,并且它适用于他们提供的示例。我认为这是正确的。但它不适用于我的任何个人卡。也没有我在寻找解决方案时发现的任何测试值。 我已经看到使
我是一名新的编程学生,我正在尝试用 Java 编写一个算法来执行以下操作: 计算信用卡号的每隔一个数字(从倒数第二个数字开始)并将每个数字乘以 2。 取结果整数并将所有数字加在一起(即,如果我得到 1
我正在尝试实现 Luhn 算法来检查信用卡号的有效性。为此,每隔一个数字需要乘以 2,如果结果 >9,则将其替换为数字之和。 def luhn_check(creditcardnr): """ che
我是一名优秀的程序员,十分优秀!