{{{~cpp # include # include # include # 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= 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=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(); } }}}