1. 程式人生 > >身份證校驗及其18歲判定(包含18位身份證和15位身份證)

身份證校驗及其18歲判定(包含18位身份證和15位身份證)

// IDCardVerifyDemo.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

bool CheckEighteenCard(const string& idCard)
{
	int size = idCard.size();
	// 驗證出生日期是否正確
	string strBirthDay = idCard.substr(6, 8); // 身份證第6位到第13位為出生日期
	string strYear = strBirthDay.substr(0,4);
	string strMonth = strBirthDay.substr(4,2);
	string strDay = strBirthDay.substr(6,2);

	int birthDay = atoi(strBirthDay.c_str());
	int year = atoi(strYear.c_str());
	int month = atoi(strMonth.c_str());
	int day = atoi(strDay.c_str());
	cout << " birthDay = " << birthDay << " year = " << year << " month=" << month << " day = " << day << endl;
	// 系統當前時間
	time_t tt = time(NULL);
	tm* t= localtime(&tt);
	int nowYear = t->tm_year + 1900;
	cout << " nowYear = " << nowYear << endl;
	if (year >= nowYear )
	{
		cout << " invalid year = " << year;
		return false;
	}
	if (month < 1 || month > 12)
	{
		cout << " invalid month = " << month;
		return false;
	}

	if (day < 1 || day > 31)
	{
		cout << " invalid day = " << day;
		return false;
	}

	// 驗證校驗碼是否匹配
	int factor[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; // 權重因子
	string verifyCode = "10X98765432"; // 校驗碼
	int sum = 0;
	for (int i =0; i < size -1; i++)
	{
		string strNum = idCard.substr(i,1);
		int num = atoi(strNum.c_str());
		sum += ( num * factor[i] );
	}
	string strVerifyCode= verifyCode.substr(sum%11 ,1);
	string userVerifyCode = idCard.substr(size -1,1);
	cout << " veryCode = " << strVerifyCode;
	cout << " userVeryCode = " << userVerifyCode;
	if ( strVerifyCode != userVerifyCode)
	{
		cout << " invalid idCard! verifyCode failed!" << endl;
		return false;
	}
	return true;
}

bool CheckFifteenCard(const string& idCard)
{
	// 驗證出生日期是否正確
	string strBirthDay = idCard.substr(6, 6); // 身份證第6位到第11位為出生日期
	string strYear = strBirthDay.substr(0,2); // 年份 15位身份證只有88-->1988年
	string strMonth = strBirthDay.substr(2,2);
	string strDay = strBirthDay.substr(4,2);

	int birthDay = atoi(strBirthDay.c_str());
	int year = atoi(strYear.c_str()) + 1900;
	int month = atoi(strMonth.c_str());
	int day = atoi(strDay.c_str());
	cout << " birthDay = " << birthDay << " year = " << year << " month=" << month << " day = " << day << endl;
	// 系統當前時間
	time_t tt = time(NULL);
	tm* t= localtime(&tt);
	int nowYear = t->tm_year + 1900;
	cout << " nowYear = " << nowYear << endl;
	if (year >= nowYear )
	{
		cout << " invalid year = " << year;
		return false;
	}
	if (month < 1 || month > 12)
	{
		cout << " invalid month = " << month;
		return false;
	}

	if (day < 1 || day > 31)
	{
		cout << " invalid day = " << day;
		return false;
	}
	// 15位身份證無校驗碼
	return true;
}

bool CheckIdCard(const string& idCard)
{
	cout << " ----- checking idCard------";
	int size = idCard.size();
	cout<< " idNum = " << idCard << " size = " << size << endl;
	if (size == 18) // 18位身份證驗證
	{
		return CheckEighteenCard(idCard);
	}
	//else if ( size == 15) //15位身份證驗證, 全域性禁用15位身份證,如果以後要啟用直接開啟這個分支就ok
	//{
	//	return CheckFifteenCard(idCard);
	//}
	return false;
}

int CheckBirthDay(const string& idCard, int year, int month, int day)
{
	cout  << " year = " << year << " month=" << month << " day = " << day << endl;
	// 判斷是否超過18歲
	time_t tt = time(NULL);
	tm* t= localtime(&tt);
	int nowYear = t->tm_year + 1900;
	int nowMonth = t->tm_mon + 1;
	int nowDay = t->tm_mday;

	if ((nowYear - year) < 18)
	{
		return 0;
	}else if ((nowYear - year) == 18)
	{
		if ( nowMonth < month)
		{
			return 0;
		}else if (nowMonth == month)
		{
			if (nowDay < day)
			{
				return 0;
			}
		}
	}
	return 1;
}

// 返回1 表示成年人 0 表示未成年人 -1 表示身份證號碼錯誤
int CheckIsAdult(const string& idCard)
{
	cout << " ----- checking checkIsAdult------";
	if (!CheckIdCard(idCard))
	{
		return -1;
	}
	int size = idCard.size();
	if (size == 18)
	{
		string strBirthDay = idCard.substr(6, 8); // 身份證第6位到第13位為出生日期
		string strYear = strBirthDay.substr(0,4);
		string strMonth = strBirthDay.substr(4,2);
		string strDay = strBirthDay.substr(6,2);
		int year = atoi(strYear.c_str());
		int month = atoi(strMonth.c_str());
		int day = atoi(strDay.c_str());
		return CheckBirthDay(idCard, year, month, day);
	}else if (size == 15)
	{
		string strBirthDay = idCard.substr(6, 6); // 身份證第6位到第11位為出生日期
		string strYear = strBirthDay.substr(0,2);
		string strMonth = strBirthDay.substr(2,2);
		string strDay = strBirthDay.substr(4,2);
		int year = atoi(strYear.c_str()) + 1900; // 15位身份證 年齡為88 --> 1988
		int month = atoi(strMonth.c_str());
		int day = atoi(strDay.c_str());
		return CheckBirthDay(idCard, year, month, day);
	}	
	return -1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	string idNum = "511321198810047755";
	//string idNum = "123456950231775";
	int size = idNum.size();
	cout<< " idNum = " << idNum << " size = " << size << endl;
	//checkIdCard(idNum);
	int ret = CheckIsAdult(idNum);
	switch(ret)
	{
	case -1:
		{
			cout << "invalid idCard = " << idNum;
			break;
		}
	case 0:
		{
			cout << " your are less 18 years!";
			break;
		}
	case 1:
		{
			cout << " your are audit!";
			break;
		}
	}
	getchar();
	return 0;
}

相關資料:

http://baike.baidu.com/view/188003.htm?fromId=118340

vs2008的環境。

程式碼: