C++ 入門指南 V2.00 - 單元 15 範例及練習程式碼



encrypt01.cpp


// 引入 Encrypt 類別的標頭檔 
#include "encrypt01.h"

// Encrypt 的建構函數 
Encrypt::Encrypt() {
   // 呼叫 setter 設定 code_array
   set_code_array();    
}

// 設定 code_array 的 setter 成員函數
void Encrypt::set_code_array() {
   // 設定 a 、 b 值 
   int a = 3;
   int b = 5;

   // 利用公式建立密碼表字串     
   int x, y, m;
   char c = 'a';
   string s;
   int i;
   for (i = 0; i < 26; i++) {
      x = c; 
      y = x * a + b;
      m = y % 26;
      s += m + 97; 
      c++; 
   }

   // 將建立好的密碼表直接設定給成員變數
   code_array = s;
}

// 回傳密碼表字串的 getter 成員函數     
string Encrypt::get_code_array() {
   return code_array;
}

/* encrypt01.cpp 
   Kaiching Chang 
   2014-5 */

encrypt_demo01.cpp


// 引入標準程式庫中的 iostream
#include <iostream>
// 引入 Encrypt 類別的標頭檔
#include "encrypt01.h"

// 使用 std 中的兩個名稱
using std::cout; // 標準輸出串流的物件
using std::endl; // 新行符號,等於 '\n'

// 程式執行的 main() 函數 
int main() {
   // 印出空白一行
   cout << endl;

   // 建立 Encrypt 物件
   Encrypt encryptor; 
   // 印出密碼表字串
   cout << encryptor.get_code_array()
        << endl << endl;

   return 0;
}
 
/* encrypt_demo01.cpp 
   Kaiching Chang 
   2014-5 */

exercise1501.cpp


#include <iostream>
 
using namespace std;

// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < 4; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}
 
int main(void) {
   int answer = 1234;
   int answer_array[4];
   ArrayNumber(answer_array, answer);
   
   int guess;
   cin >> guess;
   int guess_array[4];
   ArrayNumber(guess_array, guess);
   
   int number = 0;
   for (int i = 0; i < 4; i++) {
      if (guess_array[i] == answer_array[i]) {
         number++;
      }
   }
   
   if (number == 4) {
      cout << "Right!!" << endl;
   }
   else {
      cout << "Wrong!!" << endl;
   }
}

/* 檔名: exercise1501.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1502.cpp


#include <iostream>
 
using namespace std;

// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < 4; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}

// 測試猜測與答案是否相等
bool EqualTest(int array1[], int array2[]) {
   int number = 0;
   for (int i = 0; i < 4; i++) {
      if (array1[i] == array2[i]) {
         number++;
      }
   }
   
   if (number == 4) {
      return true;
   }
   else {
      return false;
   }
}
 
int main(void) {
   int answer = 1234;
   int answer_array[4];
   ArrayNumber(answer_array, answer);
   
   int guess;
   cin >> guess;
   int guess_array[4];
   ArrayNumber(guess_array, guess);

   if (EqualTest(answer_array, guess_array)) {
      cout << "Right!!" << endl;
   }
   else {
      cout << "Wrong!!" << endl;
   }
}

/* 檔名: exercise1502.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1503.cpp


#include <iostream>
 
using namespace std;

// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < 4; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}

// 測試猜測與答案是否相等
bool EqualTest(int array1[], int array2[]) {
   int number = 0;
   for (int i = 0; i < 4; i++) {
      if (array1[i] == array2[i]) {
         number++;
      }
   }
   
   if (number == 4) {
      return true;
   }
   else {
      return false;
   }
}

// 計算 A 、 B 值
void ABCounter(int array1[], int array2[], int array3[]) {
   int a = 0;
   int b = 0;
   for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
         if (array1[i] == array2[j]) {
            if (i == j) {
               a++;
            }
            else {
               b++;
            }
         }
      }
   }
   
   array3[0] = a;
   array3[1] = b;
} 
 
int main(void) {
   int answer = 1234;
   int answer_array[4];
   ArrayNumber(answer_array, answer);
   
   int guess;
   cin >> guess;
   int guess_array[4];
   ArrayNumber(guess_array, guess);
   
   int ab_array[2];
   ABCounter(answer_array, guess_array, ab_array);

   if (answer == guess) {
      cout << "Right!!" << endl;
   }
   else {
      cout << "Wrong!! " << ab_array[0] << "A" << ab_array[1] << "B!!" << endl;
   }
}

/* 檔名: exercise1503.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1504.cpp


#include <iostream>
 
using namespace std;

// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < 4; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}

// 測試猜測與答案是否相等
bool EqualTest(int array1[], int array2[]) {
   int number = 0;
   for (int i = 0; i < 4; i++) {
      if (array1[i] == array2[i]) {
         number++;
      }
   }
   
   if (number == 4) {
      return true;
   }
   else {
      return false;
   }
}

// 計算 A 、 B 值
void ABCounter(int array1[], int array2[], int array3[]) {
   int a = 0;
   int b = 0;
   for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
         if (array1[i] == array2[j]) {
            if (i == j) {
               a++;
            }
            else {
               b++;
            }
         }
      }
   }
   
   array3[0] = a;
   array3[1] = b;
} 
 
int main(void) {
   int answer = 1234;
   int answer_array[4];
   ArrayNumber(answer_array, answer);
   
   while (true) {
      int guess;
      cin >> guess;
      int guess_array[4];
      ArrayNumber(guess_array, guess);
    
      int ab_array[2];
      ABCounter(answer_array, guess_array, ab_array);

      if (answer == guess) {
         cout << "Right!!" << endl;
         break;
      }
      else {
         cout << "Wrong!! " << ab_array[0] << "A" << ab_array[1] << "B!!" << endl;
      }
   }
}

/* 檔名: exercise1504.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1505.cpp


#include <iostream>
 
using namespace std;

// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < 4; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}

// 測試猜測與答案是否相等
bool EqualTest(int array1[], int array2[]) {
   int number = 0;
   for (int i = 0; i < 4; i++) {
      if (array1[i] == array2[i]) {
         number++;
      }
   }
   
   if (number == 4) {
      return true;
   }
   else {
      return false;
   }
}

// 計算 A 、 B 值
void ABCounter(int array1[], int array2[], int array3[]) {
   int a = 0;
   int b = 0;
   for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
         if (array1[i] == array2[j]) {
            if (i == j) {
               a++;
            }
            else {
               b++;
            }
         }
      }
   }
   
   array3[0] = a;
   array3[1] = b;
} 
 
int main(void) {
   int answer = 1234;
   int answer_array[4];
   ArrayNumber(answer_array, answer);
   
   int times = 0;
   while (true) {
      times++;
   
      int guess;
      cin >> guess;
      int guess_array[4];
      ArrayNumber(guess_array, guess);
    
      int ab_array[2];
      ABCounter(answer_array, guess_array, ab_array);

      if (answer == guess) {
         cout << "Right!! You guess " << times << " times!!" << endl;
         break;
      }
      else {
         cout << "Wrong!! " << ab_array[0] << "A" << ab_array[1] << "B!!" << endl;
      }
   }
}

/* 檔名: exercise1505.cpp 
   作者: Kaiching Chang 
   時間: 2014-5 */

the end

沒有留言: