Blame view

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