C language L1-016 Check ID card

L1-016 Check ID Card
A legal ID card number consists of 17 digits of region, date number and sequence number plus a 1-digit check code. The calculation rules for the check code are as follows:

First, the first 17 digits are weighted and summed, and the weight distribution is: {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; Then the calculated sum is modulo 11 to obtain the value Z; finally, the value of Z corresponds to the value of the check code M according to the following relationship:

Z: 0 1 2 3 4 5 6 7 8 9 10
M: 1 0 X 9 8 7 6 5 4 3 2
Now Given some ID numbers, please verify the validity of the verification code and output the problematic number.

Input format:
The first line of input gives a positive integer N (≤100) which is the number of entered ID number. Then there are N lines, each line gives an 18-digit ID number.

Output format:
Output one problematic ID number per line in the order of input. This does not check whether the first 17 digits are reasonable, but only checks whether the first 17 digits are all numbers and the last digit of the check code is calculated accurately. If all numbers are normal, All passed is output.

Input sample 1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
Output sample 1:
12010X198901011234
110108196711301866
37070419881216001X
Input sample 2:
2
320124198808240056
110108196711301862 All passed
Output sample 2:


#include<stdio.h>
#include <stdlib.h>

int main() {
    
    
    int N;
    int weight[17] = {
    
    7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    char verify[11] = {
    
    '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    scanf("%d", &N);
    getchar();
    char **out=(char **) malloc (N*sizeof (char *)), out_length = 0;
    for (int tmp = 0; tmp < N; tmp++) {
    
    
        int sum = 0;
        out[out_length] = (char *) calloc(19, sizeof(char));
        for (int temp = 0; temp < 17; temp++) {
    
    
            char ch = (char) getchar();
            sum += (ch - '0') * weight[temp];
            out[out_length][temp] = ch;
        }
        char ch = (char) getchar();
        if (verify[sum % 11] != ch) {
    
    
            out[out_length][17] = ch;
            out[out_length][18] = '\0';
            out_length++;
        } else
            free(out[out_length]);
        getchar();
    }
    if (out_length == 0)
        printf("All passed");
    else
        for (int tmp = 0; tmp < out_length; tmp++)
            printf("%s\n", out[tmp]);
}

Guess you like

Origin blog.csdn.net/ainuliba/article/details/133279758