c语言 身份证检测 代码

Modified on: Sat, 02 Mar 2019 01:39:30 +0800 热度: 1,104 度

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

int main(int argc, const char * argv[]) {

char m[11]="10X98765432";
int z[18]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char idcard[18];
int birthday=0;
int n,i,j;
int flag=0;
int judge = 0;
int sex;
putchar(0);
scanf("%d",&n);
for(i=0;i<n;i++)
{
    scanf("%s",idcard);
    judge=0;
    for(j=0;j<17;j++)
    {
        //printf("%d ",idcard[j]-'0');
        if(idcard[j]<'0'||idcard[j]>'9')
        {
            flag=1;
        }
    }
    
    if(!flag)
    {
        for(j=0;j<17;j++)
            judge+=(idcard[j]-'0')*z[j];
        if(m[judge%11]!=idcard[17])flag=2;  //   2=识别位错误
        
        for(j=6;j<10;j++)
            birthday+=(idcard[j]-'0')*pow(10,9-j);
        if(birthday<1800) flag=3;           //   3=生日异常
        birthday=0;
        
        for(j=10;j<12;j++)
            birthday+=(idcard[j]-'0')*pow(10,11-j);
        if(birthday>12||birthday<1) flag=4; //   3=生日月份模糊判断
        birthday=0;
        
        for(j=12;j<14;j++)
            birthday+=(idcard[j]-'0')*pow(10,13-j);
        if(birthday>31||birthday<1) flag=5; //   3=生日日期模糊判断
        
    }
    if(flag)
    {
        printf("%s\n无法识别的身份证号码\n",idcard);
        flag=0;
    }else
    {
        printf("%s\n",idcard);
        sex=(idcard[16]-'0')%2;
        if(sex==1)
        {
            printf("性别:男\n");
        }else
            printf("性别:女\n");
    }
}
return 0;

}

添加新评论