Advertisement
samymy

10th assignment

Jun 22nd, 2018
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef enum {
  4.  
  5.     OFF,
  6.     ON,
  7.     ESPRESSO_READY,
  8.     STEAMER_READY,
  9.     MAKE_ESPRESSO,
  10.     MAKE_STEAM
  11.  
  12. }   coffee_machine_state;
  13.  
  14. typedef struct {
  15.    
  16.     coffee_machine_state    current_state;
  17.  
  18. }   coffee_machine;
  19.  
  20. void    switch_on(coffee_machine* FSM) {
  21.     FSM->current_state = ON;
  22. }
  23.  
  24. void    switch_off(coffee_machine* FSM) {
  25.     FSM->current_state = OFF;
  26. }
  27.  
  28. void    heat_up(coffee_machine* FSM) {
  29.     FSM->current_state = ESPRESSO_READY;
  30. }
  31.  
  32. void    steam_on(coffee_machine* FSM) {
  33.     FSM->current_state = STEAMER_READY;
  34. }
  35.  
  36. void    steam_off(coffee_machine* FSM) {
  37.     FSM->current_state = ESPRESSO_READY;
  38. }
  39.  
  40. void    espresso_on(coffee_machine* FSM) {
  41.     FSM->current_state = MAKE_ESPRESSO;
  42. }
  43.  
  44. void    espresso_off(coffee_machine* FSM) {
  45.     FSM->current_state = ESPRESSO_READY;
  46. }
  47.  
  48. void    steam_knob_forward(coffee_machine* FSM) {
  49.     FSM->current_state = MAKE_STEAM;
  50. }
  51.  
  52. void    steam_knob_backward(coffee_machine* FSM) {
  53.     FSM->current_state = STEAMER_READY;
  54. }
  55.  
  56. #define STATE_NUMBER 6
  57.  
  58. int     state_matrix[STATE_NUMBER][STATE_NUMBER] = {
  59.  
  60.     { 0, 1, 0, 0, 0, 0 },
  61.     { 1, 0, 1, 0, 0, 0 },
  62.     { 1, 0, 0, 1, 1, 0 },
  63.     { 1, 0, 1, 0, 0, 1 },
  64.     { 0, 0, 1, 0, 0, 0 },
  65.     { 0, 0, 0, 1, 0, 0 }
  66.  
  67. };
  68.  
  69. typedef void (*action)(coffee_machine*);
  70.  
  71. action  action_matrix[STATE_NUMBER][STATE_NUMBER] = {
  72.  
  73.     { 0, switch_on, 0, 0, 0, 0 },
  74.     { switch_off, 0, heat_up, 0, 0, 0 },
  75.     { switch_off, 0, 0, steam_on, espresso_on, 0 },
  76.     { switch_off, 0, steam_off, 0, 0, steam_knob_forward },
  77.     { 0, 0, espresso_off, 0, 0, 0 },
  78.     { 0, 0, 0, steam_knob_backward, 0, 0 }
  79.    
  80. };
  81.  
  82. int     main(int argc, char **argv)
  83. {
  84.     coffee_machine      FSM;
  85.     FSM.current_state = OFF;
  86.  
  87.     printf("State - %d\n", FSM.current_state);
  88.  
  89.     if (state_matrix[FSM.current_state][ON])
  90.         action_matrix[FSM.current_state][ON](&FSM);
  91.  
  92.     printf("State - %d\n", FSM.current_state);
  93.  
  94.     if (state_matrix[FSM.current_state][ESPRESSO_READY])
  95.         action_matrix[FSM.current_state][ESPRESSO_READY](&FSM);
  96.  
  97.     printf("State - %d\n", FSM.current_state);
  98.  
  99.     /*
  100.         state_matrix can be represent the adjactive matrix
  101.         for nooriented graph and used to get a set of actions
  102.         needed to go from one state to another (using BFS)
  103.     */
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement