【C++】判断身份证号是否合法

const char* cityCode[35] = { "11", "12", "13", "14", "15", "21","22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42","43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62","63", "64", "65", "71", "81", "82", "91" };

int checkCityCode(const char* city)
{
	int i = 0;

	for (i = 0; i < 35; i++)
	{
		if (memcmp(cityCode[i], city, 2) == 0)
		{
			return 0;
		}
	}
	return 1;
}

int isdigit(int c)
{
	return (c >= '0' && c <= '9');
}

int Chk18PaperId(char *sPaperId)
{
	long lSumQT = 0;
	int R[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
	char sChecker[11] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
	if (18 != strlen(sPaperId)) return -1;

	for (int i = 0; i<18; i++)
	{
		if (!isdigit(sPaperId[i]) && !(('X' == sPaperId[i] || 'x' == sPaperId[i]) && 17 == i))
		{
			return -2;
		}
	}

	if (checkCityCode(sPaperId))
	{
		return -3;
	}


	for (int i = 0; i <= 16; i++)
	{
		lSumQT += (sPaperId[i] - 48) * R[i];
	}
	if (sChecker[lSumQT % 11] != sPaperId[17])
	{
		return -5;
	}

	return 0;
}


#if 0
int _tmain(int argc, _TCHAR* argv[])
{
	for (int i = '7'; i <= '7'; i++)
	{
		for (int j = '8'; j <= '8'; j++)
		{
			for (int k = '0'; k <= '1'; k++)
			{
				for (int m = '1'; m <= '9'; m++)
				{
					for (int n = '0'; n <= '1'; n++)
					{
						for (int l = '1'; l <= '9'; l++)
						{
							int rcode = 0;
							char s[19];
							memset(s, 0, 19);
							s[0] = '2';
							s[1] = '2';
							s[2] = '0';
							s[3] = '4';
							s[4] = '0';
							s[5] = '2';
							s[6] = '0';
							s[7] = '0';
							s[8] = i;
							s[9] = j;
							s[10] = k;
							s[11] = m;
							s[12] = n;
							s[13] = l;
							s[14] = '0';
							s[15] = '0';
							s[16] = '0';
							s[17] = '0';
							s[18] = '\0';
							//printf("s = %s\n",s);
							rcode = Chk18PaperId(s);
							if (rcode)
							{
								//printf("error, is a wrong id number");
							}
							else
							{
								printf("%s\n", s);
							}
						}
					}
				}
			}
		}
	}

	printf("OK!\n");
	getchar();
	return 0;
}
#endif

判断身份证号合法性检验程序

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值