gha890826

5/13課輔-身分證字號檢驗

May 13th, 2020 (edited)
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5. int main()
  6. {
  7.     string s, num = "0123456789";
  8.     bool check = true;
  9.     cout << "nplease input ID:";
  10.     while (getline(cin, s))
  11.     {
  12.         if (s.size() != 10)
  13.         {
  14.             cout << "ID應為10碼\n";
  15.             check = false;
  16.         }
  17.         if (isalpha(s[0]))
  18.         {
  19.             if (s.find_first_not_of(num,1) != string::npos)
  20.             {
  21.                 cout << "後九碼應為數字\n";
  22.                 check = false;
  23.             }
  24.         }
  25.         else
  26.         {
  27.             cout << "第一碼應為英文字母\n";
  28.             check = false;
  29.         }
  30.  
  31.         if (check)
  32.         {
  33.             cout << "is ID\n";
  34.         }
  35.         else
  36.         {
  37.             cout << "not ID\nplease input ID:";
  38.             check=true;
  39.             continue;
  40.         }
  41.  
  42.  
  43.         int letter_number[] = { 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 };
  44.         if (islower(s[0]))
  45.         {
  46.             s[0] = toupper(s[0]);
  47.         }
  48.         int sum = letter_number[(s[0] - 'A')] / 10 + letter_number[(s[0] - 'A')] % 10 * 9;
  49.         for (int i = 1; i < 9; i++)
  50.         {
  51.             sum += (s[i] - '0')*(9 - i);
  52.         }
  53.         int checknum = (10 - (sum % 10)) % 10;
  54.         if (checknum == (s[9] - '0'))
  55.         {
  56.             cout << "true ID\n";
  57.         }
  58.         else
  59.         {
  60.             cout << "fake ID\n";
  61.         }
  62.         cout << "please input ID:";
  63.     }
  64. }
Add Comment
Please, Sign In to add comment