arbre.c
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct arbre{
char val;
struct arbre *suite[26];
bool finmot; //1 si fin de mot
}Arbre;
typedef struct dico {
Arbre *alpha[26];
}Dico;
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;
}
void cons_dico(struct dico **ppt_dico,char val){
Arbre *monarbre=malloc(sizeof(struct arbre));
monarbre->val=val;
monarbre->finmot=false;
for(int i=0;i<26;i++){
monarbre->suite[i]=NULL;
}
(*ppt_dico)->alpha[val-'a']=monarbre;
}
void ini_dico(struct dico * pt_dico){
printf("ini_dico");
for(int i=0;i<26;i++){
cons_dico(&pt_dico,'a'+i);
}
}
void ajout_mot(struct arbre **pt_arbre,char *mot, int i){
printf("%d",i);
printf("%c",mot[i]);
while(mot[i]!='\0'){
if (*pt_arbre==NULL){
printf("creation arbre\n");
Arbre *monarbre=malloc(sizeof(struct arbre));
monarbre->val=mot[i];
monarbre->finmot=false;
for(int i=0;i<26;i++){
monarbre->suite[i]=NULL;
}
pt_arbre=&monarbre;
printf("%c\n",(*pt_arbre)->val);
}
i++;
ajout_mot(pt_arbre,mot,i);
}
// (*pt_arbre)->finmot=true;
// printf("true\n");
}
void charger_arbre(FILE *fp, struct dico **ppt_dico)
{ char mot[20];
ini_dico(*ppt_dico);
while (fscanf(fp, "%s", mot)!=EOF){
printf("\nmot:%s\n",mot);
printf("%c\n",(*ppt_dico)->alpha[mot[0]-'a']->val);
ajout_mot(&((*ppt_dico)->alpha[mot[0]-'a']),mot,0);
}
return ;
}
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);
}
void free_dico(struct dico *pt_dico){
if (pt_dico==NULL){
return ;
}
for(int i=0;i<26;i++){
free_arbre(pt_dico->alpha[i]);
free(pt_dico->alpha[i]);
}
free(pt_dico);
}
void affiche_arbre(struct arbre *arbre){
if(arbre==NULL){
return ;
}
printf("%c\n",arbre->val);
for(int i=0;i<26;i++){
affiche_arbre(arbre->suite[i]);
}
}
void affiche_dico(struct dico *dico){
if(dico==NULL){
return ;
}
printf("---------------------------------------\n");
for(int i=0;i<26;i++){
printf("%dmot:",i);
affiche_arbre(dico->alpha[i]);
printf("\n");
}
}
int main (){
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);
free(mondico);
fclose(fp);
return 0;
}