Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- bool checkID(string idStr)
- {
- 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 };
- if (islower(idStr[0]))
- idStr[0] = toupper(idStr[0]);
- int total = (letterNums[idStr[0] - 'A'] / 10) + (letterNums[idStr[0] - 'A'] % 10) * 9;
- for (int i = 1; i < 9; i++)
- total += (idStr[i] - '0')*(9 - i);
- int checkNum = (10 - total % 10) % 10;
- if (checkNum == (idStr[9] - '0'))
- return true;
- else
- return false;
- }
- int main()
- {
- string s, t = "01234546789";
- int i;
- for (i = 0; i < 3; i++)
- {
- cout << "請輸入身分證字號" << endl;
- cin >> s;
- if (s.size() != 10)
- {
- cout << "不正確,請重新輸入" << endl;
- }
- else
- {
- cout << "總共10字元,正確" << endl;
- break;
- }
- }
- if (i == 3)
- cout << "錯誤達三次,請稍後再試" << endl;
- while (i < 3)
- {
- unsigned int j, count = 0, pos = 1;
- while ((j = s.find_first_of(t, pos)) != string::npos)
- {
- count++;
- pos = pos + 1;
- }
- cout << "在" << s << "中";
- if (count == 0)
- {
- cout << "沒有數字字元" << endl;
- i = 4;
- }
- else
- {
- cout << "有" << count << "個數字字元" << endl;
- i = 4;
- }
- }
- if (checkID(s) == 1)
- cout << "此身分證字號為合法的身分證字號" << endl;
- else
- cout << "此身分證不合法" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement