返回列表 發帖
  1. /* a020: 身分證檢驗  */
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main(void){
  6.     string id; // 取得使用者輸入字串
  7.     int code[10]; //存放加權後各組數字
  8.     int total; // 存放加權數字總合
  9.     int user; // 將字母轉換成地區代碼
  10.     int num; // 存放驗證碼
  11.     int check[26] = { 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21,
  12.                       22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33};
  13.     while (cin >> id){
  14.           total = 0;//將 total 初始化
  15.           num = 0;//將 num 初始化
  16.           if (id[0] >= 65 && id[0] <= 90){ //大寫
  17.              user = id[0]-65; //取得陣列位置
  18.           }else if(id[0] >= 97 && id[0] <= 122){ //小寫
  19.                 user = id[0]-97; //取得陣列位置
  20.           }
  21.           /*開始求權數(字母部分)*/
  22.           code[0] = check[user] / 10 * 1;
  23.           code[1] = check[user] % 10 * 9;
  24.    
  25.           /* 開始求權數(整數部份) */
  26.           /*code[2] = (id[1]-48) * 8;
  27.           code[3] = (id[2]-48) * 7;
  28.           code[4] = (id[3]-48) * 6;
  29.           code[5] = (id[4]-48) * 5;
  30.           code[6] = (id[5]-48) * 4;
  31.           code[7] = (id[6]-48) * 3;
  32.           code[8] = (id[7]-48) * 2;
  33.           code[9] = (id[8]-48) * 1;*/
  34.           for (int i = 1; i <=8; i++){
  35.               code[i+1] = (id[i]-48) * (9-i);
  36.           }
  37.           /* 將權數相加存入 total */
  38.           for (int i = 0; i < 10; i++){
  39.               total += code[i];
  40.           }
  41.          
  42.           /*檢查流水號是否等於驗證碼*/
  43.           num = 10 - (total % 10);
  44.          
  45.           if (num == (id[9]-48)){
  46.              cout << "real" << endl;        
  47.           }else{
  48.                 cout << "fake" << endl;     
  49.           }

  50.                     


  51.          }
  52.     //system("pause");
  53.     return 0;
  54. }
複製代碼
我是來去無蹤的..

                                ..士豪(Alen)黑輪

TOP

返回列表