fork download
  1. #include <stdio.h>
  2. void f() {} // до С89
  3.  
  4. void g(void) {} // С89
  5.  
  6. int CalcFactorial(int n) {
  7. if (n <= 0) {
  8. return 1;
  9. } else {
  10. return CalcFactorial(n - 1) * n;
  11. }
  12. }
  13.  
  14. int CalcFibonacci(int n) {
  15. if (n <= 1) {
  16. return 1;
  17. } else {
  18. return CalcFibonacci(n - 1) +
  19. CalcFibonacci(n - 2);
  20. }
  21. }
  22.  
  23. enum sequence_id {
  24. FACTORIAL,
  25. FIBONACCI
  26. };
  27.  
  28. int (*SelectSequence(enum sequence_id id))(int) {
  29. if (id == FACTORIAL) {
  30. return CalcFactorial;
  31. }
  32. if (id == FIBONACCI) {
  33. return CalcFibonacci;
  34. }
  35. return NULL; // сердито 
  36. }
  37.  
  38. int main() {
  39. int n = SelectSequence(FACTORIAL)(5); // n == 120
  40. printf("n = %d\n", n);
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
n = 120