home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / alb_c10 / chap_13 / ch13_05.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-08  |  1.2 KB  |  62 lines

  1. /*********************************************************************
  2. *  CH13_05.C                   Conversion d'un entier signΘ en binaire *
  3. *           Exercice d'aprΦs un programme de William J-M MARIE *
  4. *********************************************************************/
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8.  
  9. #define n (8* sizeof( int))
  10.  
  11. void LitInt( int*);
  12. void convbin_n( int);
  13.  
  14. main(void)
  15. {
  16.       int entier;
  17.  
  18.     printf(" **** Conversion:  int => binaire ****\t\t"
  19.            "( 0 pour sortir)");
  20.  
  21.       while(1)
  22.       {
  23.          printf("\n\n Entrez un entier, de -32768 α 32767 :  ");
  24.        LitInt( &entier);
  25.  
  26.        if( entier== 0) break;
  27.        
  28.            convbin_n( entier);
  29.     }
  30. }
  31.  
  32. void convbin_n( int nombre)
  33. {
  34.       int i;
  35.     unsigned int valeur= 0x1 << ( n- 1);  /* 1000 0000 0000 0000  */
  36.  
  37.     printf(" DΘcimal: %d =>  Binaire: ", nombre);
  38.  
  39.     for( i= 1; i<= n; i++)
  40.         {
  41.         printf("%d", ( nombre & valeur) ? 1 : 0);
  42.  
  43.         if( i % 4== 0)  printf(" ");
  44.  
  45.         valeur>>= 1;
  46.         }
  47. }
  48.  
  49. void LitInt( int *entier)
  50. {
  51.     double dble;
  52.     do
  53.         {
  54.        while( scanf("%lf", &dble)!= 1)
  55.         while( getchar() != '\n');
  56.        while( getchar() != '\n');
  57.     }
  58.     while( dble< -32768.0 || dble> 32767.0);
  59.     *entier= ( int) dble;
  60. }
  61.  
  62.