身份证号码验证程序


运行代码后,输入身份证号码
正确输出Right,错误输出Wrong

验证逻辑如下:

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

[cc lang = “cpp” escaped = “true”]
#include
#include

using namespace std;

int main(void) {
int i, count = 0, sum = 0;
int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char trans[11] = {‘1’, ‘0’, ‘X’, ‘9’, ‘8’, ‘7’, ‘6’, ‘5’, ‘4’, ‘3’, ‘2’};
string id;
cin >> id;
sum = 0;
for (i = 0; i < 17; i++) {
if (!isdigit(id[i])) {
cout << id << endl;
count++;
break;
}
else
sum += (id[i]-‘0’) * weight[i];
}
if (i == 17 && id[17] != trans[sum%11])
cout << “Wrong” << endl;
else
cout << “Right” << endl;
return 0;
}

[/cc]



Leave a Reply

Your email address will not be published. Required fields are marked *