ID card verification (c ++) and ID card verification
Description:
The Chinese national standard [GB 11643-1999] stipulates that the citizenship number is an 18-digit feature combination code, which consists of a 17-digit ontology code and a digital verification code. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right. The calculation method and steps of the Verification Code (last digit) are as follows: (1) weighted summation formula S = Sum (Ai * Wi), I = 0 ,..., 16. First, sum the weights of the first 17 digits. "Ai" indicates the ID card number at the I position. "Wi" indicates the weighting factor at the I position, the first 17 weighting factors are from left to right for Wi: 7 9 10 5 8 8 4 2 1 6 3 7 9 10 5 8 4 2 (2) modulo Y = mod (S, 11) (3) Check the following table by MoD Y to obtain the corresponding verification code.
| Y |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
| Verification Code |
1 |
0 |
X |
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |
For example, the first 17 digits of an ID card are 11010519491231002.
| I |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
| Wi |
7 |
9 |
10 |
5 |
8 |
4 |
2 |
1 |
6 |
3 |
7 |
9 |
10 |
5 |
8 |
4 |
2 |
| |
1 |
1 |
0 |
1 |
0 |
5 |
1 |
9 |
4 |
9 |
1 |
2 |
3 |
1 |
0 |
0 |
2 |
| Product |
7 |
9 |
0 |
5 |
0 |
20 |
2 |
9 |
24 |
27 |
7 |
18 |
30 |
5 |
0 |
0 |
4 |
If the sum is 167, the modulo is y = 167% 11 = 2 (3) check code is X (uppercase). Follow the steps above to program and enter a second generation ID card number, check whether the ID card is correct. Input: enter several lines, one ID card number per line, and the last line input-1 Output: Output 1 indicates correct, 0 indicates wrong input:
120223198902021249130132199210293822130402198207290622-1
output:
1
1
0
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int cal(string a) 6 { 7 return (a[0] - '0') * 7 + (a[1] - '0') * 9 + (a[2] - '0') * 10 + (a[3] - '0') * 5 + (a[4] - '0') * 8 + (a[5] - '0') *4 + (a[6] - '0') * 2 + (a[7] - '0') * 1 + (a[8] - '0') * 6 +(a[9] - '0') * 3 + (a[10] - '0') * 7 + (a[11] - '0') * 9 + (a[12] - '0') * 10 + (a[13] - '0') * 5 + (a[14] - '0') * 8 + (a[15] - '0') * 4 +(a[16] - '0') * 2; 8 } 9 10 char s(string a)11 {12 int k = cal(a) % 11;13 if (k == 0)14 return '1';15 else if (k == 1)16 return '0';17 else if (k == 2)18 return 'X';19 else20 return '0'+12-k;21 }22 int main()23 {24 string number;25 while ((cin >> number) && number != "-1")26 {27 if (number[17] == s(number))28 cout << "1" << endl;29 else30 cout << "0" << endl;31 }32 system("pause");33 return 0;34 }