1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#include <iostream> #include <conio.h> #include <Windows.h> #define KEY_ESC 27 using std::cout; using std::endl; class CAccount { public: enum { STATE_IDLE, STATE_INPUT, STATE_WAIT_ACK, STATE_MAX }; public: int m_iState; CAccount(); void OnIdle(); void OnInput(); void OnWaitAck(); void SetState(int iState); void Render(); }; CAccount::CAccount() { m_iState = STATE_IDLE; } void CAccount::OnIdle() { cout<< "OnIdle()"<<endl; } void CAccount::OnInput() { cout<<"OnInput()"<<endl; } void CAccount::OnWaitAck() { cout<<"OnWaitAck"<<endl; } void CAccount::SetState(int iState) { m_iState = iState; } void CAccount::Render() { static void (CAccount::*f[])()= //배열 { &CAccount::OnIdle, &CAccount::OnInput, &CAccount::OnWaitAck }; if(m_iState >= 0 && m_iState <STATE_MAX) { (this->*f[m_iState])(); } } void main() { CAccount account; int ch=0; while(ch!=KEY_ESC) { if(kbhit()) { ch=getch(); if(ch=='1') account.SetState(CAccount::STATE_IDLE); else if(ch=='2') account.SetState(CAccount::STATE_INPUT); else if(ch=='3') account.SetState(CAccount::STATE_WAIT_ACK); } Sleep(500); account.Render(); } } |