encrypt02.cpp
// 引入標準程式庫中的 cstdlib 及 ctime #include <cstdlib> // srand(), rand() #include <ctime> // time() // 引入 Encrypt 類別的標頭檔 #include "encrypt01.h" // Encrypt 的建構函數 Encrypt::Encrypt() { // 呼叫 setter 設定 code_array set_code_array(); } // 設定 code_array 的 setter 成員函數 void Encrypt::set_code_array() { // 初始化擬隨機數產生器 srand(time(0)); // 取得 a 、 b 值 int a = rand() % 10; int b = rand() % 10; // 利用公式建立密碼表字串 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 += (char) m + 97; c++; } // 將建立好的密碼表直接設定給成員變數 code_array = s; } // 回傳密碼表字串的 getter 成員函數 string Encrypt::get_code_array() { return code_array; } /* encrypt02.cpp Kaiching Chang 2014-5 */
exercise1601.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; } } /* 檔名: exercise1601.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise1602.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; } } /* 檔名: exercise1602.cpp 作者: Kaiching Chang 時間: 2014-5 */
exercise1603.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; } } } /* 檔名: exercise1603.cpp 作者: Kaiching Chang 時間: 2014-5 */
the end
沒有留言:
張貼留言