Blame view

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