计算公民身份号码校验码(C++)

输入

公民身份号码的本体码(master number),身份证前17位数字,6位数字地址码 + 8位数字出生日期码 + 3位数字顺序码

输出

公民身份号码的校验码(check number),身份证第18位(最后一位)数字,用来验证本体码的录入或转录过程准确性的号码,1位数字校验码

C++实现代码

#include <iostream>

int main()
{
	uint64_t inID = 0;

	std::cout << "Enter the master number of the citizen ID: ";
	std::cin >> inID;
	if (11000000000101000 <= inID && inID <= 82999999991231999) {
		const int w[17] = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 };
		int s = 0;
		uint64_t id = inID;

		for (int i = 0; i < 17; ++i) {
			s += (id % 10) * w[i];
			id /= 10;
		}

		const char cn[11] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

		std::cout << std::endl << "The citizen ID check number is: " << cn[s % 11] << std::endl;
		return 0;
	}

	std::cerr << std::endl
		<< "The master number is incorrect." << std::endl
		<< "It must have 17 digits (address code + YYYYMMDD + seq, 6+8+3)." << std::endl;
	return 1;
}

参考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章