gpt4 book ai didi

c - 即使满足条件,C 中的“if”语句也不会执行

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:01 24 4
gpt4 key购买 nike

作为 first assignment 的一部分,我是第一次尝试完成一个简单的命令行程序的程序员对于我正在参加的在线类(class),但我似乎遇到了 GDB 或我自己的研究无法解决的障碍。

经过几个小时的重写和调试,我终于得到了下面的代码来编译。该程序应该将信用卡号作为输入,然后根据作业规范检查它是否有效。我使用了这里的测试编号:PayPal Test Credit Cards

奇怪的是,当我输入 AMEX 卡号时,它会正确地生成文本“AMEX”,但是当我尝试使用 Visa 或 Master 卡时,它会打印“INVALID”。

在 GDB 中,我在 Verify 函数处中断,它似乎错误地跳过了这两个 if/else if 语句,而没有继续执行 Checksum 函数,即使似乎满足了条件。

if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.

...

else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.

...

正确执行的 AMEX 代码行是:

else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0)  // checks for a valid American Express.

所有三行的参数格式似乎完全相同。不过,这是我在 GDB 中所能达到的极限。在逐步执行上述两个非执行行之前,我会在 GDB 中打印 totalDigits、firstDigit 和 secondDigit,一切看起来都是正确的。所以我很困惑,为什么 AMEX 线路正在执行,而不是其他线路?

提前谢谢大家。这是继 hello.c 之后我尝试编写的第一个程序,因此如果看起来我在做一些奇怪/错误的事情,我绝对愿意接受任何批评或建议。

完整代码:

checker.c
#include <stdio.h>
#include <stdlib.h>



int MAX = 16;

int* DigitSort(unsigned long long x, int* array);
int Verify(int* array);


int main (void)
{

int* output = malloc (sizeof(int) * (MAX + 2)); // creates a blank array for the individual digits of the card number.
unsigned long long userInput = 0;

do
{
printf("Please enter a credit card number:\n");
scanf("%lld", &userInput);
}
while (userInput <= 0); // checks to make sure the user entered a number.


switch(Verify(DigitSort(userInput, output))) // sorts the user's input into individual digits and verifies the card type and validity.
{
case 1 :
printf("VISA\n");
break;
case 2 :
printf("MASTERCARD\n");
break;
case 3 :
printf("AMEX\n");
break;
case 0 :
printf("INVALID\n");
break;
default :
printf("INVALID\n");
}

free(output);
return 0;

}


int Verify(int* array) // verifies whether or not a card number is valid. Must pass the function a sorted array of individual digits.
{

int* cardNumber = array;
int firstDigit = cardNumber[0];
int secondDigit = cardNumber[1];
int totalDigits = 0;
int Checksum(int* cardNumber, int totalDigits);
int i = 0;

while (firstDigit >= 1 && cardNumber[i] >= 0) // this step counts the number of digits in the array.
{
totalDigits = totalDigits + 1;
i++;
}

if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
{
return 1;
}

else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
{
return 2;
}

else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
{
return 3;
}
else // if the card number doesn't match any of the above conditions or fails the checksum, an 'I' for Invalid is returned.
{
return 0;
}

}
int* DigitSort(unsigned long long x, int* array) // takes a long long as input and sorts it into individual digits
{
int* arrayReversed = malloc (sizeof(int) * (MAX + 2)); // creates a new array to hold the reversed order of digits.


int i = 0;
arrayReversed[0] = 0;

if (i < (MAX - 1) && x >= 10)
{
do
{
arrayReversed[i] = x % 10;
x = x / 10;
i++;
}
while (i < (MAX -1) && x >= 10);
}

if (i < MAX && x >= 1 && x <= 9)
{
arrayReversed[i] = (int) x;
x = (x - x);
}

if (x == 0)
{
int j = 0;

do
{
array[j] = arrayReversed[i]; // sorts the digits from the reversed array and places them into the sorted array.
j++;
i--;
}
while (j < MAX && i >= 0);

array[j] = -1;

}

free(arrayReversed);
return array;
}

int Checksum(int* cardNumber, int totalDigits)
{
int sum1 = 0;
int sum2 = 0;
int i = (totalDigits - 2);
int j = (totalDigits - 1);

while (i >= 0)
{
sum1 = ((cardNumber[i] * 2)%10) + ((cardNumber[i] * 2)/10) + sum1;
i -= 2;
}

while (j >= 0)
{
sum2 = (cardNumber[j] + sum2);
j -= 2;
}

if (((sum1 + sum2) % 10) == 0)
{
return 0;
}
else
{
return 1;
}
}

最佳答案

你的第一个问题在这里:

if (firstDigit == 4 && totalDigits == (13 | 16) && ...

你需要写:

if (firstDigit == 4 && (totalDigits == 13 || totalDigits == 16) && ...

您的第一个检查是寻找 0x1D == 29 作为位数(因为,正如 paisancocomment 中指出的那样,| 运算符是按位或运算符),并且没有信用卡需要 29 位数字(现在,而且在很长一段时间内都不会)。为了清楚和准确起见,请注意额外的括号。不要冒险删除它们 - 代码将无法再次正常工作。通常,如果您的条件同时具有 &&|| 运算符并使用括号明确地对术语进行分组,则要明确。

你在其他地方也有类似的问题。碰巧的是,(4 | 7)7 的值相同,因此条件在第二位数字为 7 时有效(但在为 4 时无效)。但这并不代表您想要表达的意思。

计算机语言与人类语言不同。习惯于更详细地写出条件。其他一些语言为这些条件提供了简写形式; C 不是这样的语言。

关于c - 即使满足条件,C 中的“if”语句也不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31062776/

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