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



encryptwindow.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "encryptwindow.h"
#include "ui_encryptwindow.h"
 
EncryptWindow::EncryptWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::EncryptWindow)
{
    ui->setupUi(this);
}
 
EncryptWindow::~EncryptWindow()
{
    delete ui;
}

exercise2601.h


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
#include <string>
 
using namespace std;
 
// 宣告 GuessGame 類別
class GuessGame {
public:
   // 宣告建構函數
   GuessGame(int);
   // 存取函數與修改函數
   void set_game();
   string get_answer();
   int get_times();
   int get_a();
   int get_b();
   // 工具函數
   bool FindNumber(string, int, char);
   void ABCounter(string, string);
   // 命令列遊戲版本
   void Run();
  
private:
   // 資料成員區
   string answer;
   int length;
   string guess;
   int a;
   int b;
   int times;
 
};
 
/* 檔名: exercise2601.h
   作者: Kaiching Chang
   時間: 2014-5 */

exercise2601.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <iostream>
 
#include "exercise2601.h"
 
int myrandom(int i) {
   return rand() % i;
}
 
GuessGame::GuessGame(int digit) {
   if (digit < 3 || digit > 6) {
      length = 4;
   }
   else {
      length = digit;
   }
    
   set_game();
}
 
void GuessGame::set_game() {
   srand(time(0));
 
   string result = "0123456789";
   while (true) {
      random_shuffle(result.begin(), result.end(), myrandom);
       
      if (result.at(0) != '0') {
         break;
      }
   }
     
   answer = result.substr(0, length);
   times = 0;
   a = 0;
   b = 0;
}
 
string GuessGame::get_answer() {
   return answer;
}
 
int GuessGame::get_times() {
   return times;
}
 
int GuessGame::get_a() {
   return a;
}
 
int GuessGame::get_b() {
   return b;
}
 
bool GuessGame::FindNumber(string array, int index, char number) {
   for (int i = 0; i < length; i++) {
      if (i == index) {
         continue;
      }
         
      if (array[i] == number) {
         return true;
      }
   }
     
   return false;
}
 
void GuessGame::ABCounter(string array1, string array2) {
   for (int i = 0; i < length; i++) {
      for (int j = 0; j < length; j++) {
         if (array1[i] == array2[j]) {
            if (i == j) {
               a++;
            }
            else {
               b++;
            }
         }
      }
   }
}
 
void GuessGame::Run() {
   while (true) {
      // 計算次數
      times++;
       
      // 接受猜測
      cin >> guess;
       
      // 檢查使用者是否輸入四個不同數字
      bool state = false;
      for (int i = 0; i < 4; i++) {
         if (FindNumber(guess, i, guess[i])) {
            state = true;
         }
      }
       
      if (state) {
         cout << "Please guess 4 different numbers!!" << endl;
         continue;
      }
       
      // 檢查使用者是否輸入過少或過多的數字
      if (guess.length() != 4) {
         cout << "Please input 4 numbers!!" << endl;
         continue;
      }
       
      // 計算 A 、 B 值
      ABCounter(answer, guess);
 
      // 判斷使用者是否猜對
      if (answer == guess) {
         cout << "Right!! You guess " << times << " times!!" << endl;
         break;
      }
      else {
         cout << "Wrong!! " << a << "A" << b << "B!!" << endl;
         a = 0;
         b = 0;
      }
   }
}
 
/* 檔名: exercise2601.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

exercise2602.cpp


1
2
3
4
5
6
7
8
9
10
11
12
#include "exercise2601.h"
 
int main() {
   GuessGame g(4);
   g.Run();
    
   return 0;
}
 
/* 檔名: exercise2602.cpp
   作者: Kaiching Chang
   時間: 2014-5 */

the end

沒有留言: