-1

I tried to make a C program that checks if a bank card is valid.

According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.

Add the sum to the sum of the digits that weren’t multiplied by 2.

If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

That’s kind of confusing, so let’s try an example with my AmEx: 378282246310005.

For the sake of discussion, let’s first underline every other digit, starting with the number’s second-to-last digit:

378282246310005

Okay, let’s multiply each of the underlined digits by 2:

7•2 + 2•2 + 2•2 + 4•2 + 3•2 + 0•2 + 0•2

That gives us:

14 + 4 + 4 + 8 + 6 + 0 + 0

Now let’s add those products' digits (i.e., not the products themselves) together:

1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27

Now let’s add that sum (27) to the sum of the digits that weren’t multiplied by 2:

27 + 3 + 8 + 8 + 2 + 6 + 1 + 0 + 5 = 60

Yup, the last digit in that sum (60) is a 0, so my card is legit!

So, validating credit card numbers isn’t hard, but it does get a bit tedious by hand

#include <cs50.h>
#include <math.h>
#include <stdio.h>

int length(long long n);
int num_at(int x,long long y);
long long flip(double a);

int main(void)
{
    //int total2=0;
    //printf("Number: ");
    //int i=get_int();
    long long ll=get_long_long();
    //if (ll<=0)
    //{
    //    printf("INVALID\n");
    //}
    //for (int i=1;2*i<length(ll);i++)
    //{
    //    total2=total2+2*num_at(i,ll);
    //}
    //int total1=0;
//for (int i=0;2*i<length(ll);i++)
//{
//    total1=total1+num_at(i,ll);
//}
//int total=total1+total2;
//printf("%i\n",total);
printf("%lli\n",flip(ll));
}

int length(long long n)/* length of number */
{
    long long x=1;
    int len=0;
    while(n-n%x!=0)/* x % y means x mod y*/
    {
        len++;
        x=x*10;
    }
        return len;
}
int num_at(int x,long long y)/* digit at specific spot in number */
{
    int digit=0;//the digit at position z(see below)
    int z=1;
    for(z=x;z>=0;z--)
    {
        digit=y%10;
        y=y-y%10;
        y=y/10;
    }
    return digit;
}
long long flip(double a)//flips a
{
    long long b=0;
    for(int y=0;y<length(a);y++)
    {
        b=b*10;
        b=b+(a%pow(10.0,y+1)-a%pow(10.0,y))/pow(10.0,y);
    }
    return b;
}

The error when compiling is:

credit.c:99:15: error: invalid operands to binary expression ('double' and 'double')
    b=b+(a%pow(10.0,y+1)-a%pow(10.0,y))/pow(10.0,y);
         ~^~~~~~~~~~~~~~
credit.c:99:31: error: invalid operands to binary expression ('double' and 'double')
    b=b+(a%pow(10.0,y+1)-a%pow(10.0,y))/pow(10.0,y);
                         ~^~~~~~~~~~~~
4

4 回答 4

1

您不能对双精度(或浮点数)执行取模运算。原因很简单,余数只为整数算术定义。如果我将 2.3 除以 0.4,余数是多少?

因此,要执行您想要的操作,您需要将 double ( a 和 pow 的结果)施放很长的时间。

所以你可以做((long long) a) % (long long) pow( ... ),应该没问题。

将来您应该查看错误消息(以及警告)。

这里清楚地表明二进制表达式的操作数无效,并且标记了 % 运算符。

于 2017-03-28T04:12:16.010 回答
1

pow返回一个 double 并且%运算符仅针对整数定义。我认为这应该可以解决您的问题。

于 2017-03-28T04:13:14.567 回答
1

算法应该比你写的更简单。我不是 C 程序员,但是:

// transform LL to char []
int len = length(ll);
char card[len];
sprintf(card, "%lld", ll);
int sum =0;
for(i=0;i < len; i++)
{
    int num = card[i] - '0';
    if(i % 2 == 0)
    { sum += num; }
    else
    { 
        int tmp = num * 2; 
        sum += (tmp / 10);
        sum += (tmp % 10);
    }
}

if(sum % 10)
{ 
     // card valid 
}

在我看来更具可读性。(对不起,如果错字,这篇文章是用旧智能手机写的......)

编辑 我知道您喜欢使用循环,但您的 num_at 也可以得到增强:

int num_at(int pos, long long x)
{
     return (x / (int)pow(10, pos)) % 10;
}
于 2017-03-28T04:40:02.760 回答
0

您希望如何%在两个双精度值之间进行模运算 ( )?

究竟是10.3 % 5.2什么?

错误消息只是说,“你不能做双模双”

于 2017-03-28T04:10:23.283 回答