Blame view

arbre.c 2.91 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
  
7a7157b0   rsimonin   modification crea...
35
  void cons_dico(struct dico **ppt_dico,char val){
41bf8d90   rsSimonin   correction de bug
36
37
38
39
      Arbre *monarbre=malloc(sizeof(struct arbre));
      monarbre->val=val;
      monarbre->finmot=false;
      
2e977567   rsSimonin   2 commit
40
41
42
      for(int i=0;i<26;i++){
          monarbre->suite[i]=NULL;        
      }
d6bcca7a   rsSimonin   initialisation
43
  
7a7157b0   rsimonin   modification crea...
44
      (*ppt_dico)->alpha[val-'a']=monarbre; 
41bf8d90   rsSimonin   correction de bug
45
  }
d6bcca7a   rsSimonin   initialisation
46
47
  
  void ini_dico(struct dico * pt_dico){
41bf8d90   rsSimonin   correction de bug
48
      printf("ini_dico");
d6bcca7a   rsSimonin   initialisation
49
      for(int i=0;i<26;i++){
41bf8d90   rsSimonin   correction de bug
50
          cons_dico(&pt_dico,'a'+i);        
d6bcca7a   rsSimonin   initialisation
51
52
      }
  }
2e977567   rsSimonin   2 commit
53
54
    
    
7a7157b0   rsimonin   modification crea...
55
56
57
58
59
60
  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");
41bf8d90   rsSimonin   correction de bug
61
62
63
64
65
66
              Arbre *monarbre=malloc(sizeof(struct arbre));
              monarbre->val=mot[i];
              monarbre->finmot=false;
              for(int i=0;i<26;i++){
                  monarbre->suite[i]=NULL;     
              }
7a7157b0   rsimonin   modification crea...
67
68
              pt_arbre=&monarbre;
              printf("%c\n",(*pt_arbre)->val);
41bf8d90   rsSimonin   correction de bug
69
                  
2e977567   rsSimonin   2 commit
70
71
          }
          i++;
41bf8d90   rsSimonin   correction de bug
72
          ajout_mot(pt_arbre,mot,i);
2e977567   rsSimonin   2 commit
73
      }
7a7157b0   rsimonin   modification crea...
74
75
  //     (*pt_arbre)->finmot=true;
  //     printf("true\n");
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
          printf("\nmot:%s\n",mot); 
7a7157b0   rsimonin   modification crea...
83
84
          printf("%c\n",(*ppt_dico)->alpha[mot[0]-'a']->val);
          ajout_mot(&((*ppt_dico)->alpha[mot[0]-'a']),mot,0);
2e977567   rsSimonin   2 commit
85
86
      }
      return ;
2e977567   rsSimonin   2 commit
87
  }
d6bcca7a   rsSimonin   initialisation
88
  
41bf8d90   rsSimonin   correction de bug
89
90
91
92
93
94
95
96
97
  
  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
98
  
075f46e2   rsimonin   affichage et corr...
99
  }
d6bcca7a   rsSimonin   initialisation
100
  
41bf8d90   rsSimonin   correction de bug
101
102
  void free_dico(struct dico *pt_dico){
     if (pt_dico==NULL){
075f46e2   rsimonin   affichage et corr...
103
104
105
          return ;
      }
      for(int i=0;i<26;i++){
41bf8d90   rsSimonin   correction de bug
106
          free_arbre(pt_dico->alpha[i]);
7a7157b0   rsimonin   modification crea...
107
          free(pt_dico->alpha[i]);
075f46e2   rsimonin   affichage et corr...
108
      }
41bf8d90   rsSimonin   correction de bug
109
110
      free(pt_dico);
  
075f46e2   rsimonin   affichage et corr...
111
112
113
114
115
116
  }
  
  void affiche_arbre(struct arbre *arbre){
      if(arbre==NULL){
          return ;
      }
7a7157b0   rsimonin   modification crea...
117
      printf("%c\n",arbre->val);
075f46e2   rsimonin   affichage et corr...
118
119
120
121
      for(int i=0;i<26;i++){
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
122
  
41bf8d90   rsSimonin   correction de bug
123
124
125
126
127
128
  void affiche_dico(struct dico *dico){
      if(dico==NULL){
          return ;
      }
      printf("---------------------------------------\n");
      for(int i=0;i<26;i++){
7a7157b0   rsimonin   modification crea...
129
          printf("%dmot:",i);
41bf8d90   rsSimonin   correction de bug
130
131
132
133
134
135
          affiche_arbre(dico->alpha[i]);
          printf("\n");
      }
     
  }
  
d6bcca7a   rsSimonin   initialisation
136
  
5e43fa77   rsSimonin   arbre.c
137
  int main (){
41bf8d90   rsSimonin   correction de bug
138
139
140
141
142
143
      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
144
145
      
      
41bf8d90   rsSimonin   correction de bug
146
147
      free(mondico);
      fclose(fp);    
5e43fa77   rsSimonin   arbre.c
148
149
      return 0;
  }