2010年9月19日 星期日

簡易猜數字

//newGame.h//
#pragma once

class newGame
{
public:
newGame(void);
virtual void gameStart(void) = 0;
virtual void setGame(int nums) = 0;
virtual void inGame(void) = 0;
virtual ~newGame(void){}
};

class guessNumberGame : public newGame
{
public:
guessNumberGame(void);
void gameStart(void);
void setGame(int nums);
void inGame(void);
void examNum(void);
virtual ~guessNumberGame(void);
private:
int _nums;
int _A,_B;
char* _secretNum;
char* _guessNum;

};

//newGame.cpp//
#include
#include
#include "newGame.h"

newGame::newGame(void)
{
}

guessNumberGame::guessNumberGame(void)
{
gameStart();
}

guessNumberGame::~guessNumberGame(void)
{
}

void guessNumberGame::gameStart(void)
{
int nums;
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
while (scanf("%d",&nums))
{
if (nums==0) break;
if (nums<4 || nums>6)
{
printf("Wrong input!\n");
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
continue;
}
_nums=nums;
setGame(_nums);
inGame();
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
}
printf("Exit the game. Thank you for playing.\n");
system("pause");
}

void guessNumberGame::setGame(int _nums)
{
_secretNum = new char[_nums];
_guessNum = new char[_nums];
int arr[10];
int j=0,tmp;
srand(time(NULL));
for (int i=0;i<10;i++)
{
arr[i]=i;
}
while(j<_nums)
{
tmp = rand() % 10;
if (arr[tmp]>-1)
{
_secretNum[j]=arr[tmp];
arr[tmp]=-1;
j++;
}
}
printf("secretNum = ");
for (int i=0;i<_nums;i++)
printf("%d",_secretNum[i]);
printf("\n");
}

void guessNumberGame::inGame(void)
{
int chance = 10;
for (int i=0;i {
printf("Enter your guess(%d):",i+1);
flushall();
for (int j=0;j<_nums;j++)
{
scanf("%c",(_guessNum+j));
}
examNum();
printf("%dA%dB\n",_A,_B);
if (_A == _nums)
{
printf("You win!\n");
break;
}
//for (int j=0;j<_nums;j++)
//{
// printf("%c",_guessNum[j]);
//}
}
}

void guessNumberGame::examNum(void)
{
_A = _B = 0;
for (int i=0;i<_nums;i++)
{
for (int j=0;j<_nums;j++)
{
if (_secretNum[i]+48==_guessNum[j])
{
_B++;
if (i==j) _A++;
continue;
}
}
}
}

int main()
{
guessNumberGame game;
return 0;
}

2010年9月1日 星期三

ACM102

資源回收題
題目from lucky貓
http://www.tcgs.tc.edu.tw/~sagit/luckycat/q102.htm

code:
#include
using namespace std;

#define MAX_BIN 3 //max number of bins
#define MAX_TYPE 3 //max number of type

enum bottleType {B,C,G};

class bin{
public:
bin(): _type(0) {}
char getType(){return _type;}
void modifyType(char c){_type=c;}
//void setBottle(int in,int i){}
private:
char _type;
int _stock[MAX_TYPE]; //bottle stock (order:BCG)


friend class binWorld;
};

class binWorld{
public:
binWorld();
char returnType(int j);
void setBottle(int i, int j, int in);
void print(); //print result
void searchMax(); //search largest bottle numbers in each bin (under condition of _type not been defined)
void moveBottle(); //move bottles

private:
bin* _binPtr;
int _move_count;
int _max;
int _bin_num;
int _stock_num;
bool* _isDefine;
};



binWorld::binWorld(){
_binPtr= new bin[MAX_BIN];
_isDefine= new bool[MAX_TYPE];
_move_count=0;
_max=0;
_bin_num=-1;
_stock_num=-1;
for (int j=0;j}

char binWorld::returnType(int j){
if (j==1) return 'C';
else if (j==0) return 'B';
else return 'G';

}

void binWorld::setBottle(int i, int j, int in){
if (j==1) (_binPtr+i)->_stock[G]=in;
else if (j==0) (_binPtr+i)->_stock[B]=in;
else (_binPtr+i)->_stock[C]=in;
//(_binPtr+i)->_stock[returnType(j)]=in;
}

void binWorld::print(){
for (int i=0;i_type);
printf(" %d\n",_move_count);
}

//modify max & type number
void binWorld::searchMax(){
_max=-1;
for (int i=0;i if (!(_binPtr+i)->_type) {
for (int j=0;j if (_max<(_binPtr+i)->_stock[j] && *(_isDefine+j)==false){
_max=(_binPtr+i)->_stock[j];
_bin_num=i;
_stock_num=j;
}
}
}
}
(_binPtr+_bin_num)->_type=returnType(_stock_num);
*(_isDefine+_stock_num)=true;
}

void binWorld::moveBottle(){
for (int i=0;i if (i==_bin_num) continue;
_move_count+=(_binPtr+i)->_stock[_stock_num];
}
}

int main(){
char bottles[30];
int in;
FILE *f;
fopen_s(&f,"ACM102.txt","rt");
if (f==NULL) {printf("Error open file."); return -1;}
while (!feof(f)){
binWorld binWorld;
//load input (one line)
printf("Input:");
for (int i=0;i for (int j=0;j fscanf(f,"%d",&in);
binWorld.setBottle(i,j,in);
printf("%d ",in);
}
}

for (int i=0;i binWorld.searchMax();
//binWorld.tprint();
binWorld.moveBottle();
}
printf("\nOutput:");
binWorld.print();
}
fclose(f);
}