Advertisement
Guest User

abfibo

a guest
Nov 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None | 0 0
  1. *                                                                              
  2.   abfact.c                                                                      
  3.   S. Edward Dolan                                                              
  4.   Wednesday, November 20 2019                                                  
  5. */                                                                              
  6.                                                                                
  7. #include <stdlib.h>                                                            
  8. #include <stdio.h>                                                              
  9. #include <string.h>                                                            
  10.                                                                                
  11. char* fibo(int n) {                                                            
  12.     char* out;                                                                  
  13.     if (n == 1) {                                                              
  14.         /*                                                                      
  15.           `out' has to be malloc'd because the caller of fibo() must call      
  16.           free(), marked with ### below. The caller may be main() or the final  
  17.           `else' below.                                                        
  18.         */                                                                      
  19.         out = malloc(sizeof(char) * 2); /* two bytes that `out' points to */    
  20.         strcpy(out, "b");               /* out => ['a', 0] */                  
  21.     }                                                                          
  22.     else if (n == 2) {                                                          
  23.         out = malloc(sizeof(char) * 2);                                        
  24.         strcpy(out, "a");                                                      
  25.     }                                                                          
  26.     else {                                                                      
  27.         /*                                                                      
  28.           This loosely does what Python does for you:                          
  29.           return fibo(n-1) + fibo(n-2)                                          
  30.         */                                                                      
  31.         char* s1 = fibo(n - 1), *s2 = fibo(n - 2); /* caller */                
  32.         out = malloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1));            
  33.         out = strcat(strcpy(out, s1), s2);                                      
  34.         /*                                                                      
  35.           You no longer need s1 and s2 because their characters were copied to  
  36.           out, which the caller does need.                                      
  37.         */                                                                      
  38.         free(s1);               /* ### */                                      
  39.         free(s2);               /* ### */                                      
  40.     }                                                                          
  41.     return out;                                                                
  42. }                                                                              
  43.                                                                                
  44. int main(int argc, char** argv) {                                              
  45.     int i = 1, n = 5;                                                          
  46.     if (argc > 1)                                                              
  47.         n = atoi(argv[1]);                                                      
  48.     while (i < n) {                                                            
  49.         char* v = fibo(i++);    /* caller */                                    
  50.         printf("%ld\n", strlen(v));                                            
  51.         /* puts(v); */                                                          
  52.         free(v);                /* ### */                                      
  53.     }                                                                          
  54.     return 0;                                                                  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement