5e43fa77
rsSimonin
arbre.c
|
1
2
3
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
|
7d0a6328
rsimonin
modif creation
|
4
|
#include <string.h>
|
5e43fa77
rsSimonin
arbre.c
|
5
6
7
8
|
typedef struct arbre{
char val;
|
d6bcca7a
rsSimonin
initialisation
|
9
|
struct arbre *suite[26];
|
5e43fa77
rsSimonin
arbre.c
|
10
11
12
13
14
15
16
17
18
|
bool finmot; //1 si fin de mot
}Arbre;
typedef struct dico {
Arbre *alpha[26];
}Dico;
|
7d0a6328
rsimonin
modif creation
|
19
20
21
|
int calculcase(char c){
return 'c'-'a';
}
|
5e43fa77
rsSimonin
arbre.c
|
22
|
|
5e43fa77
rsSimonin
arbre.c
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
void arbre_vide(struct arbre ** pt_arbre)
{
*pt_arbre = NULL;
}
bool est_vide(struct arbre *arbre)
{
return arbre==NULL;
}
bool fin_de_mot(struct arbre *arbre)
{
return arbre->finmot;
}
|
5e43fa77
rsSimonin
arbre.c
|
37
|
|
d6bcca7a
rsSimonin
initialisation
|
38
|
void ini_dico(struct dico * pt_dico){
|
fc2a89b1
rsimonin
con
|
39
|
printf("ini_dico\n");
|
d6bcca7a
rsSimonin
initialisation
|
40
|
for(int i=0;i<26;i++){
|
7d0a6328
rsimonin
modif creation
|
41
|
pt_dico->alpha[i]=NULL;
|
d6bcca7a
rsSimonin
initialisation
|
42
43
|
}
}
|
7d0a6328
rsimonin
modif creation
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
void ajout_mot(struct arbre *arbrecourant,char *mot,int i){
// Arbre *arbrecourant=malloc(sizeof(struct arbre));
// if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){
// arbrecourant->val=mot[0];
// arbrecourant->finmot=false;
// for(int j=0;j<26;j++){
// arbrecourant->suite[j]=NULL;
// }
// }
// arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])];
//
// int i=1;
printf("%s:length:%d\n",mot,strlen(mot));
while(mot[i]!='\0'){
printf("\nlettre:%c; i=%d\n",mot[i],i);
if (arbrecourant->suite[calculcase(mot[i])]==NULL){
|
7a7157b0
rsimonin
modification crea...
|
64
|
printf("creation arbre\n");
|
41bf8d90
rsSimonin
correction de bug
|
65
66
67
|
Arbre *monarbre=malloc(sizeof(struct arbre));
monarbre->val=mot[i];
monarbre->finmot=false;
|
fc2a89b1
rsimonin
con
|
68
69
|
for(int j=0;j<26;j++){
monarbre->suite[j]=NULL;
|
41bf8d90
rsSimonin
correction de bug
|
70
|
}
|
7d0a6328
rsimonin
modif creation
|
71
72
|
arbrecourant=monarbre;
// printf("%c\n",(*pt_arbre)->val);
|
41bf8d90
rsSimonin
correction de bug
|
73
|
|
2e977567
rsSimonin
2 commit
|
74
|
}
|
7d0a6328
rsimonin
modif creation
|
75
|
arbrecourant=arbrecourant->suite[calculcase(mot[i])];
|
2e977567
rsSimonin
2 commit
|
76
|
i++;
|
7d0a6328
rsimonin
modif creation
|
77
|
ajout_mot(arbrecourant->suite[calculcase(mot[i])],mot,i);
|
2e977567
rsSimonin
2 commit
|
78
|
}
|
fc2a89b1
rsimonin
con
|
79
|
|
2e977567
rsSimonin
2 commit
|
80
81
|
}
|
41bf8d90
rsSimonin
correction de bug
|
82
|
void charger_arbre(FILE *fp, struct dico **ppt_dico)
|
2e977567
rsSimonin
2 commit
|
83
|
{ char mot[20];
|
41bf8d90
rsSimonin
correction de bug
|
84
|
ini_dico(*ppt_dico);
|
fc2a89b1
rsimonin
con
|
85
|
while (fscanf(fp, "%s", mot)==1){
|
fc2a89b1
rsimonin
con
|
86
|
printf("mot:%s\n",mot);
|
7d0a6328
rsimonin
modif creation
|
87
88
89
90
91
92
93
94
95
96
97
98
|
Arbre *arbrecourant=malloc(sizeof(struct arbre));
if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){
arbrecourant->val=mot[0];
arbrecourant->finmot=false;
for(int j=0;j<26;j++){
arbrecourant->suite[j]=NULL;
}
}
arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])];
int i=1;
ajout_mot(&arbrecourant,mot,i);
|
2e977567
rsSimonin
2 commit
|
99
100
|
}
return ;
|
2e977567
rsSimonin
2 commit
|
101
|
}
|
d6bcca7a
rsSimonin
initialisation
|
102
|
|
41bf8d90
rsSimonin
correction de bug
|
103
104
105
106
107
108
109
110
111
|
void free_arbre(struct arbre *pt_arbre){
if (pt_arbre==NULL){
return ;
}
for(int i=0;i<26;i++){
free_arbre(pt_arbre->suite[i]);
}
free(pt_arbre);
|
d6bcca7a
rsSimonin
initialisation
|
112
|
|
075f46e2
rsimonin
affichage et corr...
|
113
|
}
|
d6bcca7a
rsSimonin
initialisation
|
114
|
|
41bf8d90
rsSimonin
correction de bug
|
115
116
|
void free_dico(struct dico *pt_dico){
if (pt_dico==NULL){
|
075f46e2
rsimonin
affichage et corr...
|
117
118
119
|
return ;
}
for(int i=0;i<26;i++){
|
41bf8d90
rsSimonin
correction de bug
|
120
|
free_arbre(pt_dico->alpha[i]);
|
7a7157b0
rsimonin
modification crea...
|
121
|
free(pt_dico->alpha[i]);
|
075f46e2
rsimonin
affichage et corr...
|
122
|
}
|
41bf8d90
rsSimonin
correction de bug
|
123
124
|
free(pt_dico);
|
075f46e2
rsimonin
affichage et corr...
|
125
126
127
128
129
130
|
}
void affiche_arbre(struct arbre *arbre){
if(arbre==NULL){
return ;
}
|
7a7157b0
rsimonin
modification crea...
|
131
|
printf("%c\n",arbre->val);
|
075f46e2
rsimonin
affichage et corr...
|
132
133
134
135
|
for(int i=0;i<26;i++){
affiche_arbre(arbre->suite[i]);
}
}
|
d6bcca7a
rsSimonin
initialisation
|
136
|
|
41bf8d90
rsSimonin
correction de bug
|
137
138
139
140
141
142
|
void affiche_dico(struct dico *dico){
if(dico==NULL){
return ;
}
printf("---------------------------------------\n");
for(int i=0;i<26;i++){
|
fc2a89b1
rsimonin
con
|
143
|
printf("%d:",i);
|
41bf8d90
rsSimonin
correction de bug
|
144
145
146
147
148
149
|
affiche_arbre(dico->alpha[i]);
printf("\n");
}
}
|
d6bcca7a
rsSimonin
initialisation
|
150
|
|
5e43fa77
rsSimonin
arbre.c
|
151
|
int main (){
|
41bf8d90
rsSimonin
correction de bug
|
152
153
154
155
156
157
|
FILE* fp = fopen("dicotest.txt","r"); //amelioration entrée
if(fp == NULL){ return EXIT_FAILURE;} //File is not readable
struct dico *mondico=malloc(sizeof(struct dico));
charger_arbre(fp,&mondico);
affiche_dico(mondico);
|
5e43fa77
rsSimonin
arbre.c
|
158
159
|
|
41bf8d90
rsSimonin
correction de bug
|
160
161
|
free(mondico);
fclose(fp);
|
5e43fa77
rsSimonin
arbre.c
|
162
163
|
return 0;
}
|