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



encrypt03.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 引入標準程式庫中的 cstdlib 及 ctime
#include <cstdlib> // srand(), rand()
#include <ctime>   // time()
 
// 引入標準程式庫中的 iostream
#include <iostream> // cout, endl
 
// 引入 Encrypt 類別的標頭檔
#include "encrypt01.h"
 
// 使用 std 中的兩個名稱
using std::cout; // 標準輸出串流的物件
using std::endl; // 新行符號,等於 '\n'
 
// 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;
   cout << a << ", "; // 印出 a 的值
   int b = rand() % 10;
   cout << b << ", "; // 印出 b 的值
 
   // 利用公式建立密碼表字串    
   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;
}
 
/* encrypt03.cpp
   Kaiching Chang
   2014-5 */

encrypt04.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 引入標準程式庫中的 cstdlib 及 ctime
#include <cstdlib> // srand(), rand()
#include <ctime>   // time()
 
// 引入標準程式庫中的 iostream
#include <iostream> // cout, endl
 
// 引入 Encrypt 類別的標頭檔
#include "encrypt01.h"
 
// 使用 std 中的兩個名稱
using std::cout; // 標準輸出串流的物件
using std::endl; // 新行符號,等於 '\n'
 
// Encrypt 的建構函數
Encrypt::Encrypt() {
   // 呼叫 setter 設定 code_array
   set_code_array();   
}
 
// 設定 code_array 的 setter 成員函數
void Encrypt::set_code_array() {
   // 初始化擬隨機數產生器
   srand(time(0));
     
   // 取得 a 、 b 值,其中 a 不等於 0 或偶數
   int a = 0;
   int b = 0;
   while (a % 2 == 0) {
      // rand() 回傳 0 到 RAND_MAX 之間的擬隨機整數
      a = rand() % 10;
      b = rand() % 10;
   }
     
   // 印出 a 、 b 值
   cout << a << ", ";
   cout << b << ", ";
 
   // 利用公式建立密碼表字串    
   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;
}
 
/* encrypt04.cpp
   Kaiching Chang
   2014-5 */

exercise1701.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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;
   }
}
 
// 計算 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;
      }
   }
}
 
/* 檔名: exercise1701.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

exercise1702.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
  
using namespace std;
 
// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number, int length) {
   int i;
   int j = 1000;
   int temp = number;
   for (i = 0; i < length; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}
 
// 計算 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, 4);
    
   int times = 0;
   while (true) {
      times++;
    
      int guess;
      cin >> guess;
      int guess_array[4];
      ArrayNumber(guess_array, guess, 4);
     
      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;
      }
   }
}
 
/* 檔名: exercise1702.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

exercise1703.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <cmath>
  
using namespace std;
 
// 將整數拆成整數陣列的函數
void ArrayNumber(int array[], int number, int length) {
   int i;
   int j = pow(10, length - 1);
   int temp = number;
   for (i = 0; i < length; i++) {
      array[i] = temp / j;
      temp %= j;
      j /= 10;
   }
}
 
// 計算 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, 4);
    
   int times = 0;
   while (true) {
      times++;
    
      int guess;
      cin >> guess;
      int guess_array[4];
      ArrayNumber(guess_array, guess, 4);
     
      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;
      }
   }
}
 
/* 檔名: exercise1703.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

the end

沒有留言: