判断身份证是否合法
#include <stdio.h>
int main()
{
char a[80];
char *p;
int i;
printf("请输入一个身份证号:\n");
scanf("%s",a);
p=a;
if(strlen(a)==18){
for(i=0;i<17;i++)
{
if(!(47<a[i]&&a[i]<58))
{
printf("不合法\n");
exit(0);
}
}
if((47<a[17]&&a[17]<58)||(a[17]=='x'))
{
if(nian(p+6)==1){
printf("身份证合法\t");
exit(0);
}
}
}
printf("不合法\n");
return 0;
}
int nian(char *p1)
{
int bools=0;
int count;
int number;
int year=0,month,day,sum,leap,i;
for(i=3;i>=0;i--)
{
number=1;
for(count=i;count>=0;count--)
{
number=number*10;
}
year+=(*p1-48)*number;
p1++;
}
if((*p1-48)==0)
month=(*(++p1)-48);
else
month=((*p1)-48)*10+(*(++p1)-48);
p1++;
if((*p1-48)==0)
day=(*(++p1)-48);
else
day=((*p1)-48)*10+(*(++p1)-48);
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: if(day<32&&day>0) bools=1; break;
case 4:
case 6:
case 9:
case 11:if(day<31&&day>0) bools=1;break;
case 2:
if(leap==1)
{
if(day>0&&day<30) bools=1;
}
else
{
if(day<29&&day>0) bools=1;
}
break;
default:bools=0;
}
return bools;
}