fork download
  1. #include <stdio.h>
  2.  
  3. int main(void);
  4. int button_clicked(int position);
  5. int cycle_completed(int position);
  6.  
  7.  
  8. int main(void)
  9. {
  10. int position = 0;
  11.  
  12. /*
  13. positions:
  14. 0 = closed
  15. 1 = opening
  16. 2 = stopped_while_opening
  17. 3 = open
  18. 4 = closing
  19. 5 = stopped_while_closing
  20. */
  21.  
  22. position = button_clicked(position);
  23. position = cycle_completed(position);
  24. position = button_clicked(position);
  25. position = button_clicked(position);
  26. position = button_clicked(position);
  27. position = button_clicked(position);
  28. position = button_clicked(position);
  29. position = cycle_completed(position);
  30.  
  31. return 0;
  32. }
  33.  
  34. int button_clicked(int position)
  35. {
  36. if (!position)
  37. {
  38. printf(">Button clicked.\nDOOR: OPENING\n");
  39. return 1;
  40. }
  41. if (position == 1)
  42. {
  43. printf(">Button clicked.\nDOOR: STOPPED_WHILE_OPENING\n");
  44. return 2;
  45. }
  46. if (position == 2)
  47. {
  48. printf(">Button clicked.\nDOOR: CLOSING\n");
  49. return 4;
  50. }
  51. if (position == 3)
  52. {
  53. printf(">Button clicked.\nDOOR: CLOSING\n");
  54. return 4;
  55. }
  56. if (position == 4)
  57. {
  58. printf(">Button clicked.\nDOOR: STOPPED_WHILE_CLOSING\n");
  59. return 5;
  60. }
  61. if (position == 5)
  62. {
  63. printf(">Button clicked.\nDOOR: OPENING\n");
  64. return 1;
  65. }
  66. }
  67.  
  68. int cycle_completed(int position)
  69. {
  70. if (position == 1)
  71. {
  72. printf(">Cycle complete. DOOR: OPEN\n");
  73. return 3;
  74. }
  75. if (position == 4)
  76. {
  77. printf(">Cycle complete. DOOR: CLOSED\n");
  78. return 0;
  79. }
  80. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
>Button clicked.
DOOR: OPENING
>Cycle complete. DOOR: OPEN
>Button clicked.
DOOR: CLOSING
>Button clicked.
DOOR: STOPPED_WHILE_CLOSING
>Button clicked.
DOOR: OPENING
>Button clicked.
DOOR: STOPPED_WHILE_OPENING
>Button clicked.
DOOR: CLOSING
>Cycle complete. DOOR: CLOSED