Advertisement
wendy890711

判斷字串<身分證>

May 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cctype>
  5. using namespace std;
  6.  
  7. bool checkID(string idStr)
  8. {
  9. int letterNums[] = { 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 };
  10. if (islower(idStr[0]))
  11. idStr[0] = toupper(idStr[0]);
  12.  
  13. int total = (letterNums[idStr[0] - 'A'] / 10) + (letterNums[idStr[0] - 'A'] % 10) * 9;
  14. for (int i = 1; i < 9; i++)
  15. total += (idStr[i] - '0')*(9 - i);
  16.  
  17. int checkNum = (10 - total % 10) % 10;
  18. if (checkNum == (idStr[9] - '0'))
  19. return true;
  20. else
  21. return false;
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27. string s, t = "01234546789";
  28. int i;
  29. for (i = 0; i < 3; i++)
  30. {
  31. cout << "請輸入身分證字號" << endl;
  32. cin >> s;
  33.  
  34. if (s.size() != 10)
  35. {
  36. cout << "不正確,請重新輸入" << endl;
  37. }
  38. else
  39. {
  40. cout << "總共10字元,正確" << endl;
  41. break;
  42. }
  43. }
  44. if (i == 3)
  45. cout << "錯誤達三次,請稍後再試" << endl;
  46. while (i < 3)
  47. {
  48. unsigned int j, count = 0, pos = 1;
  49. while ((j = s.find_first_of(t, pos)) != string::npos)
  50. {
  51. count++;
  52. pos = pos + 1;
  53. }
  54. cout << "在" << s << "中";
  55. if (count == 0)
  56. {
  57. cout << "沒有數字字元" << endl;
  58. i = 4;
  59. }
  60. else
  61. {
  62. cout << "有" << count << "個數字字元" << endl;
  63. i = 4;
  64. }
  65. }
  66.  
  67. if (checkID(s) == 1)
  68. cout << "此身分證字號為合法的身分證字號" << endl;
  69. else
  70. cout << "此身分證不合法" << endl;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement