我什至尝试使用 long int 代替 int ,但是编译器花费太多时间执行并且仍然不提供任何输出。
我正在使用CS50的IDE。
这是导致问题的代码:
int sum_alt = 0;
int sum_rest = 0;
int total = 0;
for(int i = 1; i < count; i=+2)
{
int temp = number[i] * 2;
sum_alt = sum_alt + temp;
}
for(int i = 0; i <= count; i=+2)
{
int temp = number[i];
sum_rest = sum_rest + temp;
}
total = sum_rest + sum_alt;
如何解决此问题?
这是完整的代码:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long long Cardnumber;
//Typos correction + User input
do
{
Cardnumber = get_long_long("Number: ");
}
while(Cardnumber < 0);
int count = 0;
long long counter = Cardnumber;
//To count the number of digits
while(counter != 0)
{
counter = counter / 10;
count++;
}
//Declaring an array for cardnumber
printf("%i", count);
int number[count];
if((count != 13) && (count != 15) && (count != 16))
{
printf("INVALID\n");
}
for(int access = 0; access < count; access++)
{
number[access] = Cardnumber % 10;
Cardnumber = Cardnumber / 10;
}
int originalnumber[count];
for(int i = 0; i < count; i++)
{
originalnumber[i] = number[i];
}
long long int sum_alt = 0;
long long int sum_rest = 0;
long long int total = 0;
for(int i = 1; i < count; i=+2)
{
int temp = number[i] * 2;
sum_alt = sum_alt + temp;
}
for(int i = 0; i < count; i=+2)
{
int temp = number[i];
sum_rest = sum_rest + temp;
}
total = sum_rest + sum_alt;
if(total % 10 == 0)
{
if(((count == 13) || (count == 16)))
{
if((originalnumber[12] == 4) || originalnumber[15] == 4)
{
printf("VISA\n");
}
else if(originalnumber[15]== 5)
{
if(originalnumber[14] == 1 || originalnumber[14]== 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5)
{
printf("MASTERCARD\n");
}
}
else
{
printf("INVALID\n");
}
}
else if (count == 15)
{
if(originalnumber[15] == 3)
{
if(originalnumber[15] == 4 || originalnumber[15] == 7)
{
printf("AMERICAN EXPRESS\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}
PS。我不知道CS50的IDE花这么多时间进行计算的原因。
答案 0 :(得分:1)
这是因为2147483644 + 4 = 2147483648,它大于2147483647(有符号int32限制)。 64位整数类型为 long long int ,即AFAIK。
应该是:
long long int sum_alt = 0;
long long int sum_rest = 0;
long long int total = 0;
for(int i = 1; i < count; i=+2)
{
int temp = number[i] * 2;
sum_alt = sum_alt + temp;
}
for(int i = 0; i <= count; i=+2)
{
int temp = number[i];
sum_rest = sum_rest + temp;
}
total = sum_rest + sum_alt;
P.S。另外,该代码未定义变量“ count”,因此可能存在问题。
答案 1 :(得分:0)
以下代码从用户那里获得信用卡号,检查其长度和校验和,然后确定发行者。
可以使用从https://ccardgenerator.com/获得的信用卡号对程序进行测试。
这段代码的属性是
number[i]*2
的数字求和)#include <stdio.h>
#include <ctype.h>
int main()
{
char buf[32];
printf("enter credit card number: ");
fgets(buf, sizeof(buf), stdin);
int len = 0;
while(isdigit(buf[len]))
len++;
if(len < 13 || len > 16) {
printf("INVALID\n");
return 1;
}
// verify credit card number check sum
int checkSum = 0;
for(int i = 0; i < len; i++) {
int d = buf[len-i-1] - '0'; // get digit from right most to left most
if((i&1) == 0)
checkSum += d;
else
checkSum += (d*2)%10 + (d*2)/10;
}
if(checkSum%10 != 0) {
printf("INVALID\n");
return 1;
}
// determine issuer (see https://www.regular-expressions.info/creditcard.html)
int start = (buf[0]-'0')*1000 + (buf[1]-'0')*100 + (buf[2]-'0')*10 + buf[3]-'0';
if((len == 13 || len == 16) && start/1000 == 4) {
printf("VISA\n");
} else if(len == 16 && ((start >= 5100 && start < 5600) || (start >= 2221 && start <= 2720))) {
printf("MASTERCARD\n");
} else if(len == 15 && ((start >= 3400 && start < 3500) || (start >= 3700 && start < 3800))) {
printf("AMERICAN EXPRESS\n");
} else if(len == 14 && ((start >= 3000 && start < 3060) || start/100 == 36 || start/100 == 38)) {
printf("DINERS CLUB\n");
} else if(len == 16 && (start == 6011 || start/100 == 65)) {
printf("DISCOVER\n");
} else if((len == 15 && (start == 2131 || start == 1800)) || (len == 16 && start/100 == 35)) {
printf("JCB\n");
} else {
printf("UNKNOWN\n");
}
return 0;
}