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