diff --git a/projet0segfault.c b/projet0segfault.c index db40c64..47e8a98 100644 --- a/projet0segfault.c +++ b/projet0segfault.c @@ -1,7 +1,7 @@ #include #include -#define A 26 +#define A 27 struct node { char lettre; @@ -13,96 +13,101 @@ struct cell { struct cell* arbreSuivant; }; +void lien_listeFils(struct cell** pL) { + struct cell* p; + p = malloc(sizeof(struct cell)); + + (*pL)->arbre->listeFils = p; +} + void initialisation_tab_arbre(struct node tab[]) { - for(int i = 0; i < A; i++) { - tab[i].lettre = 97+i; //ajout lettres minuscules + for(int i = 0; i < A-1; i++) { + tab[i].lettre = 97+i; //ajout lettres minuscules + tab[i].listeFils = NULL; } - tab[A].lettre = 39; - /*for(int i = 0; i < 8; i++) { - tab[i+26].lettre = 130+i; //ajout caractères spéciaux - }*/ + tab[A-1].lettre = 39; + tab[A-1].listeFils = NULL; } void ajout_tete(char elem, struct cell** pL) { struct cell* p; p = malloc(sizeof(struct cell)); p->arbre = malloc(sizeof(struct node)); + p->arbre->listeFils = NULL; p->arbre->lettre = elem; p->arbreSuivant = *pL; *pL = p; } -void lien_listeFils(struct cell** pL) { - struct cell* p; - p = malloc(sizeof(struct cell)); - - (*pL)->arbre->listeFils = p; -} - -struct cell* insertion(char elem, struct cell** pL) { - printf("insert\n"); - printf("*pL : %d\n", *pL); - if((*pL == NULL)||((*pL)->arbre->lettre > elem)) { - printf("condition1\n"); - lien_listeFils(pL); - ajout_tete(elem, pL); - printf("return insertion : %d\n", (*pL)->arbre->listeFils); - return (*pL)->arbre->listeFils; - } - else if((*pL)->arbre->lettre == elem) { printf("condition2\n"); return (*pL)->arbre->listeFils;} - else {printf("esle\n"); insertion(elem, &(*pL)->arbreSuivant); } +struct cell ** insertion(char elem, struct cell** pL) { + if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) { + ajout_tete(elem, pL); + return &(*pL)->arbre->listeFils; + } + else if((*pL)->arbre->lettre == elem) { + return &(*pL)->arbre->listeFils; + } + else { + return insertion(elem, &(*pL)->arbreSuivant); + } } -/*void affiche_tab(struct node tab[]) { - for(int i = 0; i < 32; i++) { - printf("%c\n", tab[i].lettre); - } - }*/ void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) { - struct cell* localisationArbre; + + printf("lirefichier\n"); + struct cell** localisationArbre; char motLu[50]; - int i = 0; - if(fd!=NULL) - { - while(fscanf(fd, "%s", motLu)==1) - { - if((motLu[i] >= 'a') && (motLu[i] <= 'z')) localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils; - - if(motLu[i] == 39) localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab + while(fscanf(fd, "%s", motLu)==1) { + int i = 0; + if((motLu[0] >= 'a') && (motLu[0] <= 'z')) { + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils; + } + + if(motLu[0] == 39) { + localisationArbre = &tab_arbre_prcp[A].listeFils; //A = derniere case du tab + } + while(motLu[i] != '\0') { + + i += 1; + printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre); + localisationArbre = insertion(motLu[i], localisationArbre); + printf("tab_arbre[%d].listeFils : %p\n",i, tab_arbre_prcp[i].listeFils); + /*printf("localisationArbre.lettre : %c\n", (*localisationArbre)->arbre->lettre); + printf("localisation apres : %p\n", localisationArbre);*/ + printf("\n"); + } + } + printf("\n"); + fclose(fd); - printf("avant while : localisation : %d\n", localisationArbre); - while(motLu[i] != '\0') - { - i += 1; - printf("lettre lue : %c address : %d\n", motLu[i], localisationArbre); - localisationArbre = insertion(motLu[i], &localisationArbre); - printf("localisation apres : %d\n", localisationArbre); - printf("\n"); - } - printf("\n"); - } - fclose(fd); - printf("fin lire fichier\n"); - } + printf("fin lire fichier\n"); + } int main(int argc, char* argv[]) { FILE* fd; - struct node tab_arbre[A]; - struct node Arbre; - char lettre; - - if(argc>1) fd = fopen(argv[1], "r"); - Arbre.listeFils = NULL; + if(argc>1) fd = fopen(argv[1], "r"); + else fd = NULL; + if (fd == NULL) { + printf("Error : couldn't open file\n"); + return 1; + } + printf("avant init tab_arbre[1] %c\n", tab_arbre[1].lettre); + printf("fd : %p\n", fd); initialisation_tab_arbre(tab_arbre); + printf("apres init tab_arbre[1] %c\n", tab_arbre[1].lettre); + printf("fd apres init : %p\n", fd); + printf("avant lire fichier\n"); lire_fichier(fd, tab_arbre); - printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[3].listeFils->arbre->lettre); - //scanf("%c", &lettre); - //insertion(lettre, &(Arbre.listeFils)); - //printf("lettre : %c\n", Arbre.listeFils->arbre->lettre); + printf("tab_arbre[0].lettre : %c\n", tab_arbre[0].lettre); + printf("tab_arbre[0].listeFils : %p\n", tab_arbre[0].listeFils); + printf("tab_arbre[0].listeFils->arbre : %p\n", tab_arbre[0].listeFils->arbre); + printf("tab_arbre[1].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre); + printf("tab_arbre[0].listeFils->arbre->listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->listeFils->arbre->lettre); + printf("tab_arbre[0].listeFils->arbre->listeFils->arbre->listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->listeFils->arbre->listeFils->arbre->lettre); return 0; } -- libgit2 0.21.2