身份证号码合法性判断

问题描述: 
我国公民的身份证号码特点如下:
1、长度为18位;
2、第1~17位只能为数字;
3、第18位可以是数字或者小写英文字母x。
4、身份证号码的第7~14位表示持有人生日的年、月、日信息。

例如:511002198808080111或51100219880808011x。
 
请实现身份证号码合法性判断的函数。除满足以上要求外,需要对持有人生日的年、月、日信息进行校验。年份大于等于1900年,小于等于2100年。需要考虑闰年、大小月的情况。所谓闰年,能被4整除且不能被100整除或能被400整除的年份,闰年的2月份为29天,非闰年的2月份为28天。其他情况的合法性校验,考生不用考虑。
函数返回值:
1、如果身份证号合法,返回0;
2、如果身份证号长度不合法,返回1;
3、如果身份证号第1~17位含有非数字的字符,返回2;
4、如果身份证号第18位既不是数字也不是英文小写字母x,返回3;
5、如果身份证号的年信息非法,返回4;
6、如果身份证号的月信息非法,返回5;
7、如果身份证号的日信息非法,返回6(请注意闰年的情况);
注:除成功的情况外,以上其他合法性判断的优先级依次降低。也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断。
 
要求实现函数:
int verifyIDCard(char* input)

示例
1、输入:”511002111222”,函数返回值:1;
2、输入:”511002abc123456789”,函数返回值:2;
3、输入:”51100219880808123a”,函数返回值:3;
4、输入:”511002188808081234”,函数返回值:4;
5、输入:”511002198813081234”,函数返回值:5;
6、输入:”511002198808321234”,函数返回值:6;
7、输入:”511002198902291234”,函数返回值:6;

8、输入:”511002198808081234”,函数返回值:0;


C代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int verifyIDCard(char* input);
int is_leapyear(int year);
int getyear(char* input);
int getmonth(char* input);
int getday(char* input);
int check_1to17(char* input);
int check18(char* input);
int chech_year_month_day(int year, int month, int day);

int main()
{
	char input[8][100] = {"511002111222","511002abc123456789",
	"51100219880808123a","511002188808081234","511002198813081234",
	"511002198808321234","511002198902291234","511002198808081234"};
	int i;
	for(i=0; i<8; i++)
	{
		printf("the IDcard is: %s, the result is: %d\n",input[i],verifyIDCard(input[i]));
	}
	return 0;
}

int is_leapyear(int year)
{
	if(( (year%4==0) && (year%400!=0) ) || (year%400==0))
		return 1;
	return 0;
}

int getyear(char* input)
{
	int year=0;
	year = (input[6]-'0') * 1000 + (input[7]-'0') * 100 +
		(input[8]-'0') * 10 + (input[9]-'0');
	return year;
}

int getmonth(char* input)
{
	int month=0;
	month = (input[10]-'0') * 10 + (input[11]-'0');
	return month;
}

int getday(char* input)
{
	int day=0;
	day = (input[12]-'0') * 10 + (input[13]-'0');
	return day;
}

int check_1to17(char* input)
{
	int i;
	for(i=0; i<17; i++)
		if( !(input[i]>='0' && input[i]<='9') )
			return 0;
	return 1;
}

int check18(char* input)
{
	char ch = input[17];
	if( (ch=='x') || (ch>='0' && ch<='9') )
		return 1;
	else
		return 0;
}

int check_year_month_day(int year, int month, int day)
{
	int leap;
	leap = is_leapyear(year);
	if(year<1900 || year>2100)
		return 4;
	if(month<1 || month>12)
		return 5;
	switch(month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		{
			if(day<1 || day>31)
				return 6;
			break;
		}
	case 4:
	case 6:
	case 9:
	case 11:
		{
			if(day<1 || day>30)
				return 6;
			break;
		}
	case 2:
		{
			if(leap)
			{
				if(day<1 || day>29)
					return 6;
				break;
			}
			else
			{
				if(day<1 || day>28)
					return 6;
				break;
			}
		}
	}
	return 0;
}

int verifyIDCard(char* input)
{
	int year,month,day;
	if(strlen(input)!=18)
		return 1;
	else
	{
		if(!check_1to17(input))
			return 2;
		else
		{
			 if(!check18(input))
				 return 3;
			 else
			 {
				year = getyear(input);
				month = getmonth(input);
				day = getday(input);
				return check_year_month_day(year,month,day);
			 }
		}
	}
}

运行结果如下:


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值