MAD_DIM  | 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 :
 - #include<stdio.h>
 - #include<string.h>
 - #include<stdlib.h>
 - struct DATE { int jour;
 -               int mois;
 -   	  int annee;
 - };
 - struct PERS { int ref;
 -               char nom[20];
 -   	  char prenom[20];
 -   	  struct DATE nais;
 - };
 - struct INDEX { char nom[20];
 -                struct PERS *adresse;
 - };
 - void Init(struct PERS *, struct INDEX *, int, int *);
 - void Liste(struct INDEX *, int *, int);
 - void Recherche(struct INDEX *, int *, int);
 - void Ajout(struct INDEX *, int *, int);
 - void Suppression(struct INDEX *, int *, int);
 - void HeapSort(struct INDEX *, int, int *);
 - void Paterner(struct INDEX *, int, int, int *);
 - void main()
 - {
 - 	int nbel = 5;
 - 	int *pt;
 - 	int choix;
 - 	bool fin;
 -     struct PERS *per;
 - 	struct INDEX *ind;
 - 	int occup[10] = {1, 0, 1, 1, 0, 0, 0, 0, 0};
 -     struct DATE nais[] = {{29, 3, 1980}, {20, 8, 1975}, {01, 2, 1978},
 - 	{10, 5, 1979}, {25, 10, 1981}};
 -    
 - 	struct PERS perso[] = {{127, "Dupont", "Jean", {29, 3, 1980}},
 - 	{205, "Durand", "Jean", {20, 8, 1975}}, {135, "Dupond", "Marc", {01, 2, 1978}},
 - 	{128, "Pire", "Julien", {10, 5, 1979}}, {112, "Meurice", "Xavier", {25, 10, 1981}}};
 - 	struct INDEX index[10];
 - 	per = &perso[0];
 - 	ind = &index[0];
 - 	pt = &occup[0];
 - 	Init(per, ind, nbel, pt);
 - 	do
 - 	{
 -   fin = false;
 -   printf("\n                     MENU\n" );
 -   printf("                     ----\n\n" );
 -   printf("     1.Liste\n" );
 -   printf("     2.Recherche\n" );
 -   printf("     3.Ajout\n" );
 -   printf("     4.Suppression\n" );
 -   printf("\nChoix : " );
 -   fflush(stdin);
 -   scanf("%d", &choix);
 -   switch(choix)
 -   {
 -   case 1:
 -   	system("cls" );
 -   	Liste(ind, pt, nbel);
 -   	break;
 -   case 2:
 -   	system("cls" );
 -   	Recherche(ind, pt, nbel);
 -   	break;
 -   case 3:
 -   	system("cls" );
 -   	Ajout(ind, pt, nbel);
 -   	break;
 -   case 4:
 -   	system("cls" );
 -   	Suppression(ind, pt, nbel);
 -   	break;
 -   case 5:
 -   	fin = true;
 -   	break;
 -   }
 - 	}while(fin != true);
 - }
 - void Init(struct PERS *per, struct INDEX *ind, int nbel, int *pt)
 - {
 - 	int i;
 - 	struct INDEX *indeb;
 - 	indeb = ind;
 - 	for(i=0;i<nbel;i++)
 - 	{
 -   strcpy(ind->nom, per->nom);
 -   ind->adresse = per;
 -   ind ++;
 -   per++;
 - 	}
 - 	HeapSort(indeb, nbel, pt);
 - }
 - void Liste(struct INDEX *ind, int *pt, int nbel)
 - {
 - 	int i;
 - 	printf("\n                           LISTE DES MEMBRES\n" );
 - 	printf("                           -----------------\n\n" );
 - 	for(i=0;i<nbel;i++)
 - 	{
 -   if(*(pt+i) == 1)
 -   {
 -   	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);
 -   	printf("\n" );
 -   }
 -   ind++;
 - 	}
 - }
 - void Recherche(struct INDEX *ind, int *pt, int nbel)
 - {
 - 	int i, nbcar;
 - 	char nom[20];
 - 	bool trouve;
 - 	printf("\n                       RECHERCHE D UN MEMBRE\n" );
 - 	printf("                       ---------------------\n\n" );
 - 	printf("Entrez le nom du membre que vous recherchez : " );
 - 	fflush(stdin);
 - 	gets(nom);
 - 	nbcar = strlen(nom);
 - 	nom[0] = toupper(nom[0]);
 - 	for(i=1;i<nbcar;i++)
 - 	{
 -   nom[i] = tolower(nom[i]);
 - 	}
 - 	i=0;
 - 	while(i<nbel && trouve != true)
 - 	{
 -   strcpy(ind->nom, ind->nom);
 -   if(strcmp(nom, ind->nom) == NULL && *(pt+i) == 1)
 -   {
 -   	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);
 -   	trouve = true;
 -   }
 -   printf("\n" );
 -   ind++;
 -   i++;
 - 	}
 - }
 - void Ajout(struct INDEX *ind, int *pt, int nbel)
 - {
 - 	int i, j, testint, nbcar;
 - 	struct INDEX *ptdeb;
 - 	printf("\n                           AJOUT D UN MEMBRE\n" );
 - 	printf("                           -----------------\n\n" );
 - 	ptdeb = ind;
 - 	i=0;
 - 	while(i<nbel && *(pt+i) == 1)
 - 	{
 -   i++;
 -   ind++;
 - 	}
 - 	printf("Entrez la reference : " );
 - 	fflush(stdin);
 - 	scanf("%d", &ind->adresse->ref);
 - 	do
 - 	{
 -   printf("Entrez le nom : " );
 -   fflush(stdin);
 -   gets(ind->adresse->nom);
 -   nbcar = strlen(ind->adresse->nom);
 -   if(nbcar == 0)
 -   {
 -   	printf("VOUS DEVEZ ENTRER UN NOM !!!\n" );
 -   }
 - 	}while(nbcar == 0);
 - 	ind->adresse->nom[0] = toupper(ind->adresse->nom[0]);
 - 	for(j=1;j<nbcar;j++)
 - 	{
 -   ind->adresse->nom[j] = tolower(ind->adresse->nom[j]);
 - 	}
 - 	strcpy(ind->nom, ind->adresse->nom);
 - 	do
 - 	{
 -   printf("Entrez le prenom : " );
 -   fflush(stdin);
 -   gets(ind->adresse->prenom);
 -   nbcar = strlen(ind->adresse->prenom);
 -   if(nbcar == 0)
 -   {
 -   	printf("VOUS DEVEZ ENTRER UN PRENOM !!!\n" );
 -   }
 - 	}while(nbcar == 0);
 - 	ind->adresse->prenom[0] = toupper(ind->adresse->prenom[0]);
 - 	for(j=1;j<nbcar;j++)
 - 	{
 -   ind->adresse->prenom[j] = tolower(ind->adresse->prenom[j]);
 - 	}
 - 	do
 - 	{
 -   printf("Entrez le jour : " );
 -   fflush(stdin);
 -   testint = scanf("%d", &ind->adresse->nais.jour);
 -   if(testint == 0 || ind->adresse->nais.jour < 1 || ind->adresse->nais.jour > 31)
 -   {
 -   	printf("JOUR INVALIDE !!!\n" );
 -   }
 - 	}while(testint == 0 || ind->adresse->nais.jour < 1 || ind->adresse->nais.jour > 31);
 - 	do
 - 	{
 -   printf("Entrez le mois : " );
 -   fflush(stdin);
 -   testint = scanf("%d", &ind->adresse->nais.mois);
 -   if(testint == 0 || ind->adresse->nais.mois < 1 || ind->adresse->nais.mois > 12)
 -   {
 -   	printf("MOIS INVALIDE !!!\n" );
 -   }
 - 	}while(testint == 0 || ind->adresse->nais.mois < 1 || ind->adresse->nais.mois > 12);
 - 	do
 - 	{
 -   printf("Entrez le annee : " );
 -   fflush(stdin);
 -   testint = scanf("%d", &ind->adresse->nais.annee);
 -   if(testint == 0 || ind->adresse->nais.annee > 2005)
 -   {
 -   	printf("ANNEE INVALIDE !!!\n" );
 -   }
 - 	}while(testint == 0 || ind->adresse->nais.annee > 2005);
 - 	*(pt+i) = 1;
 - 	system("cls" );
 - 	ind = ptdeb;
 - 	HeapSort(ind, nbel, pt);
 - 	ind = ptdeb;
 - 	Liste(ind, pt, nbel);
 - }
 - void Suppression(struct INDEX *ind, int *pt, int nbel)
 - {
 - 	int i, nbcar;
 - 	char nom[20];
 - 	bool efface;
 - 	struct INDEX *ptdeb;
 - 	printf("\n                           SUPPRESSION D UN MEMBRE\n" );
 - 	printf("                           -----------------------\n\n" );
 - 	ptdeb = ind;
 - 	do
 - 	{
 -   printf("Entrez le nom du membre a effacer : " );
 -   fflush(stdin);
 -   gets(nom);
 -   nbcar = strlen(nom);
 -   if(nbcar == 0)
 -   {
 -   	printf("VOUS DEVEZ ENTRER UN NOM !!!\n" );
 -   }
 - 	}while(nbcar == 0);
 - 	nom[0] = toupper(nom[0]);
 - 	for(i=1;i<nbcar;i++)
 - 	{
 -   nom[i] = tolower(nom[i]);
 - 	}
 - 	i=0;
 - 	while(i<nbel && efface != true)
 - 	{
 -   strcpy(ind->nom, ind->nom);
 -   if(strcmp(nom, ind->nom) == NULL)
 -   {
 -   	*(pt+i) = 0;
 -   	efface = true;
 -   }
 -   ind++;
 -   i++;
 - 	}
 - 	system("cls" );
 - 	ind = ptdeb;
 - 	Liste(ind, pt, nbel);
 - }
 - void HeapSort(struct INDEX *ind, int nbel, int *pt)
 - {
 - 	int i, tmp3;
 - 	char tmp[20];
 - 	struct PERS *tmp1;
 - 	/* Paterner 1X tous le vecteur */
 - 	for (i = (nbel / 2)-1; i >= 0; i--)
 - 	{
 -    Paterner(ind, i, nbel, pt);
 - 	}
 - 	for (i = nbel-1; i >= 1; i--)
 - 	{
 - 	    /* Echange le premier et le dernier élément */
 -   strcpy(tmp, ind->nom);
 -   strcpy(ind->nom, (ind+i)->nom);
 -   strcpy((ind+i)->nom, tmp);
 -   tmp1 = ind->adresse;
 -   ind->adresse = (ind+i)->adresse;
 -   (ind+i)->adresse = tmp1;
 -   tmp3 = *pt;
 -   *pt = *(pt+i);
 -   *(pt+i) = tmp3;
 -   /* Paterne une 2X le vecteur avec nombre d'éléments - 1 */
 -   Paterner(ind, 0, i-1, pt);
 - 	}
 - }
 - void Paterner(struct INDEX *ind, int pere, int finvec, int *pt)
 - {
 -   int trie, fils, tmp3;
 -   char tmp[20];
 -   struct PERS *tmp1;
 -   trie = 0;
 -   /**************************************************************************/
 -   /* Boucle tant que indice du pere*2 est plus petit que le dernier élément */
 -   /**************************************************************************/
 -   while ((pere*2 <= finvec) && (trie != 1))
 -   {
 - 	  if (pere*2 == finvec)
 - 	  {
 -    fils = pere*2;
 - 	  }
 - 	  else
 - 	  {
 -    if (strcmp((ind+pere*2)->nom, (ind+(pere*2 + 1))->nom) > 0)
 -    {
 -   	fils = pere*2;
 -    }
 -    else
 -    {
 -   	fils = pere*2 + 1;
 -    }
 - 	  }
 - 	  if (strcmp((ind+pere)->nom, (ind+fils)->nom) < 0)
 - 	  {
 -   strcpy(tmp, (ind+pere)->nom);
 -   strcpy((ind+pere)->nom, (ind+fils)->nom);
 -   strcpy((ind+fils)->nom, tmp);
 -   tmp1 = (ind+pere)->adresse;
 -   (ind+pere)->adresse = (ind+fils)->adresse;
 -   (ind+fils)->adresse = tmp1;
 -   tmp3 = *(pt+pere);
 -   *(pt+pere) = *(pt+fils);
 -   *(pt+fils) = tmp3;
 -   pere = fils;
 - 	  }
 - 	  else
 - 	  {
 -    trie = 1;
 - 	  }
 -   }
 - }
 
  |  
    Message édité par MAD_DIM le 11-03-2006 à 13:32:18
  |