检查身份证号
作者:互联网
#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;
}
标签:false,检查,int,sum,身份证号,len,identification,const 来源: https://blog.csdn.net/wangzhicheng2013/article/details/100122511