// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 289. #include using namespace std; int event_value; // The "value" of the event; i.e. the integer data on the card enum state {precount, batching, t1_batch, t3_batch, end}; state current_state = precount; int a, b, c, d; // Counters // Now follow the action functions void dis_t1() { cout << "The current event's value is " << event_value << "\n"; } void inc_a() { a++ ;} void inc_b_c() { b++; c++;} void disp() { dis_t1(); cout << a << ' ' << b << ' ' << c << ' ' << d << '\n'; } void inc_b_d() { b++; d++;} void inc_c() { c++ ;} void inc_b() { b++ ;} void error() { cerr << "Received event after reaching end state\n"; } void do_nothing() {} struct table_entry { void (*action)(); // Pointer to the action to do state next_state; } table[5][3] = { dis_t1, batching, inc_a, precount, inc_a, precount, inc_b_c, t1_batch, disp, end, inc_b_d, t3_batch, inc_c, t1_batch, disp, end, inc_b_d, t3_batch, inc_b, t1_batch, disp, end, do_nothing, t3_batch, error, end, error, end, error, end }; // So all the FSM is contained here int main() { int type; while(1) { cin >> type >> event_value; // User enters a card type from 1 to 3 and a value if (type < 1 || type > 3) break; // Enter illegal card type to quit (*table[current_state][type-1].action)(); // Do action current_state = table[current_state][type-1].next_state; // Change state } return 0; }