Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
879 connectés 

  FORUM HardWare.fr
  Programmation
  C

  Probléme de tri et affichage d'élément dans une structure

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probléme de tri et affichage d'élément dans une structure

n°1323303
MAD_DIM
Posté le 11-03-2006 à 13:31:07  profilanswer
 

Bonjour,
 
J'ai fait un programme qui permet d'afficher, de rechercher, d'ajouter et supprimer des membres d'un tableau contenu dans une structure.
Lorsque j'ajoute un membre tout va bien il m'affiche a l'écran les membres trier par ordre alphabétique mais si je retourne dans Ajout pour en mettre un second la le programme plante lors de l'affichage.
Je n'arrive pas a mettre la main sur le bug. Si quelqu'un sait m'aider ??
Merci
 

Code :
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. struct DATE { int jour;
  5.               int mois;
  6.     int annee;
  7. };
  8. struct PERS { int ref;
  9.               char nom[20];
  10.     char prenom[20];
  11.     struct DATE nais;
  12. };
  13. struct INDEX { char nom[20];
  14.                struct PERS *adresse;
  15. };
  16. void Init(struct PERS *, struct INDEX *, int, int *);
  17. void Liste(struct INDEX *, int *, int);
  18. void Recherche(struct INDEX *, int *, int);
  19. void Ajout(struct INDEX *, int *, int);
  20. void Suppression(struct INDEX *, int *, int);
  21. void HeapSort(struct INDEX *, int, int *);
  22. void Paterner(struct INDEX *, int, int, int *);
  23. void main()
  24. {
  25. int nbel = 5;
  26. int *pt;
  27. int choix;
  28. bool fin;
  29.     struct PERS *per;
  30. struct INDEX *ind;
  31. int occup[10] = {1, 0, 1, 1, 0, 0, 0, 0, 0};
  32.     struct DATE nais[] = {{29, 3, 1980}, {20, 8, 1975}, {01, 2, 1978},
  33. {10, 5, 1979}, {25, 10, 1981}};
  34.    
  35. struct PERS perso[] = {{127, "Dupont", "Jean", {29, 3, 1980}},
  36. {205, "Durand", "Jean", {20, 8, 1975}}, {135, "Dupond", "Marc", {01, 2, 1978}},
  37. {128, "Pire", "Julien", {10, 5, 1979}}, {112, "Meurice", "Xavier", {25, 10, 1981}}};
  38. struct INDEX index[10];
  39. per = &perso[0];
  40. ind = &index[0];
  41. pt = &occup[0];
  42. Init(per, ind, nbel, pt);
  43. do
  44. {
  45.  fin = false;
  46.  printf("\n                     MENU\n" );
  47.  printf("                     ----\n\n" );
  48.  printf("     1.Liste\n" );
  49.  printf("     2.Recherche\n" );
  50.  printf("     3.Ajout\n" );
  51.  printf("     4.Suppression\n" );
  52.  printf("\nChoix : " );
  53.  fflush(stdin);
  54.  scanf("%d", &choix);
  55.  switch(choix)
  56.  {
  57.  case 1:
  58.   system("cls" );
  59.   Liste(ind, pt, nbel);
  60.   break;
  61.  case 2:
  62.   system("cls" );
  63.   Recherche(ind, pt, nbel);
  64.   break;
  65.  case 3:
  66.   system("cls" );
  67.   Ajout(ind, pt, nbel);
  68.   break;
  69.  case 4:
  70.   system("cls" );
  71.   Suppression(ind, pt, nbel);
  72.   break;
  73.  case 5:
  74.   fin = true;
  75.   break;
  76.  }
  77. }while(fin != true);
  78. }
  79. void Init(struct PERS *per, struct INDEX *ind, int nbel, int *pt)
  80. {
  81. int i;
  82. struct INDEX *indeb;
  83. indeb = ind;
  84. for(i=0;i<nbel;i++)
  85. {
  86.  strcpy(ind->nom, per->nom);
  87.  ind->adresse = per;
  88.  ind ++;
  89.  per++;
  90. }
  91. HeapSort(indeb, nbel, pt);
  92. }
  93. void Liste(struct INDEX *ind, int *pt, int nbel)
  94. {
  95. int i;
  96. printf("\n                           LISTE DES MEMBRES\n" );
  97. printf("                           -----------------\n\n" );
  98. for(i=0;i<nbel;i++)
  99. {
  100.  if(*(pt+i) == 1)
  101.  {
  102.   printf("%d %s %s %d/%d/%d", ind->adresse->ref, ind->nom, ind->adresse->prenom, ind->adresse->nais.jour, ind->adresse->nais.mois, ind->adresse->nais.annee);
  103.   printf("\n" );
  104.  }
  105.  ind++;
  106. }
  107. }
  108. void Recherche(struct INDEX *ind, int *pt, int nbel)
  109. {
  110. int i, nbcar;
  111. char nom[20];
  112. bool trouve;
  113. printf("\n                       RECHERCHE D UN MEMBRE\n" );
  114. printf("                       ---------------------\n\n" );
  115. printf("Entrez le nom du membre que vous recherchez : " );
  116. fflush(stdin);
  117. gets(nom);
  118. nbcar = strlen(nom);
  119. nom[0] = toupper(nom[0]);
  120. for(i=1;i<nbcar;i++)
  121. {
  122.  nom[i] = tolower(nom[i]);
  123. }
  124. i=0;
  125. while(i<nbel && trouve != true)
  126. {
  127.  strcpy(ind->nom, ind->nom);
  128.  if(strcmp(nom, ind->nom) == NULL && *(pt+i) == 1)
  129.  {
  130.   printf("%d %s %s %d/%d/%d", ind->adresse->ref, ind->nom, ind->adresse->prenom, ind->adresse->nais.jour, ind->adresse->nais.mois, ind->adresse->nais.annee);
  131.   trouve = true;
  132.  }
  133.  printf("\n" );
  134.  ind++;
  135.  i++;
  136. }
  137. }
  138. void Ajout(struct INDEX *ind, int *pt, int nbel)
  139. {
  140. int i, j, testint, nbcar;
  141. struct INDEX *ptdeb;
  142. printf("\n                           AJOUT D UN MEMBRE\n" );
  143. printf("                           -----------------\n\n" );
  144. ptdeb = ind;
  145. i=0;
  146. while(i<nbel && *(pt+i) == 1)
  147. {
  148.  i++;
  149.  ind++;
  150. }
  151. printf("Entrez la reference : " );
  152. fflush(stdin);
  153. scanf("%d", &ind->adresse->ref);
  154. do
  155. {
  156.  printf("Entrez le nom : " );
  157.  fflush(stdin);
  158.  gets(ind->adresse->nom);
  159.  nbcar = strlen(ind->adresse->nom);
  160.  if(nbcar == 0)
  161.  {
  162.   printf("VOUS DEVEZ ENTRER UN NOM !!!\n" );
  163.  }
  164. }while(nbcar == 0);
  165. ind->adresse->nom[0] = toupper(ind->adresse->nom[0]);
  166. for(j=1;j<nbcar;j++)
  167. {
  168.  ind->adresse->nom[j] = tolower(ind->adresse->nom[j]);
  169. }
  170. strcpy(ind->nom, ind->adresse->nom);
  171. do
  172. {
  173.  printf("Entrez le prenom : " );
  174.  fflush(stdin);
  175.  gets(ind->adresse->prenom);
  176.  nbcar = strlen(ind->adresse->prenom);
  177.  if(nbcar == 0)
  178.  {
  179.   printf("VOUS DEVEZ ENTRER UN PRENOM !!!\n" );
  180.  }
  181. }while(nbcar == 0);
  182. ind->adresse->prenom[0] = toupper(ind->adresse->prenom[0]);
  183. for(j=1;j<nbcar;j++)
  184. {
  185.  ind->adresse->prenom[j] = tolower(ind->adresse->prenom[j]);
  186. }
  187. do
  188. {
  189.  printf("Entrez le jour : " );
  190.  fflush(stdin);
  191.  testint = scanf("%d", &ind->adresse->nais.jour);
  192.  if(testint == 0 || ind->adresse->nais.jour < 1 || ind->adresse->nais.jour > 31)
  193.  {
  194.   printf("JOUR INVALIDE !!!\n" );
  195.  }
  196. }while(testint == 0 || ind->adresse->nais.jour < 1 || ind->adresse->nais.jour > 31);
  197. do
  198. {
  199.  printf("Entrez le mois : " );
  200.  fflush(stdin);
  201.  testint = scanf("%d", &ind->adresse->nais.mois);
  202.  if(testint == 0 || ind->adresse->nais.mois < 1 || ind->adresse->nais.mois > 12)
  203.  {
  204.   printf("MOIS INVALIDE !!!\n" );
  205.  }
  206. }while(testint == 0 || ind->adresse->nais.mois < 1 || ind->adresse->nais.mois > 12);
  207. do
  208. {
  209.  printf("Entrez le annee : " );
  210.  fflush(stdin);
  211.  testint = scanf("%d", &ind->adresse->nais.annee);
  212.  if(testint == 0 || ind->adresse->nais.annee > 2005)
  213.  {
  214.   printf("ANNEE INVALIDE !!!\n" );
  215.  }
  216. }while(testint == 0 || ind->adresse->nais.annee > 2005);
  217. *(pt+i) = 1;
  218. system("cls" );
  219. ind = ptdeb;
  220. HeapSort(ind, nbel, pt);
  221. ind = ptdeb;
  222. Liste(ind, pt, nbel);
  223. }
  224. void Suppression(struct INDEX *ind, int *pt, int nbel)
  225. {
  226. int i, nbcar;
  227. char nom[20];
  228. bool efface;
  229. struct INDEX *ptdeb;
  230. printf("\n                           SUPPRESSION D UN MEMBRE\n" );
  231. printf("                           -----------------------\n\n" );
  232. ptdeb = ind;
  233. do
  234. {
  235.  printf("Entrez le nom du membre a effacer : " );
  236.  fflush(stdin);
  237.  gets(nom);
  238.  nbcar = strlen(nom);
  239.  if(nbcar == 0)
  240.  {
  241.   printf("VOUS DEVEZ ENTRER UN NOM !!!\n" );
  242.  }
  243. }while(nbcar == 0);
  244. nom[0] = toupper(nom[0]);
  245. for(i=1;i<nbcar;i++)
  246. {
  247.  nom[i] = tolower(nom[i]);
  248. }
  249. i=0;
  250. while(i<nbel && efface != true)
  251. {
  252.  strcpy(ind->nom, ind->nom);
  253.  if(strcmp(nom, ind->nom) == NULL)
  254.  {
  255.   *(pt+i) = 0;
  256.   efface = true;
  257.  }
  258.  ind++;
  259.  i++;
  260. }
  261. system("cls" );
  262. ind = ptdeb;
  263. Liste(ind, pt, nbel);
  264. }
  265. void HeapSort(struct INDEX *ind, int nbel, int *pt)
  266. {
  267. int i, tmp3;
  268. char tmp[20];
  269. struct PERS *tmp1;
  270. /* Paterner 1X tous le vecteur */
  271. for (i = (nbel / 2)-1; i >= 0; i--)
  272. {
  273.   Paterner(ind, i, nbel, pt);
  274. }
  275. for (i = nbel-1; i >= 1; i--)
  276. {
  277.     /* Echange le premier et le dernier élément */
  278.  strcpy(tmp, ind->nom);
  279.  strcpy(ind->nom, (ind+i)->nom);
  280.  strcpy((ind+i)->nom, tmp);
  281.  tmp1 = ind->adresse;
  282.  ind->adresse = (ind+i)->adresse;
  283.  (ind+i)->adresse = tmp1;
  284.  tmp3 = *pt;
  285.  *pt = *(pt+i);
  286.  *(pt+i) = tmp3;
  287.  /* Paterne une 2X le vecteur avec nombre d'éléments - 1 */
  288.  Paterner(ind, 0, i-1, pt);
  289. }
  290. }
  291. void Paterner(struct INDEX *ind, int pere, int finvec, int *pt)
  292. {
  293.   int trie, fils, tmp3;
  294.   char tmp[20];
  295.   struct PERS *tmp1;
  296.   trie = 0;
  297.   /**************************************************************************/
  298.   /* Boucle tant que indice du pere*2 est plus petit que le dernier élément */
  299.   /**************************************************************************/
  300.   while ((pere*2 <= finvec) && (trie != 1))
  301.   {
  302.   if (pere*2 == finvec)
  303.   {
  304.   fils = pere*2;
  305.   }
  306.   else
  307.   {
  308.   if (strcmp((ind+pere*2)->nom, (ind+(pere*2 + 1))->nom) > 0)
  309.   {
  310.   fils = pere*2;
  311.   }
  312.   else
  313.   {
  314.   fils = pere*2 + 1;
  315.   }
  316.   }
  317.   if (strcmp((ind+pere)->nom, (ind+fils)->nom) < 0)
  318.   {
  319.  strcpy(tmp, (ind+pere)->nom);
  320.  strcpy((ind+pere)->nom, (ind+fils)->nom);
  321.  strcpy((ind+fils)->nom, tmp);
  322.  tmp1 = (ind+pere)->adresse;
  323.  (ind+pere)->adresse = (ind+fils)->adresse;
  324.  (ind+fils)->adresse = tmp1;
  325.  tmp3 = *(pt+pere);
  326.  *(pt+pere) = *(pt+fils);
  327.  *(pt+fils) = tmp3;
  328.  pere = fils;
  329.   }
  330.   else
  331.   {
  332.   trie = 1;
  333.   }
  334.   }
  335. }


Message édité par MAD_DIM le 11-03-2006 à 13:32:18
mood
Publicité
Posté le 11-03-2006 à 13:31:07  profilanswer
 

n°1323349
nargy
Posté le 11-03-2006 à 16:02:52  profilanswer
 

Premier problème:
 
        while(i<nbel && *(pt+i) == 1)
        {
            i++;
            ind++;
        }
 
Là tu devrai afficher un message d erreure et ne rien ajouter lorsque i>=nbel .

n°1323379
MAD_DIM
Posté le 11-03-2006 à 17:16:45  profilanswer
 

Merci c'est vrai je n'y avait pas penser.
Mais le problème d'ajout est toujours la ??

n°1323536
nargy
Posté le 12-03-2006 à 00:47:15  profilanswer
 

Le code est trop long pour que je m y penche en détail.
 
Tu devrai utiliser un deboggeur (ddd est génial), pour voir à quelle ligne exactement le programme plante.
Ensuite, ce sera surement dû à une valeure de pointeur nulle ou égarée, il te suffit alors suivre pas à pas l évolution de cette variable. C est un peu long mais tu devrai trouver d où vient le problème.
C est ce que j ai fait, et j ai tout de suite trouvé qu un index dépassait la limite de tableau. Il doit surement y en avoir d autres.
 
Et si tu trouve la ligne ou ça plante, on devrait pouvoir t aider plus facilement.

n°1323537
nargy
Posté le 12-03-2006 à 00:52:29  profilanswer
 

Un autre problème éventuel:
 
les noms et prénoms ne doivent pas faire plus de 19 caractères (plus le zero termial), et tu ne fait aucun test sur cette limite.

n°1323539
nargy
Posté le 12-03-2006 à 00:55:24  profilanswer
 

J ai pas vraiment compris le trie que tu effectue. Tu devrai peut être envisager d utiliser la fonction qsort().


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  Probléme de tri et affichage d'élément dans une structure

 

Sujets relatifs
Problème CSS pour un menuProblème cahce avec ie
Problème .htaccess il ne marche pas ?!!Problème d'exit qui ne fait pas son travail...
Probleme Recuperation donnees Textarea d'un formulaire avec split[PHP] Problème de connexion avec LDAP
PHP : problème d'envoi de mail + piece jointe sur Wanadooprobleme pour utiliser des fonctions c++ dans des dll
MS/SQL : affichage infos sur exec procedure stockeeProblème de sessions et de tables
Plus de sujets relatifs à : Probléme de tri et affichage d'élément dans une structure


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR