Commit 97749cb44ef75b8acf43f586f0d4467ef6b4bf13

Authored by tvieuble
1 parent 893dbc98

Correction segfault

Showing 1 changed file with 69 additions and 64 deletions   Show diff stats
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 3
4 -#define A 26 4 +#define A 27
5 5
6 struct node { 6 struct node {
7 char lettre; 7 char lettre;
@@ -13,96 +13,101 @@ struct cell { @@ -13,96 +13,101 @@ struct cell {
13 struct cell* arbreSuivant; 13 struct cell* arbreSuivant;
14 }; 14 };
15 15
  16 +void lien_listeFils(struct cell** pL) {
  17 + struct cell* p;
  18 + p = malloc(sizeof(struct cell));
  19 +
  20 + (*pL)->arbre->listeFils = p;
  21 +}
  22 +
16 void initialisation_tab_arbre(struct node tab[]) { 23 void initialisation_tab_arbre(struct node tab[]) {
17 - for(int i = 0; i < A; i++) {  
18 - tab[i].lettre = 97+i; //ajout lettres minuscules 24 + for(int i = 0; i < A-1; i++) {
  25 + tab[i].lettre = 97+i; //ajout lettres minuscules
  26 + tab[i].listeFils = NULL;
19 } 27 }
20 - tab[A].lettre = 39;  
21 - /*for(int i = 0; i < 8; i++) {  
22 - tab[i+26].lettre = 130+i; //ajout caractères spéciaux  
23 - }*/ 28 + tab[A-1].lettre = 39;
  29 + tab[A-1].listeFils = NULL;
24 } 30 }
25 31
26 void ajout_tete(char elem, struct cell** pL) { 32 void ajout_tete(char elem, struct cell** pL) {
27 struct cell* p; 33 struct cell* p;
28 p = malloc(sizeof(struct cell)); 34 p = malloc(sizeof(struct cell));
29 p->arbre = malloc(sizeof(struct node)); 35 p->arbre = malloc(sizeof(struct node));
  36 + p->arbre->listeFils = NULL;
30 p->arbre->lettre = elem; 37 p->arbre->lettre = elem;
31 p->arbreSuivant = *pL; 38 p->arbreSuivant = *pL;
32 *pL = p; 39 *pL = p;
33 } 40 }
34 41
35 -void lien_listeFils(struct cell** pL) {  
36 - struct cell* p;  
37 - p = malloc(sizeof(struct cell));  
38 -  
39 - (*pL)->arbre->listeFils = p;  
40 -}  
41 -  
42 -struct cell* insertion(char elem, struct cell** pL) {  
43 - printf("insert\n");  
44 - printf("*pL : %d\n", *pL);  
45 - if((*pL == NULL)||((*pL)->arbre->lettre > elem)) {  
46 - printf("condition1\n");  
47 - lien_listeFils(pL);  
48 - ajout_tete(elem, pL);  
49 - printf("return insertion : %d\n", (*pL)->arbre->listeFils);  
50 - return (*pL)->arbre->listeFils;  
51 - }  
52 - else if((*pL)->arbre->lettre == elem) { printf("condition2\n"); return (*pL)->arbre->listeFils;}  
53 - else {printf("esle\n"); insertion(elem, &(*pL)->arbreSuivant); } 42 +struct cell ** insertion(char elem, struct cell** pL) {
  43 + if(((*pL) == NULL) || ((*pL)->arbre->lettre > elem)) {
  44 + ajout_tete(elem, pL);
  45 + return &(*pL)->arbre->listeFils;
  46 + }
  47 + else if((*pL)->arbre->lettre == elem) {
  48 + return &(*pL)->arbre->listeFils;
  49 + }
  50 + else {
  51 + return insertion(elem, &(*pL)->arbreSuivant);
  52 + }
54 } 53 }
55 54
56 -/*void affiche_tab(struct node tab[]) {  
57 - for(int i = 0; i < 32; i++) {  
58 - printf("%c\n", tab[i].lettre);  
59 - }  
60 - }*/  
61 55
62 void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) { 56 void lire_fichier(FILE* fd, struct node tab_arbre_prcp[]) {
63 - struct cell* localisationArbre; 57 +
  58 + printf("lirefichier\n");
  59 + struct cell** localisationArbre;
64 char motLu[50]; 60 char motLu[50];
65 - int i = 0;  
66 - if(fd!=NULL)  
67 - {  
68 - while(fscanf(fd, "%s", motLu)==1)  
69 - {  
70 - if((motLu[i] >= 'a') && (motLu[i] <= 'z')) localisationArbre = tab_arbre_prcp[motLu[0]-97].listeFils;  
71 -  
72 - if(motLu[i] == 39) localisationArbre = tab_arbre_prcp[A].listeFils; //A = derniere case du tab 61 + while(fscanf(fd, "%s", motLu)==1) {
  62 + int i = 0;
  63 + if((motLu[0] >= 'a') && (motLu[0] <= 'z')) {
  64 + localisationArbre = &tab_arbre_prcp[motLu[0]-97].listeFils;
  65 + }
  66 +
  67 + if(motLu[0] == 39) {
  68 + localisationArbre = &tab_arbre_prcp[A].listeFils; //A = derniere case du tab
  69 + }
  70 + while(motLu[i] != '\0') {
  71 +
  72 + i += 1;
  73 + printf("lettre lue : %c address : %p\n", motLu[i], localisationArbre);
  74 + localisationArbre = insertion(motLu[i], localisationArbre);
  75 + printf("tab_arbre[%d].listeFils : %p\n",i, tab_arbre_prcp[i].listeFils);
  76 + /*printf("localisationArbre.lettre : %c\n", (*localisationArbre)->arbre->lettre);
  77 + printf("localisation apres : %p\n", localisationArbre);*/
  78 + printf("\n");
  79 + }
  80 + }
  81 + printf("\n");
  82 + fclose(fd);
73 83
74 - printf("avant while : localisation : %d\n", localisationArbre);  
75 - while(motLu[i] != '\0')  
76 - {  
77 - i += 1;  
78 - printf("lettre lue : %c address : %d\n", motLu[i], localisationArbre);  
79 - localisationArbre = insertion(motLu[i], &localisationArbre);  
80 - printf("localisation apres : %d\n", localisationArbre);  
81 - printf("\n");  
82 - }  
83 - printf("\n");  
84 - }  
85 - fclose(fd);  
86 - printf("fin lire fichier\n");  
87 - } 84 + printf("fin lire fichier\n");
  85 +
88 } 86 }
89 87
90 88
91 int main(int argc, char* argv[]) { 89 int main(int argc, char* argv[]) {
92 FILE* fd; 90 FILE* fd;
93 -  
94 struct node tab_arbre[A]; 91 struct node tab_arbre[A];
95 - struct node Arbre;  
96 - char lettre;  
97 -  
98 - if(argc>1) fd = fopen(argv[1], "r");  
99 92
100 - Arbre.listeFils = NULL; 93 + if(argc>1) fd = fopen(argv[1], "r");
  94 + else fd = NULL;
  95 + if (fd == NULL) {
  96 + printf("Error : couldn't open file\n");
  97 + return 1;
  98 + }
  99 + printf("avant init tab_arbre[1] %c\n", tab_arbre[1].lettre);
  100 + printf("fd : %p\n", fd);
101 initialisation_tab_arbre(tab_arbre); 101 initialisation_tab_arbre(tab_arbre);
  102 + printf("apres init tab_arbre[1] %c\n", tab_arbre[1].lettre);
  103 + printf("fd apres init : %p\n", fd);
  104 + printf("avant lire fichier\n");
102 lire_fichier(fd, tab_arbre); 105 lire_fichier(fd, tab_arbre);
103 - printf("tab_arbre[0].listeFils->arbre->lettre : %c\n", tab_arbre[3].listeFils->arbre->lettre);  
104 - //scanf("%c", &lettre);  
105 - //insertion(lettre, &(Arbre.listeFils));  
106 - //printf("lettre : %c\n", Arbre.listeFils->arbre->lettre); 106 + printf("tab_arbre[0].lettre : %c\n", tab_arbre[0].lettre);
  107 + printf("tab_arbre[0].listeFils : %p\n", tab_arbre[0].listeFils);
  108 + printf("tab_arbre[0].listeFils->arbre : %p\n", tab_arbre[0].listeFils->arbre);
  109 + printf("tab_arbre[1].listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->lettre);
  110 + printf("tab_arbre[0].listeFils->arbre->listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->listeFils->arbre->lettre);
  111 + printf("tab_arbre[0].listeFils->arbre->listeFils->arbre->listeFils->arbre->lettre : %c\n", tab_arbre[0].listeFils->arbre->listeFils->arbre->listeFils->arbre->lettre);
107 return 0; 112 return 0;
108 } 113 }