# Identity verification (is_id_number)

According to my observation, the ID number is regular. The ID number of the same town is basically the same as the 6. The next 8 are obviously the date of birth. The last 4 figures are like serial numbers. Why not just change numbers on the basis of personal ID number, and then turn them into others? wait a moment! Some students' ID number last is actually X, not as simple as imagined.

Task: write a function to verify whether the identity number is valid.

According to the national standard of GB11643-1999 citizenship number, the requirements are as follows:

The citizenship number consists of a seventeen digit master number and a one digit check number.
From left to right: six digit address code, eight digit date code, three digit sequence code and one digit check code.
Address code: omitted (no need to check this question)
Date code: should be a valid date (considering leap year)
Sequence code: the sequence number of people born in the same year, month and day within the area identified by the same address code. Odd numbers are allocated to men and even numbers to women.
Check code: multiply the 17 digits of the body code by the weight [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2] and accumulate.
Add: the weight can be saved in an array and processed in a loop. Don't write 17 variables or if
PS: not even 17 case s.
Divide the accumulation result by 11, and the remainder may be 0, 1,..., 10, corresponding to the check code [1,0,X,9,8,7,6,5,4,3,2], that is, the last digit of the ID number.
The check code can be saved in an array. A better way is to write it as a string (the native string of C + + is a character array) and use the remainder as the subscript index. Don't write 10 if or case

Input specifications
Each line is a string, and the whole line is read in and processed until EOF.

Output specification
For each line of input, output a line of verification results. Please replace xxx with the input string.

Valid, please output xxx: YYYY-MM-DD M/F, and the ID number corresponds to men's output m and women's output F.
If it is invalid, xxx: invalid will be output. The input may be any string. Check the format first and then calculate the check code.
The recommended design is bool is_ id_ number(const string &s); Function of form.

Input specification
Each line is a string, and the whole line is read in and processed until EOF.

Output specification
For each line of input, output a line of verification results.

Valid, please output xxx: YYYY-MM-DD M/F, and the ID number corresponds to men's output m and women's output F.
If it is invalid, xxx: invalid is output
Please replace xxx with the input string.

sample input

11010519491231002X
440524188001010019

sample output

11010519491231002X: 1949-12-31 F
440524188001010019: invalid

Idea: judge leap years and the first 17 digits

```#include <bits/stdc++.h>
using namespace std;
bool is_date(string str)
{
int num = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
string str1 = "";
for (int i = 6; i <= 13; i++)
{
str1 += str[i];
}
int a, b, c, sum = 0, d = 0;
sum = stoi(str1);
a = sum / 10000;
sum %= 10000;
b = sum / 100;
c = sum % 100;
if(a >= 1900){
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
{
if (b >= 1 && b <= 12)
{
if (c <= num[b - 1] && c >= 1)
{
d = 1;
}
}
}
else
{
if (b >= 1 && b <= 12)
{
if (c <= num[b - 1] && c >= 1)
{
d = 1;
}
}
}
}

if (d)
{
return true;
}
else
{
return false;
}
}
string date(string str)
{
string str1 = "";
for (int i = 6; i <= 9; i++)
{
str1 += str[i];
}
str1 += "-";
for (int i = 10; i <= 11; i++)
{
str1 += str[i];
}
str1 += "-";
for (int i = 12; i <= 13; i++)
{
str1 += str[i];
}
return str1;
}
bool is_number(string str)
{
int a = 1;
for (int i = 0; i < 17; i++)
{
if (str[i] < '0' || str[i] > '9')
{
a = 0;
}
}
if (a)
{
return true;
}
else
{
return false;
}
}
int main()
{
string str, str2 = "";
int sum = 0, mum;
char ch = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int num = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
while (getline(cin, str))
{
if (is_number(str))
{
sum = 0;
for (int i = 0; i < 17; i++)
{
sum += (str[i] - '0') * num[i];
}
sum = sum % 11;
if (str == ch[sum])
{
str2 = date(str);
if (is_date(str))
{
cout << str << ": ";
cout << str2 << " ";
str2 = "";
str2 = str2 + str + str + str;
mum = stoi(str2);
if (mum % 2 == 0)
{
cout << 'F' << endl;
}
else
{
cout << 'M' << endl;
}
}
else
{
cout << str << ": invalid" << endl;
}
}
else
{
cout << str << ": invalid" << endl;
}
}
else
{
cout << str << ": invalid" << endl;
}
}
return 0;
}
```

Tags: C++ Algorithm C pta

Posted by SycoSyco on Sat, 16 Apr 2022 13:20:09 +0930