Commit 41bf8d90c571cbe637380242fcd487e8a652bb1b

Authored by rsSimonin
1 parent e24487e2

correction de bug

Showing 1 changed file with 64 additions and 35 deletions   Show diff stats
@@ -34,85 +34,114 @@ bool fin_de_mot(struct arbre *arbre) @@ -34,85 +34,114 @@ bool fin_de_mot(struct arbre *arbre)
34 34
35 void cons_dico(struct dico **pt_dico,char val){ 35 void cons_dico(struct dico **pt_dico,char val){
36 Dico *mondico=malloc(sizeof(struct dico)); 36 Dico *mondico=malloc(sizeof(struct dico));
37 - mondico->alpha[val-97]->val=val; // (ascii)->a = 97  
38 - for(int i=0;i<26;i++){  
39 - mondico->alpha[val-97]->suite[i]=NULL;  
40 - }  
41 - mondico->alpha[val-97]->finmot=false;  
42 - (*pt_dico)=mondico;  
43 -}  
44 -  
45 -void cons_arbre(struct arbre **pt_arbre,char val){  
46 - struct arbre *monarbre=malloc(sizeof(struct arbre));  
47 - monarbre->val=val; // (ascii)->a = 97 37 + Arbre *monarbre=malloc(sizeof(struct arbre));
  38 + monarbre->val=val;
  39 + monarbre->finmot=false;
  40 +
48 for(int i=0;i<26;i++){ 41 for(int i=0;i<26;i++){
49 monarbre->suite[i]=NULL; 42 monarbre->suite[i]=NULL;
50 } 43 }
51 - monarbre->finmot=false;  
52 - (*pt_arbre)=monarbre;  
53 -}  
54 -  
55 44
  45 + mondico->alpha[val-'a']=monarbre;
  46 + (*pt_dico)=mondico;
  47 +}
56 48
57 void ini_dico(struct dico * pt_dico){ 49 void ini_dico(struct dico * pt_dico){
  50 + printf("ini_dico");
58 for(int i=0;i<26;i++){ 51 for(int i=0;i<26;i++){
59 - cons_dico(&pt_dico,97+i); 52 + cons_dico(&pt_dico,'a'+i);
60 } 53 }
61 } 54 }
62 55
63 56
64 -void ajout_mot(struct dico *pt_dico,char mot[]){  
65 - int i=0; 57 +void ajout_mot(struct arbre *pt_arbre,char *mot, int i){
  58 +
66 while(mot[i]!='\0'){ 59 while(mot[i]!='\0'){
67 - if (pt_dico->alpha[mot[i]-'a']==NULL){  
68 - cons_arbre(&(pt_dico->alpha[mot[i]-'a']),mot[i]);  
69 - 60 +
  61 +
  62 + if (pt_arbre==NULL){
  63 + Arbre *monarbre=malloc(sizeof(struct arbre));
  64 + monarbre->val=mot[i];
  65 + monarbre->finmot=false;
  66 + for(int i=0;i<26;i++){
  67 + monarbre->suite[i]=NULL;
  68 + }
  69 + pt_arbre=monarbre;
  70 +
70 } 71 }
71 i++; 72 i++;
  73 + ajout_mot(pt_arbre,mot,i);
72 } 74 }
73 - pt_dico->alpha[mot[i]-'a']->finmot=true;  
74 - 75 +
75 } 76 }
76 77
77 -void charger_arbre(FILE *fp, struct dico **pt_dico) 78 +void charger_arbre(FILE *fp, struct dico **ppt_dico)
78 { char mot[20]; 79 { char mot[20];
  80 + ini_dico(*ppt_dico);
79 while (fscanf(fp, "%s", mot)!=EOF){ 81 while (fscanf(fp, "%s", mot)!=EOF){
80 - ajout_mot(*pt_dico,mot); 82 + printf("\nmot:%s\n",mot);
  83 + ajout_mot(((*ppt_dico)->alpha[mot[0]-'a']),mot,0);
81 } 84 }
82 return ; 85 return ;
83 } 86 }
84 87
85 -void free_arbre(struct dico **pt_dico){  
86 - if (*pt_dico-> 88 +
  89 +void free_arbre(struct arbre *pt_arbre){
  90 + if (pt_arbre==NULL){
  91 + return ;
  92 + }
  93 + for(int i=0;i<26;i++){
  94 + free_arbre(pt_arbre->suite[i]);
  95 + }
  96 + free(pt_arbre);
87 97
88 } 98 }
89 99
90 -void affiche_dico(struct dico *dico){  
91 - if(dico==NULL){ 100 +void free_dico(struct dico *pt_dico){
  101 + if (pt_dico==NULL){
92 return ; 102 return ;
93 } 103 }
94 for(int i=0;i<26;i++){ 104 for(int i=0;i<26;i++){
95 - affiche_arbre(dico->alpha[i]); 105 + free_arbre(pt_dico->alpha[i]);
96 } 106 }
  107 + free(pt_dico);
  108 +
97 } 109 }
98 110
99 void affiche_arbre(struct arbre *arbre){ 111 void affiche_arbre(struct arbre *arbre){
100 if(arbre==NULL){ 112 if(arbre==NULL){
101 return ; 113 return ;
102 } 114 }
103 - printf("%c\n",arbre->val); 115 + printf("%c:xxx\n",arbre->val);
104 for(int i=0;i<26;i++){ 116 for(int i=0;i<26;i++){
105 affiche_arbre(arbre->suite[i]); 117 affiche_arbre(arbre->suite[i]);
106 } 118 }
107 } 119 }
108 120
  121 +void affiche_dico(struct dico *dico){
  122 + if(dico==NULL){
  123 + return ;
  124 + }
  125 + printf("---------------------------------------\n");
  126 + for(int i=0;i<26;i++){
  127 + printf("%d:zzz",i);
  128 + affiche_arbre(dico->alpha[i]);
  129 + printf("\n");
  130 + }
  131 +
  132 +}
  133 +
109 134
110 int main (){ 135 int main (){
111 - Dico *mondico=malloc(sizeof(struct dico));  
112 - charger_arbre("dicotest.txt",mondico);  
113 -  
114 - 136 + FILE* fp = fopen("dicotest.txt","r"); //amelioration entrée
  137 + if(fp == NULL){ return EXIT_FAILURE;} //File is not readable
  138 +
  139 + struct dico *mondico=malloc(sizeof(struct dico));
  140 + charger_arbre(fp,&mondico);
  141 + affiche_dico(mondico);
115 142
116 143
  144 + free(mondico);
  145 + fclose(fp);
117 return 0; 146 return 0;
118 } 147 }