home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / alb_c10 / chap_06 / ch06_03.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-26  |  1.3 KB  |  65 lines

  1. /*********************************************************************
  2. *  CH06_03.C                                       Fonctions:      *
  3. *               Calcul de C( n, m) avec des nombres rΘels *
  4. *********************************************************************/
  5.  
  6. #include<stdio.h>
  7.  
  8. #define MAX_lf 1.79e308
  9.  
  10. void LitInt( int*);
  11. int Combinaisons( int, int, double*);
  12.  
  13. main( void)
  14. {
  15.     double cnm0;
  16.     int n0, m0, test;
  17.  
  18.     do
  19.          {
  20.         printf(" Entrez  n= ");
  21.         LitInt( &n0);
  22.  
  23.         printf(" Entrez  m= ");
  24.         LitInt( &m0);
  25.  
  26.         test= Combinaisons( n0, m0, &cnm0);
  27.  
  28.         if( test== -1)
  29.                printf(" Erreur dans les paramΦtres!\n");
  30.  
  31.         if( test== 1)
  32.            printf(" Sortie du domaine des doubles!\n");
  33.      }
  34.     while( test);
  35.  
  36.     printf(" C( %d, %d)= %.0lf", n0, m0, cnm0);
  37. }
  38.  
  39. int Combinaisons( int n, int m, double* c)
  40. {
  41.     double cnm = 1;
  42.     int i, f;
  43.  
  44.     if( n< 0 || m< 0 || m> n)  return -1;
  45.  
  46.     if ( m* 2 > n) m= n- m;
  47.           for (i= 1 ; i <= m; n--, i++)
  48.             {
  49.             if ((f= n) % i == 0)
  50.                   f   /= i;
  51.         else  cnm /= i;
  52.         if( MAX_lf/ f< cnm) return 1;
  53.             cnm *= f;
  54.      }
  55.     *c= cnm;
  56.         return 0;
  57. }
  58.  
  59. void LitInt( int* Entier)
  60. {
  61.     while( scanf("%d", Entier) != 1)
  62.         while( getchar() != '\n' );
  63.     while( getchar() != '\n' );
  64. }
  65.