根据身份证前十七位判断最后一位是否正确

img


编译正确并且有几组数据也正确但是总是有一组数据过不了,想知道是哪儿错了吗,为什么总是部分正确

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

// 身份证号码校验函数
bool check_id_card(const char* id_card) {
    // 校验参数
    if (id_card == NULL || strlen(id_card) != 18) {
        return false;
    }
    // 系数表
    int factors[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    // 校验码表
    char check_codes[] = "10X98765432";
    // 计算校验和
    int sum = 0;
    for (int i = 0; i < 17; i++) {
        if (id_card[i] >= '0' && id_card[i] <= '9') {
            sum += (id_card[i] - '0') * factors[i];
        } else {
            return false;
        }
    }
    // 计算校验码
    int remainder = sum % 11;
    char check_code = check_codes[remainder];
    // 判断校验码是否正确
    return (id_card[17] == check_code);
}

// 测试函数
int main() {
    char id_card[] = "110101199003074116";
    bool result = check_id_card(id_card);
    printf("身份证号码 %s 正确:%s\n", id_card, result ? "是" : "否");
    return 0;
}

试一试这个