数据结构 之c语言 --判断身份证合法

判断身份证是否合法


#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'))  /*判断最后以为是否是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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值