Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Fsm
- {
- public:
- Fsm() : state(&Fsm::initState) {}
- void run(int i)
- {
- (this->*state)(i);
- }
- private:
- void initState(int i)
- {
- if(i>=0) state=&Fsm::statePositive;
- else state=&Fsm::stateNegative;
- }
- void statePositive(int i)
- {
- cout<<"Last number was positive"<<endl;
- if(i>=0) state=&Fsm::statePositive;
- else state=&Fsm::stateNegative;
- }
- void stateNegative(int i)
- {
- cout<<"Last number was negative"<<endl;
- if(i>=0) state=&Fsm::statePositive;
- else state=&Fsm::stateNegative;
- }
- void (Fsm::* state)(int);
- };
- int main()
- {
- Fsm fsm1;
- Fsm fsm2;
- fsm1.run(10);
- fsm2.run(-2);
- fsm1.run(0);
- fsm2.run(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement