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
判断身份证号合法性检验程序