arbre.c
3.54 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define TAILLE 27
//#include "arbre.h"
typedef struct arbre{
char val;
struct arbre *suite[TAILLE];
bool finmot; //1 si fin de mot
}Arbre;
typedef struct dico {
Arbre *alpha[TAILLE];
}Dico;
int calculcase(char c)
{
if(c==39){
return 26;
}
else if(c<97){
return c-'A';
}
else {
return c-'a';
}
}
bool est_vide(struct arbre *arbre)
{
return arbre==NULL;
}
bool mot_existe(struct arbre *monarbre,char *mot,int i){
if (monarbre==NULL){
// printf("false\n");
return false;
}
// printf("%c \n",(monarbre->val));
if (mot[i+1]=='\0'){
// printf("finmot\n");
return monarbre->finmot;
}
else {
i++;
mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
}
}
void ini_dico(struct dico *pt_dico)
{
printf("ini_dico\n");
for(int i=0;i<TAILLE;i++){
pt_dico->alpha[i]=NULL;
}
}
void creation_arbre(Arbre **ppt_arbre,char c)
{
// printf("creation arbre\n");
Arbre *monarbre=malloc(sizeof(struct arbre));
monarbre->val=c;
monarbre->finmot=false;
for(int j=0;j<TAILLE;j++){
monarbre->suite[j]=NULL;
}
*ppt_arbre=monarbre;
}
void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
{
// printf("%s:length:%zu \n",mot,strlen(mot));
if(mot[i]!='\0'){
// printf("\nlettre:%c; i=%d\n",mot[i],i);
if (*arbrecourant==NULL){
creation_arbre(arbrecourant,mot[i]);
}
i++;
if(mot[i+1]=='\0'){
printf("findemot\n");
(*arbrecourant)->finmot=true;
}
ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
}
}
void charger_arbre(FILE *fp, struct dico **ppt_dico)
{ char mot[20];
ini_dico(*ppt_dico);
while (fscanf(fp, "%s", mot)==1){
// printf("mot:%s\n\n\n",mot);
// printf("\nlettre:%c\n",mot[0]);
ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
}
return ;
}
void free_arbre(struct arbre *pt_arbre)
{
if (pt_arbre==NULL){
return ;
}
for(int i=0;i<TAILLE;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<TAILLE;i++){
free_arbre((pt_dico->alpha[i]));
}
free(pt_dico);
}
void affiche_arbre(struct arbre *arbre)
{
if(arbre==NULL){
return ;
}
printf("%c:",arbre->val);
for(int i=0;i<TAILLE;i++){
affiche_arbre(arbre->suite[i]);
}
}
void affiche_dico(struct dico *dico)
{
if(dico==NULL){
return ;
}
printf("---------------------------------------\n");
for(int i=0;i<TAILLE;i++){
printf("%d:",i);
affiche_arbre(dico->alpha[i]);
printf("\n");
}
}
int main (){
FILE* fp = fopen("words-no-accents","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);
char mot[]="zombie";
char mot2[]="zombbbiiee";
// mot_existe(mondico->alpha[calculcase(mot[0])],mot,0);
// mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0);
printf("1:%d \n",mot_existe(mondico->alpha[calculcase(mot[0])],mot,0));
printf("3:%d \n",mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0));
free_dico(mondico);
fclose(fp);
return 0;
}