我对编程非常陌生,因此,请帮助我解决Luhn的算法问题。一切 可以正常工作,但签证为411111111111111。实际上是什么导致了错误?我知道复制 粘贴被认为是错误的代码,但我试图至少创建有效的代码。你能帮助我吗 改善此代码?感谢您的帮助。
---------------------------------------------
#include <cs50.h>
#include <stdio.h>
int main ()
{ // 1
long cardnumber;
int digits = 0;
int even, odd;
// Getting credit card number
do
{
cardnumber = get_long("Enter your card number:\n");
}
while (cardnumber < 0);
// calculates the total digits in card
long digitcounter = cardnumber;
while(digitcounter > 0)
{
digitcounter = digitcounter / 10;
digits++;
}
if (digits != 13 && digits != 15 && digits != 16)
{ // 2-2
printf("INVALID");
} // 2-2
else
{ // 2
// Breaks down the credit card number into individual integers
int digit1 = (cardnumber / 1000000000000000);
int digit2 = (cardnumber / 100000000000000) % 10;
int digit3 = (cardnumber / 10000000000000) % 10;
int digit4 = (cardnumber / 1000000000000) % 10;
int digit5 = (cardnumber / 100000000000) % 10;
int digit6 = (cardnumber / 10000000000) % 10;
int digit7 = (cardnumber / 1000000000) % 10;
int digit8 = (cardnumber / 100000000) % 10;
int digit9 = (cardnumber / 10000000) % 10;
int digit10 = (cardnumber / 1000000) % 10;
int digit11 = (cardnumber / 100000) % 10;
int digit12 = (cardnumber / 10000) % 10;
int digit13 = (cardnumber / 1000) % 10;
int digit14 = (cardnumber / 100) % 10;
int digit15 = (cardnumber / 10) % 10;
int digit16 = (cardnumber % 10);
// multiplies 2 to every odd number
int multi_odd15 = (digit15 * 2);
while (multi_odd15 > 9)
{
int check1 = multi_odd15 % 10;
int check2 = multi_odd15 / 10;
multi_odd15 = check1 + check2;
}
int multi_odd13 = (digit13 * 2);
while (multi_odd13 > 9)
{
int check1 = multi_odd13 % 10;
int check2 = multi_odd13 / 10;
multi_odd13 = check1 + check2;
}
int multi_odd11 = (digit11 * 2);
while (multi_odd11 > 9)
{
int check1 = multi_odd11 % 10;
int check2 = multi_odd11 / 10;
multi_odd11 = check1 + check2;
}
int multi_odd9 = (digit9 * 2);
while (multi_odd9 > 9)
{
int check1 = multi_odd9 % 10;
int check2 = multi_odd9 / 10;
multi_odd9 = check1 + check2;
}
int multi_odd7 = (digit7 * 2);
while (multi_odd7 > 9)
{
int check1 = multi_odd7 % 10;
int check2 = multi_odd7 / 10;
multi_odd7 = check1 + check2;
}
int multi_odd5 = (digit5 * 2);
while (multi_odd5 > 9)
{
int check1 = multi_odd5 % 10;
int check2 = multi_odd5 / 10;
multi_odd5 = check1 + check2;
}
int multi_odd3 = (digit3 * 2);
while (multi_odd3 > 9)
{
int check1 = multi_odd3 % 10;
int check2 = multi_odd3 / 10;
multi_odd3 = check1 + check2;
}
int multi_odd1 = (digit1 * 2);
while (multi_odd1 > 9)
{
int check1 = multi_odd1 % 10;
int check2 = multi_odd1 / 10;
multi_odd1 = check1 + check2;
}
//sum of odd number
odd = (multi_odd15 + multi_odd13 + multi_odd11 + multi_odd9 + multi_odd7 + multi_odd5 + multi_odd3
+ multi_odd1);
// Sum of all the even numbers
even = (digit2 + digit4 + digit6 + digit8 + digit10 + digit12 + digit14 + digit16);
// Code to check if the card number is possible
if ((odd + even) % 10 == 0)
{ // 3
// If the card number is 13 digits
if (digits == 13)
{ // 4
int checkvisa1 = cardnumber / 1000000000000;
if (checkvisa1 == 4)
{
printf("Visa\n");
}
else
{
printf("INVALID\n");
}
} // 4
} //3
// If the card number is 15 digits
else if (digits == 15)
{ // 5
int checkamex = cardnumber / 10000000000000;
if (checkamex == 34 && checkamex == 37)
{
printf("American express\n");
}
else
{
printf("INVALID\n");
}
} // 5
// If the card number is 16 digits
else
{ // 6
int checkmaster = cardnumber / 100000000000000;
int checkvisa2 = cardnumber / 1000000000000000;
if (checkmaster <= 55 && checkmaster > 50)
{
printf("Mastercard\n");
}
else if (checkvisa2 == 4)
{
printf("Visa\n");
}
else
{
printf("INVALID\n");
}
} // 6
} // 2
} // 1
答案 0 :(得分:1)
仅当 else if (digits == 15)
为false时,才会执行此if ((odd + even) % 10 == 0)
。那可能不是您想到的流量控制。更有可能应该与此if (digits == 13)
“配对”。
当此if ((odd + even) % 10 == 0)
为true时,下一个测试为if (digits == 13)
,在4111111111111111
示例中为false,因此不创建任何输出,并使用该块完成。 else if
和下一个else
块不会执行(因为luhn's test评估为true),所以程序只是...结束。