pabloliva87

Ej5C++

Mar 20th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. #include "gtest/gtest.h"
  5. #include "gmock/gmock.h"
  6.  
  7. #define STATE_AMOUNT 5
  8.  
  9. using namespace std;
  10.  
  11. enum State
  12. {
  13.     Off,
  14.     Stopped,
  15.     Walking,
  16.     Running,
  17.     Error
  18. };
  19.  
  20. /** The interface of the different states */
  21.  
  22. class state_interface                   /* Abstract Class */
  23. {
  24.     public:
  25.     virtual State turnOn () = 0;
  26.     virtual State turnOff () = 0;
  27.     virtual State speedUp () = 0;
  28.     virtual State speedDown () = 0;
  29. };
  30.  
  31. /** The different states */
  32. class off_State: public state_interface
  33. {
  34.     public:
  35.     State turnOn () {
  36.         cout << "Off --[turnOn]--> Stopped \n";
  37.         return Stopped;
  38.     };
  39.     State turnOff () {
  40.         cout << "Off --[turnOff]--> Off \n";
  41.         return Off;
  42.     };
  43.     State speedUp () {
  44.         cout << "Off --[speedUp]--> Error \n";
  45.         return Error;
  46.     };
  47.     State speedDown () {
  48.         cout << "Off --[speedDown]--> Error \n";
  49.         return Error;
  50.     };
  51. };
  52.  
  53. class stopped_State: public state_interface
  54. {
  55.     public:
  56.     State turnOn () {
  57.         cout << "Stopped --[turnOn]--> Error \n";
  58.         return Stopped;
  59.     };
  60.     State turnOff () {
  61.         cout << "Stopped --[turnOff]--> Off \n";
  62.         return Off;
  63.     };
  64.     State speedUp () {
  65.         cout << "Stopped --[speedUp]--> Walking \n";
  66.         return Walking;
  67.     };
  68.     State speedDown () {
  69.         cout << "Stopped --[speedDown]--> Error \n";
  70.         return Error;
  71.     };
  72. };
  73.  
  74. class walking_State: public state_interface
  75. {
  76.     public:
  77.     State turnOn () {
  78.         cout << "Walking --[turnOn]--> Walking \n";
  79.         return Walking;
  80.     };
  81.     State turnOff () {
  82.         cout << "Walking --[turnOff]--> Off \n";
  83.         return Off;
  84.     };
  85.     State speedUp () {
  86.         cout << "Walking --[speedUp]--> Running \n";
  87.         return Running;
  88.     };
  89.     State speedDown () {
  90.         cout << "Walking --[speedDown]--> Stopped \n";
  91.         return Stopped;
  92.     };
  93. };
  94.  
  95. class running_State: public state_interface
  96. {
  97.     public:
  98.     State turnOn () {
  99.         cout << "Running --[turnOn]--> Running \n";
  100.         return Running;
  101.     };
  102.     State turnOff () {
  103.         cout << "Running --[turnOff]--> Off \n";
  104.         return Off;
  105.     };
  106.     State speedUp () {
  107.         cout << "Running --[speedUp]--> Error \n";
  108.         return Error;
  109.     };
  110.     State speedDown () {
  111.         cout << "Running --[speedDown]--> Walking \n";
  112.         return Walking;
  113.     };
  114. };
  115.  
  116. class error_State: public state_interface
  117. {
  118.     public:
  119.     State turnOn () {
  120.         cout << "Error --[turnOn]--> Error \n";
  121.         return Error;
  122.     };
  123.     State turnOff () {
  124.         cout << "Error --[turnOff]--> Off \n";
  125.         return Off;
  126.     };
  127.     State speedUp () {
  128.         cout << "Error --[speedUp]--> Error \n";
  129.         return Error;
  130.     };
  131.     State speedDown () {
  132.         cout << "Error --[speedDown]--> Error \n";
  133.         return Error;
  134.     };
  135. };
  136.  
  137.  
  138. class FSM                       /* Singleton */
  139. {
  140.     private:
  141.     state_interface * current;
  142.     state_interface * states [STATE_AMOUNT];
  143.     public:
  144.     FSM()
  145.     {
  146.         states[Off] = new (off_State);
  147.         states[Stopped] = new (stopped_State);
  148.         states[Walking] = new (walking_State);
  149.         states[Running] = new (running_State);
  150.         states[Error] = new (error_State);
  151.         current = states[Off];
  152.     };
  153.    
  154.     void cleanUp ()
  155.     {
  156.         int i;
  157.  
  158.         for (i=0; i<STATE_AMOUNT; i++)
  159.         {
  160.             delete (states[i]);
  161.             states[i] = 0;
  162.         }
  163.         current = 0;
  164.     };
  165.    
  166.     void live ()
  167.     {
  168.         int input = 0;
  169.    
  170.         while (input != 4)
  171.         {
  172.             cout << "Input stimulus: 0_On, 1_Off, 2_SpUp, 3_SpDn, 4_Exit \n";
  173.             cin >> input;
  174.  
  175.             switch(input)
  176.             {
  177.                 State new_state;
  178.                 case(0):
  179.                     new_state = current -> turnOn();
  180.                     current = states[new_state];
  181.                     break;
  182.                 case(1):
  183.                     new_state = current -> turnOff();
  184.                     current = states[new_state];
  185.                     break;
  186.                 case(2):
  187.                     new_state = current -> speedUp();
  188.                     current = states[new_state];
  189.                     break;
  190.                 case(3):
  191.                     new_state = current -> speedDown();
  192.                     current = states[new_state];
  193.                     break;
  194.                 case(4):
  195.                     cleanUp ();
  196.                     break;
  197.                 default:
  198.                     break;
  199.             }
  200.            
  201.         }
  202.     };
  203. };
  204.  
  205. int main(int argc, char** argv)
  206. {
  207.     FSM the_machine;
  208.     the_machine.live();
  209.     return EXIT_SUCCESS;
  210. }
Add Comment
Please, Sign In to add comment