Advertisement
gha890826

去你的身分證字號檢查器

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