計算公民身份號碼校驗碼(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;
}

參考

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