检查身份证号

#include <string.h>
#include <iostream>
#include <cctype>
bool CheckIdentification(const char *identification) {
    static const ushort s_weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    static const char s_verified[11]= {'1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2'};
    int len = ::strlen(identification) - 1;
    if (17 != len) {
        return false;
    }
    unsigned sum = 0;
    for (int i = 0;i < len;i++) {
        if (false == isdigit(identification[i])) {
            return false;
        }
        sum += (identification[i] - '0') * s_weight[i];
    }
    return identification[len] == s_verified[sum % 11];
}
int main() {
    std::cout << CheckIdentification("341102298311186214") << std::endl;

    return 0;
}

 

上一篇:Spring Security:使用@PreAuthorize注释不保护方法


下一篇:ssh登陆概率性失败,报错:kex_exchange_identification