Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /////////////////////////////////////////////////
  2. //
  3. // VOTRE NOM : El Aaqdi
  4. // VOTRE PRENOM : Aiman
  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; 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. // Affichage sans passage � la ligne d'un tableau t de n bits
  32. void AfficherTab(int n, int t[])
  33. {
  34. int i;
  35. for (i = 0; i < n; i++)
  36. printf("%d", t[i]);
  37. printf("\n");
  38. }
  39.  
  40. //inversion des n elements du tableau t
  41. void InverserTableau(int n, int t[])
  42. {
  43. int i, t_tmp[n];
  44. for (i =0;i<n;i++)
  45. t_tmp[n-1-i] = t[i];
  46.  
  47. i = 0;
  48. for (i = 0; i< n; i++)
  49. t[i] = t_tmp[i];
  50. }
  51.  
  52. //Convertit un entier positif val en binaire pur sur n positions
  53. void Int2Bin(int val, int n, int t[])
  54. {
  55. int j, i = 0, x = 1;
  56.  
  57. while(val > 0) {
  58. t[n - x] = val % 2;
  59. val = val / 2;
  60. i++;
  61. x = i + 1;
  62. }
  63. }
  64.  
  65. // Calcule le complement a 1 de l'entier val
  66. void ComplementA1(int val, int n, int t[])
  67. {
  68. Int2Bin(val, n, t);
  69. int i;
  70. for (i = 0; i < n; i++)
  71. (t[i]== 0) ? (t[i] = 1) : (t[i] = 0);
  72. }
  73.  
  74. // Calcule le complement a 2 de l'entier val
  75. void ComplementA2(int val, int n, int t[])
  76. {
  77. int i, carry = 1;
  78. ComplementA1(val, n, t);
  79.  
  80.  
  81.  
  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 base;
  96. int tab[NBMAX];
  97. char* menu =
  98. " \n\nProgramme de test\n\n0 Quitter\n1 ComplementA1(valeur, n, tab)\n2 ComplementA2(valeur, n, tab)\n3 ValBin(base, n, tab)\n4 InverserTableau(n, tab)\n5 Int2Bin(valeur, n, tab)\n";
  99. char* invite = "--> Quel est votre choix ? ";
  100. printf("%s", menu);
  101. while (continuer == 1) {
  102. printf("%s", "--> Quel est votre choix ? ");
  103. scanf("%d", &choix);
  104. switch (choix) {
  105. case 0:
  106. continuer = 0;
  107. break;
  108. case 1:
  109. printf("Entrez un nombre : ");
  110. scanf("%d", &valeur);
  111. ComplementA1(valeur, n, tab);
  112. AfficherTab(n, tab);
  113. break;
  114. case 2:
  115. printf("Entrez un nombre : ");
  116. scanf("%d", &valeur);
  117. ComplementA2(valeur, n, tab);
  118. AfficherTab(n, tab);
  119. break;
  120. case 3:
  121. printf("Entrez le code du nombre : ");
  122. LireTab(n, tab);
  123. printf("Entrez la base : ");
  124. scanf("%d", &base);
  125. printf("C'est le code de %d\n", ValBin(base, n, tab));
  126. break;
  127. case 4:
  128. LireTab(n, tab);
  129. printf("Tableau inverse\n");
  130. InverserTableau(n, tab);
  131. AfficherTab(n, tab);
  132. break;
  133. case 5:
  134. printf("Entrez un nombre : ");
  135. scanf("%d", &valeur);
  136. Int2Bin(valeur, n, tab);
  137. AfficherTab(n, tab);
  138. default:
  139. printf("Choix inexistant !!!!\n");
  140. printf("%s", menu);
  141. }
  142. }
  143. printf("Au revoir et a bientot\n");
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement