Commit c62798e8c8f2ac51efe3f1f6f0df7775564db14c
1 parent
f3c35adc
ajout fonction motdansarbre
Showing
1 changed file
with
55 additions
and
32 deletions
Show diff stats
arbre.c
1 | -#include "arbre.h" | 1 | +#include <stdio.h> |
2 | +#include <stdlib.h> | ||
3 | +#include <stdbool.h> | ||
4 | +#include <string.h> | ||
5 | +#define TAILLE 27 | ||
2 | 6 | ||
7 | +//#include "arbre.h" | ||
3 | 8 | ||
4 | -// typedef struct arbre{ | ||
5 | -// char val; | ||
6 | -// struct arbre *suite[TAILLE]; | ||
7 | -// bool finmot; //1 si fin de mot | ||
8 | -// }Arbre; | ||
9 | -// | ||
10 | -// typedef struct dico { | ||
11 | -// Arbre *alpha[TAILLE]; | ||
12 | -// }Dico; | 9 | + |
10 | +typedef struct arbre{ | ||
11 | + char val; | ||
12 | + struct arbre *suite[TAILLE]; | ||
13 | + bool finmot; //1 si fin de mot | ||
14 | +}Arbre; | ||
15 | + | ||
16 | +typedef struct dico { | ||
17 | + Arbre *alpha[TAILLE]; | ||
18 | +}Dico; | ||
13 | 19 | ||
14 | 20 | ||
15 | 21 | ||
@@ -31,9 +37,20 @@ bool est_vide(struct arbre *arbre) | @@ -31,9 +37,20 @@ bool est_vide(struct arbre *arbre) | ||
31 | return arbre==NULL; | 37 | return arbre==NULL; |
32 | } | 38 | } |
33 | 39 | ||
34 | -bool fin_de_mot(struct arbre *arbre) | ||
35 | -{ | ||
36 | - return arbre->finmot; | 40 | +bool mot_existe(struct arbre *monarbre,char *mot,int i){ |
41 | + if (monarbre==NULL){ | ||
42 | +// printf("false\n"); | ||
43 | + return false; | ||
44 | + } | ||
45 | +// printf("%c \n",(monarbre->val)); | ||
46 | + if (mot[i+1]=='\0'){ | ||
47 | +// printf("finmot\n"); | ||
48 | + return monarbre->finmot; | ||
49 | + } | ||
50 | + else { | ||
51 | + i++; | ||
52 | + mot_existe(monarbre->suite[calculcase(mot[i])],mot,i); | ||
53 | + } | ||
37 | } | 54 | } |
38 | 55 | ||
39 | void ini_dico(struct dico *pt_dico) | 56 | void ini_dico(struct dico *pt_dico) |
@@ -45,7 +62,7 @@ void ini_dico(struct dico *pt_dico) | @@ -45,7 +62,7 @@ void ini_dico(struct dico *pt_dico) | ||
45 | } | 62 | } |
46 | void creation_arbre(Arbre **ppt_arbre,char c) | 63 | void creation_arbre(Arbre **ppt_arbre,char c) |
47 | { | 64 | { |
48 | - printf("creation arbre\n"); | 65 | +// printf("creation arbre\n"); |
49 | Arbre *monarbre=malloc(sizeof(struct arbre)); | 66 | Arbre *monarbre=malloc(sizeof(struct arbre)); |
50 | monarbre->val=c; | 67 | monarbre->val=c; |
51 | monarbre->finmot=false; | 68 | monarbre->finmot=false; |
@@ -59,15 +76,16 @@ void creation_arbre(Arbre **ppt_arbre,char c) | @@ -59,15 +76,16 @@ void creation_arbre(Arbre **ppt_arbre,char c) | ||
59 | void ajout_mot(struct arbre **arbrecourant,char *mot,int i) | 76 | void ajout_mot(struct arbre **arbrecourant,char *mot,int i) |
60 | { | 77 | { |
61 | 78 | ||
62 | - printf("%s:length:%zu \n",mot,strlen(mot)); | 79 | +// printf("%s:length:%zu \n",mot,strlen(mot)); |
63 | 80 | ||
64 | if(mot[i]!='\0'){ | 81 | if(mot[i]!='\0'){ |
65 | - printf("\nlettre:%c; i=%d\n",mot[i],i); | 82 | +// printf("\nlettre:%c; i=%d\n",mot[i],i); |
66 | if (*arbrecourant==NULL){ | 83 | if (*arbrecourant==NULL){ |
67 | creation_arbre(arbrecourant,mot[i]); | 84 | creation_arbre(arbrecourant,mot[i]); |
68 | } | 85 | } |
69 | i++; | 86 | i++; |
70 | - if(mot[i]!='\0'){ | 87 | + if(mot[i+1]=='\0'){ |
88 | + printf("findemot\n"); | ||
71 | (*arbrecourant)->finmot=true; | 89 | (*arbrecourant)->finmot=true; |
72 | } | 90 | } |
73 | ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i); | 91 | ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i); |
@@ -79,8 +97,8 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | @@ -79,8 +97,8 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | ||
79 | { char mot[20]; | 97 | { char mot[20]; |
80 | ini_dico(*ppt_dico); | 98 | ini_dico(*ppt_dico); |
81 | while (fscanf(fp, "%s", mot)==1){ | 99 | while (fscanf(fp, "%s", mot)==1){ |
82 | - printf("mot:%s\n\n\n",mot); | ||
83 | - printf("\nlettre:%c\n",mot[0]); | 100 | +// printf("mot:%s\n\n\n",mot); |
101 | +// printf("\nlettre:%c\n",mot[0]); | ||
84 | ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0); | 102 | ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0); |
85 | } | 103 | } |
86 | return ; | 104 | return ; |
@@ -137,16 +155,21 @@ void affiche_dico(struct dico *dico) | @@ -137,16 +155,21 @@ void affiche_dico(struct dico *dico) | ||
137 | } | 155 | } |
138 | 156 | ||
139 | 157 | ||
140 | -// int main (){ | ||
141 | -// FILE* fp = fopen("words-no-accents","r"); //amelioration entrée | ||
142 | -// if(fp == NULL){ return EXIT_FAILURE;} //File is not readable | ||
143 | -// | ||
144 | -// struct dico *mondico=malloc(sizeof(struct dico)); | ||
145 | -// charger_arbre(fp,&mondico); | ||
146 | -// affiche_dico(mondico); | ||
147 | -// | ||
148 | -// | ||
149 | -// free_dico(mondico); | ||
150 | -// fclose(fp); | ||
151 | -// return 0; | ||
152 | -// } | 158 | +int main (){ |
159 | + FILE* fp = fopen("words-no-accents","r"); //amelioration entrée | ||
160 | + if(fp == NULL){ return EXIT_FAILURE;} //File is not readable | ||
161 | + | ||
162 | + struct dico *mondico=malloc(sizeof(struct dico)); | ||
163 | + charger_arbre(fp,&mondico); | ||
164 | + affiche_dico(mondico); | ||
165 | + char mot[]="zombie"; | ||
166 | + char mot2[]="zombbbiiee"; | ||
167 | +// mot_existe(mondico->alpha[calculcase(mot[0])],mot,0); | ||
168 | +// mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0); | ||
169 | + | ||
170 | + printf("1:%d \n",mot_existe(mondico->alpha[calculcase(mot[0])],mot,0)); | ||
171 | + printf("3:%d \n",mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0)); | ||
172 | + free_dico(mondico); | ||
173 | + fclose(fp); | ||
174 | + return 0; | ||
175 | + } |