Blame view

arbre.c 4.95 KB
c62798e8   rsimonin   ajout fonction mo...
1
2
3
4
5
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  #include <string.h>
  #define TAILLE 27
5e43fa77   rsSimonin   arbre.c
6
  
c62798e8   rsimonin   ajout fonction mo...
7
  //#include "arbre.h"
5e43fa77   rsSimonin   arbre.c
8
  
c62798e8   rsimonin   ajout fonction mo...
9
10
11
12
13
14
15
16
17
18
  
  typedef struct arbre{
      char val;
      struct arbre *suite[TAILLE];
      bool finmot; //1 si fin de mot
  }Arbre;
   
  typedef struct dico {
      Arbre *alpha[TAILLE];
  }Dico;
5e43fa77   rsSimonin   arbre.c
19
  
5e43fa77   rsSimonin   arbre.c
20
21
  
  
38be3329   rsSimonin   makefile et main
22
23
  int calculcase(char c)
  {
7d2c7bff   rsSimonin   implementation de...
24
25
26
27
28
29
30
      if(c==39){
          return 26;
      }
      else if(c<97){
          return c-'A';
      }
      else {
6ab96f9e   rsSimonin   tout marche
31
      return c-'a';
7d2c7bff   rsSimonin   implementation de...
32
      }
5e43fa77   rsSimonin   arbre.c
33
  }
d6eeddaa   rsimonin   toutes les foncti...
34
      
c62798e8   rsimonin   ajout fonction mo...
35
36
  bool mot_existe(struct arbre *monarbre,char *mot,int i){
      if (monarbre==NULL){
20cb4a11   rsimonin   modif_affichage
37
          //printf("%s n'est pas dans le dictionnaire\n",mot);
c62798e8   rsimonin   ajout fonction mo...
38
39
          return false;
      }
c62798e8   rsimonin   ajout fonction mo...
40
      if (mot[i+1]=='\0'){
20cb4a11   rsimonin   modif_affichage
41
          //printf("%s %d\n",mot, monarbre->finmot);
c62798e8   rsimonin   ajout fonction mo...
42
43
          return monarbre->finmot;
      }
d6eeddaa   rsimonin   toutes les foncti...
44
45
      i++;
      return mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
5e43fa77   rsSimonin   arbre.c
46
  }
5e43fa77   rsSimonin   arbre.c
47
  
38be3329   rsSimonin   makefile et main
48
49
  void ini_dico(struct dico *pt_dico)
  {
20cb4a11   rsimonin   modif_affichage
50
      //printf("Initialisation du dictionnaire\n");
7d2c7bff   rsSimonin   implementation de...
51
      for(int i=0;i<TAILLE;i++){
7d0a6328   rsimonin   modif creation
52
          pt_dico->alpha[i]=NULL;        
d6bcca7a   rsSimonin   initialisation
53
54
      }
  }
38be3329   rsSimonin   makefile et main
55
56
  void creation_arbre(Arbre **ppt_arbre,char c)
  {
c62798e8   rsimonin   ajout fonction mo...
57
  //     printf("creation arbre\n");
7d2c7bff   rsSimonin   implementation de...
58
59
60
61
62
63
64
65
66
      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
67
  
38be3329   rsSimonin   makefile et main
68
69
  void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
  {
6ab96f9e   rsSimonin   tout marche
70
  
c62798e8   rsimonin   ajout fonction mo...
71
  //     printf("%s:length:%zu \n",mot,strlen(mot));
7d0a6328   rsimonin   modif creation
72
      
6ab96f9e   rsSimonin   tout marche
73
      if(mot[i]!='\0'){
c62798e8   rsimonin   ajout fonction mo...
74
  //         printf("\nlettre:%c; i=%d\n",mot[i],i);
6ab96f9e   rsSimonin   tout marche
75
          if (*arbrecourant==NULL){
7d2c7bff   rsSimonin   implementation de...
76
              creation_arbre(arbrecourant,mot[i]);
2e977567   rsSimonin   2 commit
77
          }
c62798e8   rsimonin   ajout fonction mo...
78
          if(mot[i+1]=='\0'){
0164603a   rsimonin   affichage
79
              //printf("mot:%s %c\n",mot,mot[i]);
38be3329   rsSimonin   makefile et main
80
81
              (*arbrecourant)->finmot=true;
          }
d6eeddaa   rsimonin   toutes les foncti...
82
83
          i++;
          ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
2e977567   rsSimonin   2 commit
84
      }
d6eeddaa   rsimonin   toutes les foncti...
85
      else return ;
fc2a89b1   rsimonin   con
86
  
2e977567   rsSimonin   2 commit
87
88
  }
  
46b621e7   rsimonin   chargementdu deux...
89
  void charger_dico(FILE *fp, struct dico **ppt_dico)
2e977567   rsSimonin   2 commit
90
  {   char mot[20];
41bf8d90   rsSimonin   correction de bug
91
      ini_dico(*ppt_dico);
fc2a89b1   rsimonin   con
92
      while (fscanf(fp, "%s", mot)==1){
c62798e8   rsimonin   ajout fonction mo...
93
94
  //         printf("mot:%s\n\n\n",mot);
  //         printf("\nlettre:%c\n",mot[0]);
6ab96f9e   rsSimonin   tout marche
95
          ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
2e977567   rsSimonin   2 commit
96
97
      }
      return ;
2e977567   rsSimonin   2 commit
98
  }
d6bcca7a   rsSimonin   initialisation
99
  
41bf8d90   rsSimonin   correction de bug
100
  
38be3329   rsSimonin   makefile et main
101
102
  void free_arbre(struct arbre *pt_arbre)
  {
41bf8d90   rsSimonin   correction de bug
103
104
105
     if (pt_arbre==NULL){
          return ;
      }
7d2c7bff   rsSimonin   implementation de...
106
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
107
          free_arbre((pt_arbre->suite[i]));
41bf8d90   rsSimonin   correction de bug
108
      }
41bf8d90   rsSimonin   correction de bug
109
      free(pt_arbre);
075f46e2   rsimonin   affichage et corr...
110
  }
d6bcca7a   rsSimonin   initialisation
111
  
38be3329   rsSimonin   makefile et main
112
113
  void free_dico(struct dico *pt_dico)
  {
41bf8d90   rsSimonin   correction de bug
114
     if (pt_dico==NULL){
075f46e2   rsimonin   affichage et corr...
115
116
          return ;
      }
6ab96f9e   rsSimonin   tout marche
117
    
7d2c7bff   rsSimonin   implementation de...
118
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
119
          free_arbre((pt_dico->alpha[i]));
075f46e2   rsimonin   affichage et corr...
120
      }
41bf8d90   rsSimonin   correction de bug
121
  
6ab96f9e   rsSimonin   tout marche
122
      free(pt_dico);
075f46e2   rsimonin   affichage et corr...
123
124
  }
  
38be3329   rsSimonin   makefile et main
125
126
  void affiche_arbre(struct arbre *arbre)
  {
075f46e2   rsimonin   affichage et corr...
127
128
129
      if(arbre==NULL){
          return ;
      }
4a1bc8c3   rsSimonin   affichage free et...
130
      printf("%c:",arbre->val);
7d2c7bff   rsSimonin   implementation de...
131
      for(int i=0;i<TAILLE;i++){
075f46e2   rsimonin   affichage et corr...
132
133
134
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
135
  
38be3329   rsSimonin   makefile et main
136
137
  void affiche_dico(struct dico *dico)
  {
41bf8d90   rsSimonin   correction de bug
138
139
140
141
      if(dico==NULL){
          return ;
      }
      printf("---------------------------------------\n");
7d2c7bff   rsSimonin   implementation de...
142
      for(int i=0;i<TAILLE;i++){
fc2a89b1   rsimonin   con
143
          printf("%d:",i);
41bf8d90   rsSimonin   correction de bug
144
145
146
147
148
149
          affiche_arbre(dico->alpha[i]);
          printf("\n");
      }
     
  }
  
d6bcca7a   rsSimonin   initialisation
150
  
d6eeddaa   rsimonin   toutes les foncti...
151
  void analyse_fichier(FILE *fp,Dico *pt_dico, int *nb_t, int *nb_f){
46b621e7   rsimonin   chargementdu deux...
152
153
154
155
156
157
      int nbmot_t=0;
      int nbmot_f=0;
      char mot[20];
      while (fscanf(fp, "%s", mot)==1){
  //         printf("mot:%s\n\n\n",mot);
  //         printf("\nlettre:%c\n",mot[0]);
d6eeddaa   rsimonin   toutes les foncti...
158
          if(mot_existe(pt_dico->alpha[calculcase(mot[0])],mot,0)==1){
46b621e7   rsimonin   chargementdu deux...
159
160
161
162
              nbmot_t++;
          }
          else nbmot_f++;
      }
d6eeddaa   rsimonin   toutes les foncti...
163
164
      *nb_t=nbmot_t;
      *nb_f=nbmot_f;    
46b621e7   rsimonin   chargementdu deux...
165
166
  }
  
0164603a   rsimonin   affichage
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  // int main (int argc,char *argv[]){
  //     FILE* dico = NULL;
  //     if(argc >1){
  //         printf("chargement de votre dictionnaire \n");
  //         dico=fopen(argv[1],"r"); 
  //     }
  // //     else dico=fopen("words-no-accents","r");
  //     
  //   	if(dico == NULL){
  // //         return EXIT_FAILURE;
  //         printf("chargement du dictionnaire par default");
  //         dico=fopen("words-no-accents","r");
  //     } //File is not readable
  //   	
  //     struct dico *mondico=malloc(sizeof(struct dico));
  //     charger_dico(dico,&mondico);
  // //     affiche_dico(mondico);
  //     fclose(dico); 
  //     char mot[]="zombie";
  //     char mot2[]="chaine";
  // //     mot_existe(mondico->alpha[calculcase('r')],"rerere",0);
  //      //mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0);
  //     
  //     printf("1: %s:%d \n",mot,mot_existe(mondico->alpha[calculcase(mot[0])],mot,0));
  //     printf("2: %s:%d \n",mot2,mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0));
  //     
  //     FILE* fichier_utilisateur = NULL;
  //     if(argc >2){
  //         printf("Analyse de votre fichier \n");
  //         fichier_utilisateur=fopen(argv[2],"r"); 
  //         printf("Fin Analyse de votre fichier \n");
  //     }
  //     
  //      if (fichier_utilisateur!=NULL){
  //         printf("analyse en cours\n");
  //          int juste=0;
  //          int faux=0;
  //          analyse_fichier(fichier_utilisateur,mondico,&juste,&faux);
  //          printf("juste:%d \n",juste);
  //          printf("faux:%d \n",faux);
  //      }
  //     
  //     printf("fin programme \n");
  //     free_dico(mondico);
  //     if (fichier_utilisateur!=NULL){
  //         fclose(fichier_utilisateur);
  //     }
  //            
  //     return 0;
  // }