Commit 7d0a632830f52a702d0ad3ffa1f2c20ad721ae48
1 parent
fc2a89b1
modif creation
Showing
1 changed file
with
41 additions
and
26 deletions
Show diff stats
arbre.c
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <stdbool.h> | 3 | #include <stdbool.h> |
4 | +#include <string.h> | ||
4 | 5 | ||
5 | 6 | ||
6 | typedef struct arbre{ | 7 | typedef struct arbre{ |
@@ -15,7 +16,9 @@ typedef struct dico { | @@ -15,7 +16,9 @@ typedef struct dico { | ||
15 | 16 | ||
16 | 17 | ||
17 | 18 | ||
18 | - | 19 | +int calculcase(char c){ |
20 | + return 'c'-'a'; | ||
21 | +} | ||
19 | 22 | ||
20 | void arbre_vide(struct arbre ** pt_arbre) | 23 | void arbre_vide(struct arbre ** pt_arbre) |
21 | { | 24 | { |
@@ -32,31 +35,32 @@ bool fin_de_mot(struct arbre *arbre) | @@ -32,31 +35,32 @@ bool fin_de_mot(struct arbre *arbre) | ||
32 | return arbre->finmot; | 35 | return arbre->finmot; |
33 | } | 36 | } |
34 | 37 | ||
35 | -void cons_dico(struct dico **ppt_dico,char val){ | ||
36 | - Arbre *monarbre=malloc(sizeof(struct arbre)); | ||
37 | - monarbre->val=val; | ||
38 | - monarbre->finmot=false; | ||
39 | - | ||
40 | - for(int i=0;i<26;i++){ | ||
41 | - monarbre->suite[i]=NULL; | ||
42 | - } | ||
43 | - | ||
44 | - (*ppt_dico)->alpha[val-'a']=monarbre; | ||
45 | -} | ||
46 | - | ||
47 | void ini_dico(struct dico * pt_dico){ | 38 | void ini_dico(struct dico * pt_dico){ |
48 | printf("ini_dico\n"); | 39 | printf("ini_dico\n"); |
49 | for(int i=0;i<26;i++){ | 40 | for(int i=0;i<26;i++){ |
50 | - cons_dico(&pt_dico,'a'+i); | 41 | + pt_dico->alpha[i]=NULL; |
51 | } | 42 | } |
52 | } | 43 | } |
53 | - | ||
54 | - | ||
55 | -void ajout_mot(struct arbre **pt_arbre,char *mot, int i){ | ||
56 | - printf("%d",i); | ||
57 | - printf("%c",mot[i]); | ||
58 | - while(mot[i]!='\0'){ | ||
59 | - if (*pt_arbre==NULL){ | 44 | + |
45 | + | ||
46 | +void ajout_mot(struct arbre *arbrecourant,char *mot,int i){ | ||
47 | +// Arbre *arbrecourant=malloc(sizeof(struct arbre)); | ||
48 | +// if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ | ||
49 | +// arbrecourant->val=mot[0]; | ||
50 | +// arbrecourant->finmot=false; | ||
51 | +// for(int j=0;j<26;j++){ | ||
52 | +// arbrecourant->suite[j]=NULL; | ||
53 | +// } | ||
54 | +// } | ||
55 | +// arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; | ||
56 | +// | ||
57 | +// int i=1; | ||
58 | + printf("%s:length:%d\n",mot,strlen(mot)); | ||
59 | + | ||
60 | + while(mot[i]!='\0'){ | ||
61 | + printf("\nlettre:%c; i=%d\n",mot[i],i); | ||
62 | + if (arbrecourant->suite[calculcase(mot[i])]==NULL){ | ||
63 | + | ||
60 | printf("creation arbre\n"); | 64 | printf("creation arbre\n"); |
61 | Arbre *monarbre=malloc(sizeof(struct arbre)); | 65 | Arbre *monarbre=malloc(sizeof(struct arbre)); |
62 | monarbre->val=mot[i]; | 66 | monarbre->val=mot[i]; |
@@ -64,12 +68,13 @@ void ajout_mot(struct arbre **pt_arbre,char *mot, int i){ | @@ -64,12 +68,13 @@ void ajout_mot(struct arbre **pt_arbre,char *mot, int i){ | ||
64 | for(int j=0;j<26;j++){ | 68 | for(int j=0;j<26;j++){ |
65 | monarbre->suite[j]=NULL; | 69 | monarbre->suite[j]=NULL; |
66 | } | 70 | } |
67 | - pt_arbre=&monarbre; | ||
68 | - printf("%c\n",(*pt_arbre)->val); | 71 | + arbrecourant=monarbre; |
72 | +// printf("%c\n",(*pt_arbre)->val); | ||
69 | 73 | ||
70 | } | 74 | } |
75 | + arbrecourant=arbrecourant->suite[calculcase(mot[i])]; | ||
71 | i++; | 76 | i++; |
72 | - ajout_mot(&((*pt_arbre)->suite[mot[i]-'a']),mot,i); | 77 | + ajout_mot(arbrecourant->suite[calculcase(mot[i])],mot,i); |
73 | } | 78 | } |
74 | 79 | ||
75 | } | 80 | } |
@@ -78,9 +83,19 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | @@ -78,9 +83,19 @@ void charger_arbre(FILE *fp, struct dico **ppt_dico) | ||
78 | { char mot[20]; | 83 | { char mot[20]; |
79 | ini_dico(*ppt_dico); | 84 | ini_dico(*ppt_dico); |
80 | while (fscanf(fp, "%s", mot)==1){ | 85 | while (fscanf(fp, "%s", mot)==1){ |
81 | - int i=0; | ||
82 | printf("mot:%s\n",mot); | 86 | printf("mot:%s\n",mot); |
83 | - ajout_mot(&((*ppt_dico)->alpha[mot[0]-'a']),mot,i); | 87 | + |
88 | + Arbre *arbrecourant=malloc(sizeof(struct arbre)); | ||
89 | + if( (*ppt_dico)->alpha[calculcase(mot[0])]==NULL){ | ||
90 | + arbrecourant->val=mot[0]; | ||
91 | + arbrecourant->finmot=false; | ||
92 | + for(int j=0;j<26;j++){ | ||
93 | + arbrecourant->suite[j]=NULL; | ||
94 | + } | ||
95 | + } | ||
96 | + arbrecourant=(*ppt_dico)->alpha[calculcase(mot[0])]; | ||
97 | + int i=1; | ||
98 | + ajout_mot(&arbrecourant,mot,i); | ||
84 | } | 99 | } |
85 | return ; | 100 | return ; |
86 | } | 101 | } |