编辑代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define MAX_LEN 128
 
int CheckLuhn ( const char * cardId, int sum[2] );
int ReadLine(char *s);
 
int ReadLine(char *s)
{
    int n = 0, ch = 0;
    while((ch = getchar()) != '\n' && ch != EOF && n < MAX_LEN - 1)
        s[n++] = ch;
    s[n]='\0';
    return n;
}
 
int main()
{
    int s[2]={0}, r;
    char id[MAX_LEN];
 
    while (ReadLine(id) > 0) {
        r = CheckLuhn(id, s);
        printf("%s,R=%d\t%d\t%d\n", id, r, s[0], s[1]);
    }
    return 0;

}

int CheckLuhn ( const char * cardId, int sum[2] ){

   int len = strlen(cardId);

         for(int i = (len - 1) ; i >= 0 ;i--){

           int  num = cardId[i] - '0'; 

           if( (len - i) % 2 != 0 ){
         
              sum[0] += num; //奇数位求和

       }

           if( (len - i) % 2 == 0 ){

                 int temp =  num *2 ;

                    if(temp >= 10 ){

                      temp -= 9;

                    }
              sum[1] += temp; //偶数位求和

       }

     }
int summary = sum[0] + sum[1] ;
   if(summary % 10 == 0){

     return 0 ;

    }

   if(summary % 10 != 0){

     return 1 ;

    }

}