我目前正在参加CS50在线课程,并且在第二学分上陷入了困境。目的基本上是提供一个信用卡号(或实际上的任何数字)作为输入,并获得无效或三种主要信用卡(万事达卡,维萨卡或美国运通卡)之一的输出。
我的代码几乎按照我的意图工作,因为我得到了正确的输出,我的问题是它的格式错误。
如果我输入的是非信用卡号而不是
无效
我的输出如下:
无效 无效 无效 无效 无效
如果我输入AmEx信用卡号作为输入,我的输出将如下所示:
无效 美国运通卡 无效 无效 无效
这是我的代码:
int main(void)
{
long long CreditCardNo;
do
{
CreditCardNo = get_long_long("Enter credit card number:");
}
while (CreditCardNo <= 0);
int countDigits = 0;
long long count = CreditCardNo;
//run loop until number is no longer an integer
while (count != 0)
{
//Incremental counter
countDigits++;
//Divide input number by 10 to reduce one digit from integer
count = count / 10;
}
//return "Invalid" for all numbers with any amount of digits other than 13, 15 & 16
if (countDigits != 13 && countDigits != 15 && countDigits != 16)
{
printf("INVALID\n");
}
//Take 2nd to last digit, and then the every other digit, and multiply by 2 to get the first part for the checksum
int digit1 = (((CreditCardNo / 10) % 10) * 2);
int digit2 = (((CreditCardNo / 1000) % 10) * 2);
int digit3 = (((CreditCardNo / 100000) % 10) * 2);
int digit4 = (((CreditCardNo / 10000000) % 10) * 2);
int digit5 = (((CreditCardNo / 1000000000) % 10) * 2);
int digit6 = (((CreditCardNo / 100000000000) % 10) * 2);
int digit7 = (((CreditCardNo / 10000000000000) % 10) * 2);
int digit8 = (((CreditCardNo / 1000000000000000) % 10) * 2);
//Take last digit and then every other to get the second part for the checksum
int digit9 = (CreditCardNo % 10);
int digit10 = ((CreditCardNo % 1000) / 100);
int digit11 = ((CreditCardNo % 100000) / 10000);
int digit12 = ((CreditCardNo % 10000000) / 1000000);
int digit13 = ((CreditCardNo % 1000000000) / 100000000);
int digit14 = ((CreditCardNo % 100000000000) / 10000000000);
int digit15 = ((CreditCardNo % 10000000000000) / 1000000000000);
int digit16 = ((CreditCardNo % 1000000000000000) / 100000000000000);
//Add all digits for first checksum
int check1 = digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7 + digit8;
//Add all digits for second checksum
int check2 = digit9 + digit10 + digit11 + digit12 + digit13 + digit14 + digit15 + digit16;
//Add checksums
int check3 = check1 + check2;
//If checksum doesn't end in 0 return invalid
if (check3 % 10 != 0)
{
printf("INVALID\n");
}
//Validation for AmEx
int CheckAmex = CreditCardNo / 10000000000000;
if (CheckAmex != 34 && CheckAmex != 37)
{
printf("INVALID\n");
}
else
{
printf("AMEX\n");
}
//Validation for MasterCard
int CheckMasterCard = CreditCardNo / 100000000000000;
if (CheckMasterCard != 51 && CheckMasterCard != 52 && CheckMasterCard != 53 && CheckMasterCard != 54 && CheckMasterCard != 55)
{
printf("INVALID\n");
}
else
{
printf("AMEX\n");
}
//Validation for Visa
int CheckVisa1 = CreditCardNo / 1000000000000;
if (CheckVisa1 != 4)
{
printf("INVALID\n");
}
else
{
printf("Visa\n");
}
int CheckVisa2 = CreditCardNo / 1000000000000000;
if (CheckVisa1 != 4)
{
printf("INVALID\n");
}
else
{
printf("Visa\n");
}
}
我尝试将第一个if公式重新包装到else中,然后将其他包装成第一个,但是我似乎无法使其正常工作,因为我最终遇到了“未声明的标识符”错误“ CheckMasterCard”(或Visa或其他任何东西)。
我是否缺少一个简单的解决方案并且可以轻松实现,还是需要对整个过程进行重新设计?
答案 0 :(得分:0)
这是因为程序检查了先前的if
语句,并在每种无效情况下打印“无效”,直到到达要执行的语句为止。例如,“ AMEX”会显示签证,万事达卡和您的支票“无效”。