1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include <stdio.h>
#include <stdlib.h>
#define NEW(x) (x*)malloc(sizeof(x))
typedef struct {
int numPersonne;
char *nom;
char *prenom;
int poids;
float note;
} PERSONNE;
typedef struct bidon {
void *PERSONNE;
int nbFils;
struct bidon *tabFils[5];
} NOEUD;
typedef struct {
NOEUD *racine;
} ARBRE;
int niveau_noeud = 0;
// Fonction d'affichage de l'arbre
void affiche_personne (NOEUD *n) {
int i;
PERSONNE *p = n->PERSONNE;
i=0;
//On affiche l'objet du noeud
printf("%s\n",p->nom);
/* Si le noeud possède au moins un fils on incrémente le niveau
du noeud */
if (n->nbFils > 0) niveau_noeud++;
/* On crée autant de décalage vers la droite qu'il y a de niveau de noeud*
pour l'affichage */
for (i=0;i<niveau_noeud;i++)
printf(" ");
// On traite les fils
for (i=0;i<n->nbFils;i++) {
/* Une fois atteint la dernière feuille d'un noeud , on remonte d'un niveau
Tant qu'on se situe après un noeud de 1er niveau */
if (niveau_noeud > 1) {
if ((n->nbFils > 1) && (i == n->nbFils-1))
niveau_noeud--;
if ((n->nbFils > 0) && (i == n->nbFils))
niveau_noeud--;
}
affiche_personne(n->tabFils[i]);
}
}
int main(int argc, char *argv[])
{
PERSONNE _tb[] = {
{1793,"Seth","Dominique",78,7.5},
{1968,"Enoch","Abdelkader",14,8.25},
{1418,"Irad","Igor",71,12.5},
{1515,"Adam","Constantin",68,12.5},
{1111,"Henosh","Marcel",73,13.},
{6699,"Noe","Georges",27,8.5},
{1984,"Cain","Apollinaire",45,8.25},
{1789,"Abel","Aristide",18,11.75},
{9669,"Sem","Maurice",43,7.5},
{3945,"Cham","Leonardo",99,12.5},
{3141,"Japhet","Aziz",66,5.25}
};
NOEUD _tb2[] = {
{ &_tb[7], 0, { 0, 0, 0, 0, 0 } }, // Abel - 0
{ &_tb[0], 1, { &_tb2[9], 0, 0, 0, 0 } }, // Seth - 1
{ &_tb[3], 3, { &_tb2[6], &_tb2[0], &_tb2[1], 0, 0 } }, // Adam - 2
{ &_tb[1], 0, { 0, 0, 0, 0, 0 } }, // Enoch - 3
{ &_tb[5], 3, { &_tb2[5], &_tb2[7], &_tb2[8], 0, 0 } }, // Noe - 4
{ &_tb[8], 0, { 0, 0, 0, 0, 0 } }, // Sem - 5
{ &_tb[6], 2, { &_tb2[3], &_tb2[10], 0, 0, 0 } }, // Cain - 6
{ &_tb[9], 0, { 0, 0, 0, 0, 0 } }, // Cham - 7
{ &_tb[10], 0, { 0, 0, 0, 0, 0 } }, // Japhet - 8
{ &_tb[4], 1, { &_tb2[4], 0, 0, 0, 0 } }, // Henosh - 9
{ &_tb[2], 0, { 0, 0, 0, 0, 0 } } // Irad - 10
};
// Adam
// Cain
// Enoch
// Irad
// Abel
// Seth
// Henoch
// Noe
// Sem
// Cham
// Japhet
ARBRE *a = NEW(ARBRE);
a->racine = &_tb2[2];
affiche_personne(a->racine);
return 0;
} |
Partager