// 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的环境。
代码: