69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <ctype.h>
#define ID_LEN 18
const unsigned char weight_list[] = { 7,
9, 10, 5, 8,
4, 2, 1, 6,
3, 7, 9, 10,
5, 8, 4, 2
};
const char check_code[] = { '1',
'0', 'X', '9', '8', '7',
'6', '5', '4', '3', '2'
};
int check_digit(char *sid);
int check_id(char *sid);
int main(void)
{
char id[20];
int num, flag = 0;
scanf("%d", &num);
while (num--) {
scanf("%s", id);
if (check_digit(id)) {
if (!check_id(id)) { /* Invalid ID */
flag = 1;
printf("%s\n", id);
}
} else {
flag = 1;
printf("%s\n", id); /* Invalid ID */
}
}
if (!flag)
printf("All path!\n");
return 0;
}
int check_digit(char *sid)
{
int i;
for (i = 0; i < ID_LEN - 1; i++)
if (!isdigit(sid[i]))
return 0;
return 1;
}
int check_id(char *sid)
{
int weight_sum = 0, mode;
int i;
for (i = 0; i < ID_LEN - 1; i++)
weight_sum += (sid[i] - '0') * weight_list[i];
mode = weight_sum % 11;
if (sid[ID_LEN-1] == check_code[mode])
return 1;
else
return 0;
}
#include <atlstr.h>
#include <time.h>
//随机生成大于18岁的身份证号
CString GenerationId(int OldThan = 18)
{
time_t t = time(NULL);
tm *tTm = localtime(&t);
int iBaseYear = 1950;
int YearRange = tTm->tm_year + 1900 - iBaseYear - OldThan;
srand(t + rand());
int SixHead = rand() % 999999 + 111111;
int Year = rand() % YearRange + iBaseYear;
int Month = rand() % 12 + 1;
int Day = rand() % 28 + 1;
int ThrEnd = rand() % 999;
TCHAR chi[12] = TEXT("10X98765432");
int Wi[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
TCHAR strID[19] = {0};
_stprintf(strID, TEXT("%06d%04d%02d%02d%03d"), SixHead, Year, Month, Day, ThrEnd);
int Sum = 0;
TCHAR buf[MAX_PATH] = {0};
for(int i = 0; i < 17; i++)
{
Sum += Wi[i] * (strID[i] - '0');
}
TCHAR cLast = chi[Sum % 11];
strID[17] = cLast;
CString tStr = strID;
return tStr;
}
//校验身份证号合法性, 传入身份证串.
BOOL checkValid(const char *idStr)
{
char chi[12] = "10X98765432";
int Wi[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
int Sum = 0;
for(int i = 0; i < 17; i++)
{
Sum += Wi[i] * (idStr[i] - '0');
}
char cLast = chi[Sum % 11];
return idStr[17] == cLast;
}