gpt4 book ai didi

c - 为什么 VISA (4111111111111111) 不返回任何内容?

转载 作者:行者123 更新时间:2023-11-30 19:00:23 25 4
gpt4 key购买 nike

我对编程非常陌生,所以请帮我解决这个 Luhn 算法问题。一切 除了 Visa 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

最佳答案

这个else if (digits == 15)仅在时执行 if ((odd + Even) % 10 == 0)是假的。这可能不是您想要的流量控制。它更有可能与此 if (digits == 13)“配对”。

当此 if ((odd + Even) % 10 == 0) 为 true 时,下一个测试为 if (digits == 13),为 false在 4111111111111111 示例中,因此不会创建任何输出,并且是通过 block 完成的。 else if 和接下来的 else block 不会被执行(因为 luhn 的测试评估为 true),因此程序只是......结束。

关于c - 为什么 VISA (4111111111111111) 不返回任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59911688/

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