Commit 6ab96f9ea9f3c093b3d6a8f26e51c74300853c54
1 parent
7d0a6328
tout marche
Showing
1 changed file
with
23 additions
and
33 deletions
Show diff stats
arbre.c
@@ -17,7 +17,7 @@ typedef struct dico { | @@ -17,7 +17,7 @@ typedef struct dico { | ||
17 | 17 | ||
18 | 18 | ||
19 | int calculcase(char c){ | 19 | int calculcase(char c){ |
20 | - return 'c'-'a'; | 20 | + return c-'a'; |
21 | } | 21 | } |
22 | 22 | ||
23 | void arbre_vide(struct arbre ** pt_arbre) | 23 | void arbre_vide(struct arbre ** pt_arbre) |
@@ -43,23 +43,13 @@ void ini_dico(struct dico * pt_dico){ | @@ -43,23 +43,13 @@ void ini_dico(struct dico * pt_dico){ | ||
43 | } | 43 | } |
44 | 44 | ||
45 | 45 | ||
46 | -void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ | ||
47 | -// Arbre *arbrecourant=malloc(sizeof(struct arbre)); | ||
48 | -// if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ | ||
49 | -// arbrecourant->val=mot[0]; | ||
50 | -// arbrecourant->finmot=false; | ||
51 | -// for(int j=0;j<26;j++){ | ||
52 | -// arbrecourant->suite[j]=NULL; | ||
53 | -// } | ||
54 | -// } | ||
55 | -// arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; | ||
56 | -// | ||
57 | -// int i=1; | ||
58 | - printf("%s:length:%d\n",mot,strlen(mot)); | 46 | +void ajout_mot(struct arbre **arbrecourant,char *mot,int i){ |
47 | + | ||
48 | + printf("%s:length:%zu \n",mot,strlen(mot)); | ||
59 | 49 | ||
60 | - while(mot[i]!='(mot[i]!='\0'){'){ | 50 | + if(mot[i]!='(mot[i]!='\0'){'){ |
61 | printf("\nlettre:%c; i=%d\n",mot[i],i); | 51 | printf("\nlettre:%c; i=%d\n",mot[i],i); |
62 | - if (arbrecourant->suite[calculcase(mot[i])]==NULL){ | 52 | + if (*arbrecourant==NULL){ |
63 | 53 | ||
64 | printf("creation arbre\n"); | 54 | printf("creation arbre\n"); |
65 | Arbre *monarbre=malloc(sizeof(struct arbre)); | 55 | Arbre *monarbre=malloc(sizeof(struct arbre)); |
@@ -68,13 +58,11 @@ void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ | @@ -68,13 +58,11 @@ void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ | ||
68 | for(int j=0;j<26;j++){ | 58 | for(int j=0;j<26;j++){ |
69 | monarbre->suite[j]=NULL; | 59 | monarbre->suite[j]=NULL; |
70 | } | 60 | } |
71 | - arbrecourant=monarbre; | 61 | + *arbrecourant=monarbre; |
72 | // printf("%c\n",(*pt_arbre)->val); | 62 | // printf("%c\n",(*pt_arbre)->val); |
73 | - | ||
74 | } | 63 | } |
75 | - arbrecourant=arbrecourant->suite[calculcase(mot[i])]; | ||
76 | i++; | 64 | i++; |
77 | - ajout_mot(arbrecourant->suite[calculcase(mot[i])],mot,i); | 65 | + ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i); |
78 | } | 66 | } |
79 | 67 | ||
80 | } | 68 | } |
@@ -83,19 +71,22 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | @@ -83,19 +71,22 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | ||
83 | { char mot[20]; | 71 | { char mot[20]; |
84 | ini_dico(*ppt_dico); | 72 | ini_dico(*ppt_dico); |
85 | while (fscanf(fp, "%s", mot)==1){ | 73 | while (fscanf(fp, "%s", mot)==1){ |
86 | - printf("mot:%s\n",mot); | 74 | + printf("mot:%s\n\n\n",mot); |
87 | 75 | ||
88 | - Arbre *arbrecourant=malloc(sizeof(struct arbre)); | 76 | + printf("\nlettre:%c\n",mot[0]); |
77 | + /* | ||
89 | if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ | 78 | if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ |
79 | + printf("creation 1er niveau\n"); | ||
80 | + Arbre *arbrecourant=malloc(sizeof(struct arbre)); | ||
90 | arbrecourant->val=mot[0]; | 81 | arbrecourant->val=mot[0]; |
91 | arbrecourant->finmot=false; | 82 | arbrecourant->finmot=false; |
92 | for(int j=0;j<26;j++){ | 83 | for(int j=0;j<26;j++){ |
93 | arbrecourant->suite[j]=NULL; | 84 | arbrecourant->suite[j]=NULL; |
94 | } | 85 | } |
95 | - } | ||
96 | - arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; | ||
97 | - int i=1; | ||
98 | - ajout_mot(&arbrecourant,mot,i); | 86 | + (*ppt_dico)->alpha[calculcase(mot[0])]=arbrecourant; |
87 | + }*/ | ||
88 | + //int index=1; | ||
89 | + ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0); | ||
99 | } | 90 | } |
100 | return ; | 91 | return ; |
101 | } | 92 | } |
@@ -106,29 +97,28 @@ void free_arbre(struct arbre *pt_arbre){ | @@ -106,29 +97,28 @@ void free_arbre(struct arbre *pt_arbre){ | ||
106 | return ; | 97 | return ; |
107 | } | 98 | } |
108 | for(int i=0;i<26;i++){ | 99 | for(int i=0;i<26;i++){ |
109 | - free_arbre(pt_arbre->suite[i]); | 100 | + free_arbre((pt_arbre->suite[i])); |
110 | } | 101 | } |
102 | + printf("lettre:%c\n",pt_arbre->val); | ||
111 | free(pt_arbre); | 103 | free(pt_arbre); |
112 | - | ||
113 | } | 104 | } |
114 | 105 | ||
115 | void free_dico(struct dico *pt_dico){ | 106 | void free_dico(struct dico *pt_dico){ |
116 | if (pt_dico==NULL){ | 107 | if (pt_dico==NULL){ |
117 | return ; | 108 | return ; |
118 | } | 109 | } |
110 | + | ||
119 | for(int i=0;i<26;i++){ | 111 | for(int i=0;i<26;i++){ |
120 | - free_arbre(pt_dico->alpha[i]); | ||
121 | - free(pt_dico->alpha[i]); | 112 | + free_arbre((pt_dico->alpha[i])); |
122 | } | 113 | } |
123 | - free(pt_dico); | ||
124 | 114 | ||
115 | + free(pt_dico); | ||
125 | } | 116 | } |
126 | 117 | ||
127 | void affiche_arbre(struct arbre *arbre){ | 118 | void affiche_arbre(struct arbre *arbre){ |
128 | if(arbre==NULL){ | 119 | if(arbre==NULL){ |
129 | return ; | 120 | return ; |
130 | } | 121 | } |
131 | - printf("%c\n",arbre->val); | ||
132 | for(int i=0;i<26;i++){ | 122 | for(int i=0;i<26;i++){ |
133 | affiche_arbre(arbre->suite[i]); | 123 | affiche_arbre(arbre->suite[i]); |
134 | } | 124 | } |
@@ -157,7 +147,7 @@ int main (){ | @@ -157,7 +147,7 @@ int main (){ | ||
157 | affiche_dico(mondico); | 147 | affiche_dico(mondico); |
158 | 148 | ||
159 | 149 | ||
160 | - free(mondico); | 150 | + free_dico(mondico); |
161 | fclose(fp); | 151 | fclose(fp); |
162 | return 0; | 152 | return 0; |
163 | } | 153 | } |