身份證字號檢查

#include <iostream>
#include "stdio.h"
#include "string.h"

using namespace std;

int main(void){
                     //A   B   C   D    E   F  G   H   I   J    K  L   M   N    O  P   Q   R    S   T  U   V   W   X    Y  Z
    int letterNum[] = {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};
    char ID[80];
    char* Message;

    cout << "Input an ID to check... ";
    cin >> ID;

    int ID_Length = strlen(ID);

    if (ID_Length != 10 || //身份證長度不是10的話…
        !((ID[0] >= 'a' && ID[9] <= 'z') || (ID[0] >= 'A' && ID[9] <= 'X')) || //第1個字元不是英文字母的話
        !(ID[1] == '1' || ID[1] == '2')) //第2個字元不是1或2的話
    { 
        Message = "身份證輸入錯誤";
        goto Exit;
    }

    for (int i = 2; i <= ID_Length - 1; i++){
        if (ID[i] < '0' || ID[i] > '9'){
            Message = "身份證輸入錯誤";
            goto Exit;
        }
    }

    if (ID[0] >= 'a' && ID[0] <= 'z'){
        ID[0] -= 32;
    }

    int checkIDNum = letterNum[ID[0]-'A']/10 + (letterNum[ID[0]-'A']%10 * 9);
    checkIDNum += (ID[1]-'0') * 8;

    for (int i = 7;  i >= 1; i--){
        checkIDNum += (ID[9-i]-'0') * i;
    }

    checkIDNum %= 10;

    if (checkIDNum == 0) checkIDNum = 0;
    else checkIDNum = 10 - checkIDNum;

    if (checkIDNum == (ID[9]-'0')) Message = "身份證字號正確";
    else Message = "身份證字號錯誤";


Exit:
    cout << endl<< Message;


}

results matching ""

    No results matching ""