/* check id_card
* write by sndnvaps<sndnvaps@gmail.com>
* ai -> a1 , a2, a3, a4, a5, a6... a17 (a18 是校验码) 身份证前17位对应(ai)
* wi -> 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 (17位)
*
* int res = 0;
* for (i = 1; i < 17; i++) {
* res += (a[i] * w[i])
* }
* int y = res % 11;
*
*
* y 与 a18的对应关系
*
* y { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
* a18 { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 } -> vefiry[18] = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, ,3, 2};
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int wi[] = { , , , , , , , , , , , , , , , , };
int a18[] = { , , 'X', , , , , , , , };
int char2int(char n) {
return (n - );
}
int check_id(char id[] ) {//return y
int res = ;
int y;
int i;
for (i = ; i < ; i++) {
res += (char2int(id[i]) * wi[i]);
}
y = res % ;
return y;
}
int verify_id_t(int verify_id, int id_v) { //verify_id = y; id_v = a18
int i;
int temp; //store the i ;
for (i = ; i < ; i++) {
if (i == verify_id) {
temp = a18[i];
printf("a18[%d] = %d\n", i, temp);
break;
}
}
if (temp == id_v)
return ; // get it , 验证合格
return ;
}
void usage() {
printf("check_id_card id_card_num \n");
}
int main(int argc, char *argv[]) {
if (argc < ) {
usage();
}
char id_string[];
int id_18;
int i;
int y;
if (strlen(argv[]) > ) {
printf("id_card_num must be 18 bits\n");
return 0;
}
strncpy(id_string, argv[], );
id_18 = char2int(id_string[]);
id_string[] = '\0';
printf("id_18 = %d\n", id_18);
y = check_id(id_string);
if(verify_id_t(y, id_18) == ) {
printf("verify success\n");
} else {
printf("verify failed\n");
}
return ;
}