#include<stdio.h> #include<stdlib.h> #include<string.h> //身份证验证规则 void main(int argc,char **argv) { if(argc<2 || strlen(argv[1])<18) { printf("请输入18位身份证号码"); return; } int cr[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1}; int mr[]={1,0,'X',9,8,7,6,5,4,3,2}; char ids[20]={'\0'}; sprintf(ids,"%s",argv[1]); ids[17]='\0'; int idnum[17]={0}; int i; for(i=0;i<strlen(ids);i++) { char ch[2]={'\0'}; ch[0]=ids[i]; idnum[i]=atoi(ch); } int s1=sizeof(idnum)/sizeof(int); int s2=sizeof(cr)/sizeof(int); int ts=0; for(i=0;i<s1;i++) { ts+=idnum[i]*cr[i]; } int m=ts%11; int li=mr[m]; char newid[20]={'\0'}; for(i=0;i<s1;i++) { char ch[2]={'\0'}; sprintf(ch,"%d",idnum[i]); strcat(newid,ch); } if(li==88) strcat(newid,"X"); else { char ch[2]={'\0'}; sprintf(ch,"%d",li); strcat(newid,ch); } printf("身份证号码:%s ",argv[1]); if(strstr(argv[1],newid)) { printf("有效"); } else { printf("无效"); } }