Blame view

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