看板 CT21th339 關於我們 聯絡資訊
#include <iostream> #include <time.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> using namespace std; class Guess { private: int guess[4]; int key[4]; int temp, GuessTimes, MaxGuessTimes, A, B; public: Guess(); void FullNum(); // Full the guess array. void GuessLoop(); // Guess loop. }; Guess::Guess() { // Initialize guess and key array. for(int i = 0; i < 4; i++) { guess[i] = 10; key[i] = 10; } // Set Max guess time. MaxGuessTimes = 10; cout << "§§猜數字,有 " << MaxGuessTimes << " 次機會§§"; } void Guess::FullNum() { // Put srand(). srand(time(NULL)); // Full the guess array. for(int i = 0; i < 4; i++) { do { temp = (rand() % 10); }while(temp == guess[0] || temp == guess[1] || temp == guess[2] || temp == gu ess[3]); guess[i] = temp; } } void Guess::GuessLoop() { bool end = false; int i; // Guess loop. GuessTimes = 0; while(end == false) { // Clean the key array and A, B, temp. A = 0; B = 0; temp = 10; for(i = 0; i < 4; i++) key[i] = 10; // Guess times GuessTimes++; cout << endl << "第" << GuessTimes << "次:" << endl; // Input guess numbers. for(i = 0; i < 4; i++) { do { do { temp = getch(); temp = temp - 48; }while(temp < 0 || temp > 9); }while(temp == key[0] || temp == key[1] || temp == key[2] || temp == key[3]); key[i] = temp; printf("%d", temp); } // Check the numbers. for(i = 0; i < 4; i++) { if(key[i] == guess[i]) A++; else { for(int j = 0; j < 4; j++) { if(key[i] == guess[j]) { B++; break; } } } } // Out Put the result. if(A == 4) { cout << " 正確答案!" << endl << "共猜 " << GuessTimes << " 次"; end = true; } else { cout << " A " << A << " B " << B; } if(GuessTimes >= MaxGuessTimes) { cout << endl << endl << "正確答案 :"; for(i = 0; i < 4; i++) cout << guess[i]; cout << endl << "機會用完, 你輸了"; end = true; } } } int main() { Guess guess; // Full random numbers. guess.FullNum(); // Start Guess. guess.GuessLoop(); cout << endl; getch(); return 0; } ※ 引述《farewellwen (無情 / 厄運)》之銘言: : ※ 引述《handsomecrow (湯姆克魯斯)》之銘言: : : 我試試看... : 救我救我... -- 中文書是什麼..可以吃嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.134.242.70
naughtyboy:阿彭好強...@@ 推 218.187.36.116 03/06
naughtyboy:以後我C++不會就問你囉...^^ 推 218.187.36.116 03/06
hsudada:我上學期的作業...不早問....ㄏㄏ 推 61.229.196.201 03/07
yhhy:哈 這學期我終於不用學c了XD 推 163.25.118.9 03/07