E D R , A S I H C RSS

Vending Machine_참관자

~cpp 
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# define HELLO 1
# define PUT	2
# define RETURN	3
# define PUSH	4
# define OFF	5 
# define TOKEN_NUM	5
# define MENU_NUM	5

char *M_Name[]={"cup","cococa","water","coffee"};

char * tok[]={
		"hello",
		"put",
		"return",
		"push",
		"off"
};



static char* seps=" \t\n";

class CommandParser{
private:
	char InputStr[100];
	char * Data[20];
	int index;
public:
	int ReceiveCommand();
	char * GetNextToken();
};

class VendingMachine{
private:
	int Money;
	CommandParser p;	
	int MinPrice;
	int Menu_num;
	char Menu[10][100];
	int Price[10];
	int M[10];

public:
	void SetMenuM(int i);
	void AvailMenuPrint();
	void AddingMenu(char * name,int price);
	VendingMachine();
	void ProcessMoney();
	void ProcessReturn();
	void ProcessPush();
	void On();
};


int CommandParser::ReceiveCommand()
{
	index=0;
	char * token;
	
	printf("input command:");
	gets(InputStr);
	token = strtok( InputStr,seps);
	while(token!=NULL){
		
		Data[index]=token;
		index++;
		token=strtok(NULL,seps);
	}

	index=0;
	for(int i=1;i<=TOKEN_NUM && strcmp( Data[0] , tok[i-1]) ;i++) ;
	if(i<=TOKEN_NUM) return i;
	return 0;
}

char *CommandParser::GetNextToken()
{
	return Data[++index];
}

void VendingMachine::SetMenuM(int i)
{
	M[Menu_num]=i;
}

void VendingMachine::AvailMenuPrint()
{
	for(int i=0;i<Menu_num;i++){
		if (Money >= Price[i]){
			printf("%d : %s %d\n",i,Menu[i],Price[i]);
		}
	}
}
void VendingMachine::AddingMenu(char * name, int price)
{
	strcpy(Menu[Menu_num],name);
	Price[Menu_num]=price;
	Menu_num++;
	if (price<MinPrice) MinPrice=price;
}

VendingMachine::VendingMachine()
{
	Money=0;
	Menu_num=0;
	MinPrice=100000;
	AddingMenu("cocoa",200);
	SetMenuM(321);
	AddingMenu("coffe",200);
	SetMenuM(341);
	AddingMenu("cocoa",200);
	AddingMenu("cocoa",200);
	AddingMenu("cocoa",200);
	

}

void VendingMachine::ProcessMoney()
{
	char * t = p.GetNextToken();
	int m = atoi(t);
	Money+=m;
	printf("Current Money is %d\n",Money);
	AvailMenuPrint();
}

void VendingMachine::ProcessPush()
{
	if (Money<MinPrice) return;
	char * t = p.GetNextToken();
	int SM=atoi(t);
	if (Money >=Price[SM]){
		printf("재료:");
		int a = M[SM+1];
		int b;
		while(a){
			b=a%10;
			a=a/10;
			printf("%s  ",M_Name[b-1]);
		}
		printf("\nReceive %s\n",Menu[SM] );

		Money-=Price[SM];
	}
	printf("CurrentMoney is %d\n",Money);
	AvailMenuPrint();
}

void VendingMachine::ProcessReturn()
{
	printf("Returned Money is %d\n",Money);
	Money=0;
}

void VendingMachine::On()
{
	int command;
	while( command = p.ReceiveCommand() ){
		printf("%d\n",command);
		switch(command){
		case HELLO:
			printf("this machine is good^^;;\n");
			break;
		case PUT:
			ProcessMoney();
			break;
		case RETURN:
			ProcessReturn();
			break;
		case PUSH:
			ProcessPush();
			break;
		case OFF:
			return;

		default:
			break;
		};
	}
}

void main(void)
{
	VendingMachine v;
	v.On();
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0814 sec