fork download
  1. #include <stdio.h>
  2. typedef void (*Handler)(void);
  3.  
  4. // microcontroller commands
  5. void BlueOn(void) { printf ("Включаем синий. "); }
  6. void BlueOff(void) { printf ("Выключаем синий. "); }
  7. void RedOn(void) { printf ("Включаем красный. "); }
  8. void RedOff(void) { printf ("Выключаем красный. "); }
  9.  
  10. // handlers for the signals
  11. void BlueOnRedOn(void) { BlueOn(); RedOn(); }
  12. void BlueOnRedNone(void) { BlueOn(); }
  13. void BlueOnRedOff(void) { BlueOn(); RedOff(); }
  14. void BlueNoneRedOn(void) { RedOn(); }
  15. void BlueNoneRedOff(void) { RedOff(); }
  16. void BlueOffRedOn(void) { BlueOff(); RedOn(); }
  17. void BlueOffRedNone(void) { BlueOff(); }
  18. void BlueOffRedOff(void) { BlueOff(); RedOff(); }
  19.  
  20. // jump table
  21. Handler bothOff[4] = { 0, BlueOnRedNone, BlueNoneRedOn, BlueOnRedOn };
  22. Handler blueOn[4] = { BlueOffRedNone, 0, BlueOffRedOn, BlueNoneRedOn };
  23. Handler redOn[4] = { BlueNoneRedOff, BlueOnRedOff, 0, BlueOnRedNone };
  24. Handler bothOn[4] = { BlueOffRedOff, BlueNoneRedOff, BlueOffRedNone, 0 };
  25. Handler* handlers[4] = { bothOff, blueOn, redOn, bothOn };
  26.  
  27. int main(void) {
  28. Handler* current = bothOff;
  29. for(int n;;) {
  30. scanf("%d", &n); // signal from microcontroller
  31. if(n < 0 || n > 3) break;
  32. if(current[n]) {
  33. current[n]();
  34. current = handlers[n];
  35. }
  36. printf("Готово!\n");
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 4220KB
stdin
0
1
2
3
0
3
2
1
5
stdout
Готово!
Включаем синий. Готово!
Выключаем синий. Включаем красный. Готово!
Включаем синий. Готово!
Выключаем синий. Выключаем красный. Готово!
Включаем синий. Включаем красный. Готово!
Выключаем синий. Готово!
Включаем синий. Выключаем красный. Готово!