Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. typedef int Item;
  6.  
  7. /**
  8. * @brief Affichage d’un entier
  9. * @param[in] l'argument
  10.  
  11. */
  12. void afficher(const Item& arg) {
  13. cout << arg;
  14. }
  15.  
  16. /* Conteneur (de capacité 15) généralisé à tout type d'item */
  17. struct ConteneurTS {
  18. enum {CAPACITE = 15}; // Capacité du conteneur
  19. Item tab[CAPACITE]; // Conteneur primaire (tableau statique)
  20. unsigned int nbItems; // Nombre d'items stockés
  21. };
  22.  
  23. /**
  24. * @brief Initialise à vide un conteneur
  25. * @param[out] le conteneur
  26. */
  27. void initialiser(ConteneurTS& c) {
  28. c.nbItems=0; // aucun élément n’est stocké
  29. }
  30.  
  31. /**
  32. * @brief Lecture d'un item dans un conteneur
  33. * @param[in] c : le conteneur
  34. * @param[in] i : l'indice de l'item dans le conteneur
  35. * @return l'item (à l'index i)
  36. * @pre i < c.nbItems
  37. */
  38. Item lire(const ConteneurTS& c, unsigned int i) {
  39. assert (i < c.nbItems);
  40. return c.tab[i];
  41. }
  42.  
  43. /**
  44. * @brief Ecrire un item dans un conteneur
  45. * @param[in,out] le conteneur
  46. * @param[in] l'indice où ecrire l'item
  47. * @param[in] l'item à ecrire
  48. * @pre i <= c.nbItems et c.nbItems < c.capacite
  49. */
  50. void ecrire(ConteneurTS& c, unsigned int i, const Item& item) {
  51. assert ((i < c.nbItems) || ((i==c.nbItems) && (c.nbItems < ConteneurTS::CAPACITE)));
  52. c.tab[i] = item;
  53. if (i == c.nbItems)
  54. c.nbItems++;
  55. }
  56.  
  57. /**
  58. * @brief Affichage des elements du conteneur
  59. * @param[in] le conteneur
  60. */
  61. void afficher(const ConteneurTS& c) {
  62. cout <<"[ ";
  63. for (unsigned int i=0; i < c.nbItems; ++i) {
  64. afficher(c.tab[i]);
  65. cout << " ";
  66. }
  67. cout << "]" << endl;
  68. }
  69.  
  70. int comparer(const Item arg1,const Item arg2) {
  71. if (arg1 < arg2) {
  72. return -1;
  73. }
  74. else if (arg1 == arg2) {
  75. return 0;
  76. }
  77. else {
  78. return 1;
  79. }
  80. }
  81. void extrema(ConteneurTS c) {
  82. int j = 1, min = 0, max = 0, a = 0;
  83. while (j != c.nbItems) {
  84. a = comparer(c.tab[min], c.tab[j]);
  85. if (a == 1) {
  86. min = j;
  87. }
  88. j++;
  89. }
  90. while (j != c.nbItems) {
  91. a = comparer(c.tab[max], c.tab[j]);
  92. if (a == 1) {
  93. max = j;
  94. }
  95. j++;
  96. }
  97. cout << c.tab[min] << '-'<< c.tab[max] << endl;
  98. }
  99.  
  100. int main () {
  101. ConteneurTS c;
  102. initialiser(c);
  103. for (int i=0; i<5; ++i) {
  104. ecrire(c, i, i+1);
  105. }
  106. afficher(c);
  107. extrema(c);
  108. assert(lire(c, 0)==1);
  109. assert(lire(c, 4)==5);
  110. ecrire (c, 0, 10);
  111. assert(lire(c, 0)==10);
  112. ecrire(c, 5, 11);
  113. assert(lire(c, 5)==11);
  114.  
  115. system("pause");
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement