身份证验证程序(C++版本)

/

  1. #include <array>
  2. #include <ctime>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iomanip>
  8. #include <iostream>
  9. #include <windows.h>
  10. using namespace std;
  11. //以下是常量 - 全局 - 数组 - 一维
  12. /** 各位数的系数 */
  13. int const value_coefficient[] = { 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 };
  14. /** 校对码 */
  15. int const value_corresponding[] = { 1 , 0 , 'X' , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 };
  16. //以下是变量 - 全局 - 整型
  17. /** 临时变量1 */
  18. int temp1;
  19. /** 临时变量2 */
  20. int temp2;
  21. //以下是函数 - 整型
  22. /** 等待x秒 */
  23. int sleep( int );
  24. //以下是函数 - 字符串型
  25. /** 身份证号码 */
  26. char char_ID[19];
  27. //以下是函数 - 无类型
  28. /** 开始语 */
  29. void function_Start( );
  30. /** 结束语 */
  31. void function_Finish( );
  32. /** 总输入 */
  33. void function_Input( );
  34. /** 总输出 */
  35. void function_Output( );
  36. /** 清空界面 */
  37. void function_Clear( );
  38. /** 验证 */
  39. void function_Compute( );
  40. /** 输入是否合法 */
  41. void function_Input_violation( );
  42. int main()
  43. {
  44. SetConsoleTitle("身份证验证");
  45. // function_Start( );
  46. function_Input( );
  47. function_Compute( );
  48. // function_Finish( );
  49. return 0;
  50. }
  51. int function_sleep( int x )
  52. {
  53. for( int i = 0 ; i < x ; i++ )
  54. Sleep(1000);
  55. }
  56. void function_Clear()
  57. {
  58. system( "pause" );
  59. system( "cls" );
  60. }
  61. void function_Start( )
  62. {
  63. cout << left;
  64. cout << "欢迎进入验证天地!!!" << endl;
  65. function_sleep( 3 );
  66. cout << "下面是验证身份证号码是否正确!!!" << endl;
  67. function_sleep( 1.5 );
  68. function_Clear( );
  69. }
  70. void function_Finish( )
  71. {
  72. cout << left;
  73. cout << "感谢你光临验证天地!!!" << endl;
  74. function_sleep( 3 );
  75. cout << "希望你再次光临!!!" << endl;
  76. function_sleep( 2 );
  77. function_Clear( );
  78. }
  79. void function_Input_violation( )
  80. {
  81. for( int i = 0 ; char_ID[i] != '\0' ; i++ )
  82. temp1 = i;
  83. if( temp1 != 17 )
  84. {
  85. cout << "此身份证号码不合法!" << endl;
  86. cout << "原因可能是:" << endl;
  87. cout << "再";
  88. temp2 = 1;
  89. }
  90. else
  91. {
  92. cout << "验证通过!" << endl;
  93. temp2 = 0;
  94. }
  95. }
  96. void function_Input( )
  97. {
  98. do
  99. {
  100. cout << "请输入你的身份证号码(18位):";
  101. gets(char_ID);
  102. cout << "正在验证此身份证号码是否合法中......" << endl;
  103. function_Input_violation( );
  104. }while( temp2 == 1 );
  105. cout << "正在计算中......" << endl;
  106. }
  107. void function_Compute( )
  108. {
  109. int Answer1 , Answer2;
  110. Answer1 = Answer2 = 0;
  111. for( int i = 0 ; i < 17 ; i++ )
  112. Answer1 += ( char_ID[i] - 48 ) * value_coefficient[i];
  113. Answer2 = Answer1 % 11;
  114. if( (char_ID[17] - 48) == value_corresponding[Answer2] )
  115. {
  116. cout << "验证正确!";
  117. temp2 = 1;
  118. }
  119. else
  120. {
  121. cout << "验证失败!";
  122. temp2 = 0;
  123. }
  124. }

转载请注明作者和出处,并添加本页链接。
原文链接: 139.196.92.124/post/103