/* 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[17] = { 7, 9, 10, 5, 8 , 4, 2, 1, 6, 3, 7, 9 , 10, 5, 8, 4, 2}; int a18[11] = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2}; int char2int(char n) { return (n - 48); } int check_id(char id[17] ) {//return y int res = 0; int y; int i; for (i = 0; i < 17; i++) { res += (char2int(id[i]) * wi[i]); } y = res % 11; 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 = 0; i < 10; i++) { if (i == verify_id) { temp = a18[i]; printf("a18[%d] = %d ", i, temp); break; } } if (temp == id_v) return 1; // get it , 验证合格 return 0; } void usage() { printf("check_id_card id_card_num "); } int main(int argc, char *argv[]) { if (argc < 2) { usage(); } char id_string[18]; int id_18; int i; int y; if (strlen(argv[1]) > 18) { printf("id_card_num must be 18 bits ");
return 0; } strncpy(id_string, argv[1], 18); id_18 = char2int(id_string[17]); id_string[17] = '