NeroReflex

ricorsione con puntatore a funzioni

Apr 26th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int fact(int n);
  4.  
  5. int base(int n) {
  6.     return n;
  7. }
  8.  
  9. int non_base(int n) {
  10.     return n * fact(n-1);
  11. }
  12.  
  13. int fact(int n) {
  14.     int (*caso)(int) = (n <= 1) ? base : non_base;
  15.    
  16.     return caso(n);
  17. }
  18.  
  19. int main(void) {
  20.     int n = 0;
  21.    
  22.     printf("Inserisci un numero (intero non negativo)");
  23.     scanf("%d", &n);
  24.    
  25.     n = fact(n);
  26.    
  27.     printf("%d", n);
  28.    
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment