我定义的条件循环不按预期运行是否正常?
Is it normal that my defined condition loop does not act like expected?
刚开始哈佛的CS50课程。我目前正在处理问题集 1,即验证信用卡号。为什么我的条件循环没有像预期的那样运行?例如,当我输入信用卡号 len 16 并且不符合给定的条件(例如:59e14)时,它仍然会打印出“MASTERCARD”?
# include <stdio.h>
# include <cs50.h>
# include <math.h>
// Declaration of the functions
long get_number(void);
//const char * check_len(long cc);
void check_len(long cc);
// Main program
int main(void)
{
int i;
long credit_number = get_number();
printf("Validation for %ld\n", credit_number);
check_len(credit_number);
}
// Function that prompt the user for a credit card number
long get_number(void)
{
// Declaration of the credit card variable
long number;
// Ask the user for a credit card number. If it's not none-negative, keep asking
do
{
number = get_long("Enter credit card number: ");
}
while (number <=0);
return number;
}
// Function to check the type of credit card
void check_len(long cc)
{
// Declaration of variables
int i;
long number = cc;
// Count the len of the provided credit card number
for (i = 0; cc != 0; i++)
{
cc = cc/10;
}
printf("%i\n", i);
// Validate the type of card
if (i == 15)
{
if ( (number >= 34e13 && number < 35e13) || (number >= 37e13 && number < 38e13) )
printf("AMEX\n");
else
{
printf("INVALID\n");
}
}
else if (i == 13 || i == 16)
{
if ( (number >= 51e14 || number < 56e14) )
{
printf("MASTERCARD\n");
}
else if ( (number >= 4e12 || number < 5e12) || (number >= 4e15 || number <= 5e15))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
给定
if ( (number >= 51e14 || number < 56e14) )
...
51e14
小于 56e14
,所以 每个 实数要么大于 51e14
要么小于 56e14
.
如果您只想要该范围内的数字,则需要使用 &&
以便满足两个条件:
if ( ( number >= 51e14 ) && ( number < 56e14 ) )
...
你的其他情况看起来也有类似的问题。
刚开始哈佛的CS50课程。我目前正在处理问题集 1,即验证信用卡号。为什么我的条件循环没有像预期的那样运行?例如,当我输入信用卡号 len 16 并且不符合给定的条件(例如:59e14)时,它仍然会打印出“MASTERCARD”?
# include <stdio.h>
# include <cs50.h>
# include <math.h>
// Declaration of the functions
long get_number(void);
//const char * check_len(long cc);
void check_len(long cc);
// Main program
int main(void)
{
int i;
long credit_number = get_number();
printf("Validation for %ld\n", credit_number);
check_len(credit_number);
}
// Function that prompt the user for a credit card number
long get_number(void)
{
// Declaration of the credit card variable
long number;
// Ask the user for a credit card number. If it's not none-negative, keep asking
do
{
number = get_long("Enter credit card number: ");
}
while (number <=0);
return number;
}
// Function to check the type of credit card
void check_len(long cc)
{
// Declaration of variables
int i;
long number = cc;
// Count the len of the provided credit card number
for (i = 0; cc != 0; i++)
{
cc = cc/10;
}
printf("%i\n", i);
// Validate the type of card
if (i == 15)
{
if ( (number >= 34e13 && number < 35e13) || (number >= 37e13 && number < 38e13) )
printf("AMEX\n");
else
{
printf("INVALID\n");
}
}
else if (i == 13 || i == 16)
{
if ( (number >= 51e14 || number < 56e14) )
{
printf("MASTERCARD\n");
}
else if ( (number >= 4e12 || number < 5e12) || (number >= 4e15 || number <= 5e15))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
给定
if ( (number >= 51e14 || number < 56e14) )
...
51e14
小于 56e14
,所以 每个 实数要么大于 51e14
要么小于 56e14
.
如果您只想要该范围内的数字,则需要使用 &&
以便满足两个条件:
if ( ( number >= 51e14 ) && ( number < 56e14 ) )
...
你的其他情况看起来也有类似的问题。