本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

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



encryptwindow.cpp


#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


#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


#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


#include "exercise2601.h"

int main() {
   GuessGame g(4);
   g.Run();
   
   return 0;
}

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

the end

沒有留言: