Advertisement
Lactuka

exercice1-commenté

Oct 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. /*
  2. **Filename : exercise_1
  3. **
  4. **Made by : LIONTI Theo / DESJARDINS Alexis
  5. **
  6. **Description : Affichage de nombres régulier dans un intervalle [a;b] saisi par l'utilisateur.
  7. **
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. int main()
  15. {
  16.     int a, b, temp, digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8, i, regulars, y, sum, x, restart, c, w;
  17.  
  18.    do{
  19.  
  20.      x=w=0;
  21.     a = b = 129;           //allows the loop to start
  22.  
  23.  
  24.     printf("Entrez un interval d'entiers [a,b] avec a > -128 et b < 128\n");
  25.  
  26.     while(a < -127 || a > 127){
  27.         printf("\na = ");               //forces user to input values between -127 and 127
  28.         scanf("%d", &a);
  29.     }
  30.  
  31.     while(b < -127 || b > 127 || a > b){
  32.         printf("\nb = ");
  33.         scanf("%d", &b);
  34.     }
  35.  
  36.     printf("Les nombres reguliers entre %d et %d sont :", a, b);
  37.     regulars = 0;    
  38.  
  39.     for(y=0;y<=1;y++){
  40.  
  41.         for(i=a;i<=b;i++){    //converts i to binary. Every digit is stocked in a separate variable
  42.             temp = abs(i);
  43.  
  44.  
  45.             digit1 = temp % 2;
  46.             temp = temp / 2;
  47.  
  48.             digit2 = temp % 2;
  49.             temp = temp / 2;
  50.  
  51.             digit3 = temp % 2;
  52.             temp = temp / 2;
  53.  
  54.             digit4 = temp % 2;
  55.             temp = temp / 2;
  56.  
  57.             digit5 = temp % 2;
  58.             temp = temp / 2;
  59.  
  60.             digit6 = temp % 2;
  61.             temp = temp / 2;
  62.  
  63.             digit7 = temp % 2;
  64.             temp = temp /2;
  65.  
  66.             digit8 = 0;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.                 if(i<0){          // if working on negative values converts the absolute value to negative value by replacing each digit
  73.                     digit8 = 1;
  74.  
  75.                     if (digit7)
  76.                         digit7 = 0;
  77.  
  78.                     else
  79.                         digit7 = 1;
  80.  
  81.                     if(digit6)
  82.                         digit6 = 0;
  83.  
  84.                     else
  85.                         digit6 = 1;
  86.  
  87.                     if(digit5)
  88.                         digit5 =0;
  89.  
  90.                     else
  91.                         digit5 = 1;
  92.  
  93.                     if (digit4)
  94.                         digit4 = 0;
  95.  
  96.                     else
  97.                         digit4 = 1;
  98.  
  99.                     if(digit3)
  100.                         digit3 = 0;
  101.  
  102.                     else
  103.                         digit3 = 1;
  104.  
  105.  
  106.                     if(digit2)
  107.                         digit2 =0;
  108.  
  109.                     else
  110.                         digit2 = 1;
  111.  
  112.  
  113.                     if(digit1)
  114.                         digit1 = 0;
  115.  
  116.                     else
  117.                         digit1 = 1;
  118.  
  119.                                                    
  120.  
  121.  
  122.                     if(!digit1){               //then adds 1
  123.                         digit1++;
  124.                                 }
  125.  
  126.                     else {
  127.                         digit1 = 0;
  128.  
  129.                         if(!digit2){
  130.                             digit2++;
  131.                                     }
  132.                         else {
  133.                             digit2 = 0;
  134.  
  135.  
  136.                             if(!digit3){
  137.                                 digit3++;
  138.                                         }
  139.                             else {
  140.                                 digit3 = 0;
  141.                                 if(!digit4){
  142.                                     digit4++;
  143.                                             }
  144.                                 else{
  145.                                     digit4 = 0;
  146.                                     if(!digit5){
  147.                                         digit5++;
  148.                                                 }
  149.                                     else{
  150.                                         digit5 = 0;
  151.                                         if(!digit6){
  152.                                         digit6++;
  153.                                                     }
  154.                                         else{
  155.                                             digit6 = 0;
  156.                                             if(!digit7){
  157.                                                 digit7 = 1;
  158.                                                         }
  159.                                             else{
  160.                                                 digit7 = 0;
  161.  
  162.                                                 }
  163.                                             }
  164.                                         }
  165.                                     }
  166.                                 }
  167.                             }
  168.                         }
  169.  
  170.                 }
  171.  
  172.  
  173.  
  174. sum = digit1+digit2+digit3+digit4+digit5+digit6+digit7+digit8; //if the number is regular, the sum of its digits is 4
  175.  
  176.     if(sum == 4){
  177.             if(y == 0){
  178.  
  179.                 regulars++;
  180.             }
  181.  
  182.  
  183.  
  184.  
  185.       if(y==1){
  186. x++;
  187.  
  188.                  if(x == regulars-1){
  189.                 printf(" %d et", i)
  190.  
  191.                 }
  192.                 else if(x==regulars){
  193.                     printf(" %d.\n", i);
  194.                     w=1;
  195.                 }
  196.                 else
  197.                     printf(" %d,", i);
  198.  
  199.       }
  200.     }
  201.  
  202.         }
  203.  
  204.  
  205. }
  206. if(regulars==0){
  207.                 printf(" ERREUR - il n'y a pas d'entiers reguliers entre %d et %d", a, b); //ouptuts
  208.                // return 0;
  209.            }
  210.  
  211.  
  212.  
  213. if(w==1){
  214.  
  215.  
  216.                 do{
  217.                     while(c != '\n' && c != EOF)    //emptying buffer
  218.     {
  219.         c = getchar();
  220.     }
  221.                         printf("\n\nRelancer ? [Y/N] : ");
  222.                         temp = 0;         //alloxs the user to start again with != values
  223.            scanf("%c", &temp);
  224.            }while(temp != 'y' && temp != 'Y' && temp !='n' && temp != 'N');
  225.  
  226.            if(temp=='y'||temp=='Y')
  227.            restart = 1;
  228.  
  229.            if(temp =='n' || temp =='N')
  230.             restart = 0;
  231.  
  232.         }
  233.     }while(restart);
  234.  
  235.  
  236.    return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement