From 893dbc9885d10c9c600b31259552c7e38f6a6114 Mon Sep 17 00:00:00 2001 From: TanguyV Date: Thu, 4 Apr 2019 12:25:45 +0200 Subject: [PATCH] update projet0segfault.c --- projet0segfault.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+), 0 deletions(-) create mode 100644 projet0segfault.c diff --git a/projet0segfault.c b/projet0segfault.c new file mode 100644 index 0000000..db40c64 --- /dev/null +++ b/projet0segfault.c @@ -0,0 +1,108 @@ +#include +#include + +#define A 26 + +struct node { + char lettre; + struct cell* listeFils; +}; + +struct cell { + struct node* arbre; + struct cell* arbreSuivant; +}; + +void initialisation_tab_arbre(struct node tab[]) { + for(int i = 0; i < A; i++) { + tab[i].lettre = 97+i; //ajout lettres minuscules + } + tab[A].lettre = 39; + /*for(int i = 0; i < 8; i++) { + tab[i+26].lettre = 130+i; //ajout caractères spéciaux + }*/ +} + +void ajout_tete(char elem, struct cell** pL) { + struct cell* p; + p = malloc(sizeof(struct cell)); + p->arbre = malloc(sizeof(struct node)); + 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); } +} + +/*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; + 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 + + 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"); + } +} + + +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; + initialisation_tab_arbre(tab_arbre); + 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); + return 0; +} -- libgit2 0.21.2