我有这个 Luhn 算法的 C 代码。通过测试一些信用卡号码 运行 没问题,但是代码检查给了我这些错误。为什么?
I have this C code for Luhn’s Algorithm. it is running fine by testing some credit card numbers, but the code checking gives me these errors. Why?
我有 Luhn 算法的 C 代码。通过测试一些信用卡号码 运行 没问题,但是代码检查给了我这些错误。为什么?
当 运行 代码时,所有信用卡号码都被正确检查,但代码检查是这样说的:
应为“xxxx”,而不是“”,如下所示。
文件名为credittest.c
代码如下
the error check gives these errors below:
~/pset1/credit/ $ check50 cs50/problems/2020/x/credit
Connecting.....
Authenticating....
Verifying.....
Preparing.....
Uploading............
Waiting for results...........................
Results for cs50/problems/2020/x/credit generated by check50 v3.1.2
:) credit.c exists
:) credit.c compiles
:( identifies 378282246310005 as AMEX
expected "AMEX\n", not ""
:( identifies 371449635398431 as AMEX
expected "AMEX\n", not ""
:( identifies 5555555555554444 as MASTERCARD
expected "MASTERCARD\n", not ""
:( identifies 5105105105105100 as MASTERCARD
expected "MASTERCARD\n", not ""
:( identifies 4111111111111111 as VISA
expected "VISA\n", not ""
:( identifies 4012888888881881 as VISA
expected "VISA\n", not ""
:( identifies 1234567890 as INVALID
expected "INVALID\n", not ""
:( identifies 369421438430814 as INVALID
expected "INVALID\n", not ""
:( identifies 4062901840 as INVALID
expected "INVALID\n", not ""
:( identifies 5673598276138003 as INVALID
expected "INVALID\n", not ""
:( identifies 4111111111111113 as INVALID
expected "INVALID\n", not ""
下面是代码:
//prompts the user for a credit card number and then reports (via printf) whether
//it is a validity American Express, MASTERCARD, or Visa card number
//All American Express numbers 15-digit start with 34 or 37;
//most MASTERCARD numbers 16-digit start with 51, 52, 53, 54, or 55
///all Visa numbers 13- and 16-digit start with 4
#include<stdio.h>
#include<cs50.h>
#include<math.h>
bool validity(long cardnumber);
int find_length(long cardnumber);
bool checksum(long cardnumber);
void printcardbrand(long cardnumber);
int main(void)
{
long cardnumber;
do
{
cardnumber = get_long("What is the card number: ");
}
while (cardnumber < 0);
if (validity(cardnumber))
{
printcardbrand(cardnumber);
}
else
{
printf("INVALID\n\n");
}
}
bool validity(long cardnumber)
{
int len = find_length(cardnumber);
if((len == 13 || len == 16) && checksum(cardnumber) && ((cardnumber >= 4e12 && cardnumber < 5e12)|| (cardnumber >= 4e15 && cardnumber < 5e15)))
return true;
if(len == 16 && checksum(cardnumber) && (cardnumber >= 51e14 && cardnumber < 56e14))
return true;
if(len == 15 && checksum(cardnumber) && ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) ) )
return true;
else
return false;
}
int find_length(long cardnumber)
{
int len;
for (len = 0; cardnumber != 0; cardnumber /= 10)
{
len++;
}
return len;
}
bool checksum(long cardnumber)
{
int sum = 0;
for (int i = 0; cardnumber != 0; i++, cardnumber /=10)
{
if (i % 2 == 0)
{
sum = sum + cardnumber % 10; //get remainder, i.e. the even index digit
}
else
{
int digit = 2 * (cardnumber % 10);
sum = sum + digit / 10 + digit % 10;
}
}
return (sum % 10) == 0;
}
void printcardbrand(long cardnumber)
{
if ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) )
{
printf("AMEX\n\n");
}
else if ( (cardnumber >= 51e14 && cardnumber < 56e14) )
{
printf("MASTERCARD\n\n");
}
else if ( (cardnumber >= 4e12 && cardnumber < 5e12) || (cardnumber >= 4e15 && cardnumber < 5e15) )
{
printf("VISA\n\n");
}
}
// checksum, card length, starting digits will decide which card it is.
//last line of output be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n
``
你的 printf 调用说 "AMEX\n\n"
而它应该是 "AMEX\n"
check50 的措辞不是很好,从技术上讲,您按字面意思做了,但这不是他们的意思。 Check50 说期待 "AMEX\n"
但终端中的输出应该是 AMEX
全部单独在一行上。为此,您需要使用以下代码。
printf("AMEX\n")
显然所有其他信用卡都一样
我有 Luhn 算法的 C 代码。通过测试一些信用卡号码 运行 没问题,但是代码检查给了我这些错误。为什么?
当 运行 代码时,所有信用卡号码都被正确检查,但代码检查是这样说的: 应为“xxxx”,而不是“”,如下所示。
文件名为credittest.c
代码如下
the error check gives these errors below:
~/pset1/credit/ $ check50 cs50/problems/2020/x/credit
Connecting.....
Authenticating....
Verifying.....
Preparing.....
Uploading............
Waiting for results...........................
Results for cs50/problems/2020/x/credit generated by check50 v3.1.2
:) credit.c exists
:) credit.c compiles
:( identifies 378282246310005 as AMEX
expected "AMEX\n", not ""
:( identifies 371449635398431 as AMEX
expected "AMEX\n", not ""
:( identifies 5555555555554444 as MASTERCARD
expected "MASTERCARD\n", not ""
:( identifies 5105105105105100 as MASTERCARD
expected "MASTERCARD\n", not ""
:( identifies 4111111111111111 as VISA
expected "VISA\n", not ""
:( identifies 4012888888881881 as VISA
expected "VISA\n", not ""
:( identifies 1234567890 as INVALID
expected "INVALID\n", not ""
:( identifies 369421438430814 as INVALID
expected "INVALID\n", not ""
:( identifies 4062901840 as INVALID
expected "INVALID\n", not ""
:( identifies 5673598276138003 as INVALID
expected "INVALID\n", not ""
:( identifies 4111111111111113 as INVALID
expected "INVALID\n", not ""
下面是代码:
//prompts the user for a credit card number and then reports (via printf) whether
//it is a validity American Express, MASTERCARD, or Visa card number
//All American Express numbers 15-digit start with 34 or 37;
//most MASTERCARD numbers 16-digit start with 51, 52, 53, 54, or 55
///all Visa numbers 13- and 16-digit start with 4
#include<stdio.h>
#include<cs50.h>
#include<math.h>
bool validity(long cardnumber);
int find_length(long cardnumber);
bool checksum(long cardnumber);
void printcardbrand(long cardnumber);
int main(void)
{
long cardnumber;
do
{
cardnumber = get_long("What is the card number: ");
}
while (cardnumber < 0);
if (validity(cardnumber))
{
printcardbrand(cardnumber);
}
else
{
printf("INVALID\n\n");
}
}
bool validity(long cardnumber)
{
int len = find_length(cardnumber);
if((len == 13 || len == 16) && checksum(cardnumber) && ((cardnumber >= 4e12 && cardnumber < 5e12)|| (cardnumber >= 4e15 && cardnumber < 5e15)))
return true;
if(len == 16 && checksum(cardnumber) && (cardnumber >= 51e14 && cardnumber < 56e14))
return true;
if(len == 15 && checksum(cardnumber) && ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) ) )
return true;
else
return false;
}
int find_length(long cardnumber)
{
int len;
for (len = 0; cardnumber != 0; cardnumber /= 10)
{
len++;
}
return len;
}
bool checksum(long cardnumber)
{
int sum = 0;
for (int i = 0; cardnumber != 0; i++, cardnumber /=10)
{
if (i % 2 == 0)
{
sum = sum + cardnumber % 10; //get remainder, i.e. the even index digit
}
else
{
int digit = 2 * (cardnumber % 10);
sum = sum + digit / 10 + digit % 10;
}
}
return (sum % 10) == 0;
}
void printcardbrand(long cardnumber)
{
if ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) )
{
printf("AMEX\n\n");
}
else if ( (cardnumber >= 51e14 && cardnumber < 56e14) )
{
printf("MASTERCARD\n\n");
}
else if ( (cardnumber >= 4e12 && cardnumber < 5e12) || (cardnumber >= 4e15 && cardnumber < 5e15) )
{
printf("VISA\n\n");
}
}
// checksum, card length, starting digits will decide which card it is.
//last line of output be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n
``
你的 printf 调用说 "AMEX\n\n"
而它应该是 "AMEX\n"
check50 的措辞不是很好,从技术上讲,您按字面意思做了,但这不是他们的意思。 Check50 说期待 "AMEX\n"
但终端中的输出应该是 AMEX
全部单独在一行上。为此,您需要使用以下代码。
printf("AMEX\n")
显然所有其他信用卡都一样