Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /////////////////////////////////////////////////
  2. //
  3. // VOTRE NOM : CAPOZZA
  4. // VOTRE PRENOM : Vito
  5. //
  6. /////////////////////////////////////////////////
  7.  
  8. //Vous pouvez tester les fonctions du main que vous avez écrites en exécutant le programme CodNum.exe
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. // Nombre maximum de bits codant toute donnée
  13. #define NBMAX 32
  14.  
  15. //initialisation les n éléments du tableau t avec des 0
  16. void InitTab(int n, int t[])
  17. {
  18. int i;
  19. for (i = 0; i < n-1; i++)
  20. t[i] = 0;
  21. }
  22.  
  23. // Lit le code en complement à b (1 ou 2) sur n positions d'un nombre
  24. void LireTab(int n, int t[])
  25. {
  26. int i;
  27. for (i = 0; i < n; i++){
  28. scanf("%d", &t[i]);
  29. }
  30. }
  31.  
  32. // Affichage sans passage à la ligne d'un tableau t de n bits
  33. void AfficherTab(int n, int t[])
  34. {
  35. int i;
  36. for (i = 0; i < n; i++)
  37. printf("%d", t[i]);
  38. printf("\n");
  39. }
  40.  
  41. //inversion des n elements du tableau t
  42. void InverserTableau(int n, int t[])
  43. {
  44. int i, reverse;
  45. int j = n-1;
  46. for (i = 0; i != j/2; i++) {
  47. reverse = t[i];
  48. t[i] = t[j];
  49. t[j] = reverse;
  50. j--;
  51. }
  52. }
  53.  
  54. //Convertit un entier positif val en binaire pur sur n positions
  55. void Int2Bin(int val, int n, int t[])
  56. {
  57. int i, reverse, reste;
  58. int j = n-1;
  59.  
  60. for (i = 0; i < n-1; i++){
  61. t[i] = 0;
  62. }
  63.  
  64. for (i=0; val>0, i< n-1; i++){
  65. t[i] = val%2;
  66. val = val/2;
  67. }
  68. InverserTableau(n, t);
  69.  
  70. }
  71.  
  72. // Calcule le complement a 1 de l'entier val
  73. void ComplementA1(int val, int n, int t[])
  74. {
  75. //A completer
  76. }
  77.  
  78. // Calcule le complement a 2 de l'entier val
  79. void ComplementA2(int val, int n, int t[])
  80. {
  81. //A completer
  82. }
  83.  
  84. //Retourne la valeur entiere d'un binaire en complement a b sur n positions
  85. int ValBin(int b, int n, int t[])
  86. {
  87. //A completer
  88. }
  89.  
  90. int main()
  91. {
  92. int choix, valeur;
  93. int continuer = 1;
  94. int n = 16;
  95. int val = 77;
  96. int base;
  97. int tab[NBMAX];
  98. char* menu =
  99. " \n\nProgramme de test\n\n0 Quitter\n1 ComplementA1(valeur, n, tab)\n2 ComplementA2(valeur, n, tab)\n3 ValBin(base, n, tab)\n\n";
  100. char* invite = "--> Quel est votre choix ? ";
  101. Int2Bin(val, 32, tab);
  102. printf("%s", menu);
  103. while (continuer == 1) {
  104. printf("%s", "--> Quel est votre choix ? ");
  105. scanf("%d", &choix);
  106. switch (choix) {
  107. case 0:
  108. continuer = 0;
  109. break;
  110. case 1:
  111. printf("Entrez un nombre : ");
  112. scanf("%d", &valeur);
  113. ComplementA1(valeur, n, tab);
  114. AfficherTab(n, tab);
  115. break;
  116. case 2:
  117. printf("Entrez un nombre : ");
  118. scanf("%d", &valeur);
  119. ComplementA2(valeur, n, tab);
  120. AfficherTab(n, tab);
  121. break;
  122. case 3:
  123. printf("Entrez le code du nombre : ");
  124. LireTab(n, tab);
  125. printf("Entrez la base : ");
  126. scanf("%d", &base);
  127. printf("C'est le code de %d\n", ValBin(base, n, tab));
  128. break;
  129. case 4:
  130. printf("Entrez le code du nombre :");
  131. scanf("%d", &valeur);
  132. Int2Bin(valeur, n, tab);
  133. AfficherTab(n, tab);
  134. break;
  135.  
  136. default:
  137. printf("Choix inexistant !!!!\n");
  138. printf("%s", menu);
  139. }
  140. }
  141. printf("Au revoir et a bientot\n");
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement