Blame view

arbre.c 4.83 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
34
35
36
37
38
39
  }
  
  bool est_vide(struct arbre *arbre)
  {
  	return arbre==NULL;
  }
  
c62798e8   rsimonin   ajout fonction mo...
40
41
42
43
44
45
46
47
48
49
50
51
  bool mot_existe(struct arbre *monarbre,char *mot,int i){
      if (monarbre==NULL){
  //         printf("false\n");
          return false;
      }
  //     printf("%c \n",(monarbre->val));
      if (mot[i+1]=='\0'){
  //         printf("finmot\n");
          return monarbre->finmot;
      }
      else {
          i++;
46b621e7   rsimonin   chargementdu deux...
52
53
          return mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
          
c62798e8   rsimonin   ajout fonction mo...
54
      }
5e43fa77   rsSimonin   arbre.c
55
  }
5e43fa77   rsSimonin   arbre.c
56
  
38be3329   rsSimonin   makefile et main
57
58
  void ini_dico(struct dico *pt_dico)
  {
fc2a89b1   rsimonin   con
59
      printf("ini_dico\n");
7d2c7bff   rsSimonin   implementation de...
60
      for(int i=0;i<TAILLE;i++){
7d0a6328   rsimonin   modif creation
61
          pt_dico->alpha[i]=NULL;        
d6bcca7a   rsSimonin   initialisation
62
63
      }
  }
38be3329   rsSimonin   makefile et main
64
65
  void creation_arbre(Arbre **ppt_arbre,char c)
  {
c62798e8   rsimonin   ajout fonction mo...
66
  //     printf("creation arbre\n");
7d2c7bff   rsSimonin   implementation de...
67
68
69
70
71
72
73
74
75
      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
76
  
38be3329   rsSimonin   makefile et main
77
78
  void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
  {
6ab96f9e   rsSimonin   tout marche
79
  
c62798e8   rsimonin   ajout fonction mo...
80
  //     printf("%s:length:%zu \n",mot,strlen(mot));
7d0a6328   rsimonin   modif creation
81
      
6ab96f9e   rsSimonin   tout marche
82
      if(mot[i]!='\0'){
c62798e8   rsimonin   ajout fonction mo...
83
  //         printf("\nlettre:%c; i=%d\n",mot[i],i);
6ab96f9e   rsSimonin   tout marche
84
          if (*arbrecourant==NULL){
7d2c7bff   rsSimonin   implementation de...
85
              creation_arbre(arbrecourant,mot[i]);
2e977567   rsSimonin   2 commit
86
          }
2e977567   rsSimonin   2 commit
87
          i++;
c62798e8   rsimonin   ajout fonction mo...
88
          if(mot[i+1]=='\0'){
46b621e7   rsimonin   chargementdu deux...
89
  //             printf("mot:%s  %d\n\n\n",mot,i);
38be3329   rsSimonin   makefile et main
90
91
              (*arbrecourant)->finmot=true;
          }
46b621e7   rsimonin   chargementdu deux...
92
          else ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
2e977567   rsSimonin   2 commit
93
      }
fc2a89b1   rsimonin   con
94
  
2e977567   rsSimonin   2 commit
95
96
  }
  
46b621e7   rsimonin   chargementdu deux...
97
  void charger_dico(FILE *fp, struct dico **ppt_dico)
2e977567   rsSimonin   2 commit
98
  {   char mot[20];
41bf8d90   rsSimonin   correction de bug
99
      ini_dico(*ppt_dico);
fc2a89b1   rsimonin   con
100
      while (fscanf(fp, "%s", mot)==1){
c62798e8   rsimonin   ajout fonction mo...
101
102
  //         printf("mot:%s\n\n\n",mot);
  //         printf("\nlettre:%c\n",mot[0]);
6ab96f9e   rsSimonin   tout marche
103
          ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
2e977567   rsSimonin   2 commit
104
105
      }
      return ;
2e977567   rsSimonin   2 commit
106
  }
d6bcca7a   rsSimonin   initialisation
107
  
41bf8d90   rsSimonin   correction de bug
108
  
38be3329   rsSimonin   makefile et main
109
110
  void free_arbre(struct arbre *pt_arbre)
  {
41bf8d90   rsSimonin   correction de bug
111
112
113
     if (pt_arbre==NULL){
          return ;
      }
7d2c7bff   rsSimonin   implementation de...
114
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
115
          free_arbre((pt_arbre->suite[i]));
41bf8d90   rsSimonin   correction de bug
116
      }
41bf8d90   rsSimonin   correction de bug
117
      free(pt_arbre);
075f46e2   rsimonin   affichage et corr...
118
  }
d6bcca7a   rsSimonin   initialisation
119
  
38be3329   rsSimonin   makefile et main
120
121
  void free_dico(struct dico *pt_dico)
  {
41bf8d90   rsSimonin   correction de bug
122
     if (pt_dico==NULL){
075f46e2   rsimonin   affichage et corr...
123
124
          return ;
      }
6ab96f9e   rsSimonin   tout marche
125
    
7d2c7bff   rsSimonin   implementation de...
126
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
127
          free_arbre((pt_dico->alpha[i]));
075f46e2   rsimonin   affichage et corr...
128
      }
41bf8d90   rsSimonin   correction de bug
129
  
6ab96f9e   rsSimonin   tout marche
130
      free(pt_dico);
075f46e2   rsimonin   affichage et corr...
131
132
  }
  
38be3329   rsSimonin   makefile et main
133
134
  void affiche_arbre(struct arbre *arbre)
  {
075f46e2   rsimonin   affichage et corr...
135
136
137
      if(arbre==NULL){
          return ;
      }
4a1bc8c3   rsSimonin   affichage free et...
138
      printf("%c:",arbre->val);
7d2c7bff   rsSimonin   implementation de...
139
      for(int i=0;i<TAILLE;i++){
075f46e2   rsimonin   affichage et corr...
140
141
142
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
143
  
38be3329   rsSimonin   makefile et main
144
145
  void affiche_dico(struct dico *dico)
  {
41bf8d90   rsSimonin   correction de bug
146
147
148
149
      if(dico==NULL){
          return ;
      }
      printf("---------------------------------------\n");
7d2c7bff   rsSimonin   implementation de...
150
      for(int i=0;i<TAILLE;i++){
fc2a89b1   rsimonin   con
151
          printf("%d:",i);
41bf8d90   rsSimonin   correction de bug
152
153
154
155
156
157
          affiche_arbre(dico->alpha[i]);
          printf("\n");
      }
     
  }
  
d6bcca7a   rsSimonin   initialisation
158
  
46b621e7   rsimonin   chargementdu deux...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  void analyse_fichier(FILE *fp,Dico *pt_dico, int *res[2]){
      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]);
          if(mot_existe(pt_dico->alpha[calculcase(mot[0])],mot,0)){
              nbmot_t++;
          }
          else nbmot_f++;
      }
      *res[0]=nbmot_t;
      *res[1]=nbmot_f;    
  }
  
  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
c62798e8   rsimonin   ajout fonction mo...
188
189
    	
      struct dico *mondico=malloc(sizeof(struct dico));
46b621e7   rsimonin   chargementdu deux...
190
      charger_dico(dico,&mondico);
c62798e8   rsimonin   ajout fonction mo...
191
      affiche_dico(mondico);
46b621e7   rsimonin   chargementdu deux...
192
      fclose(dico); 
c62798e8   rsimonin   ajout fonction mo...
193
194
195
196
197
      char mot[]="zombie";
      char mot2[]="zombbbiiee";
  //     mot_existe(mondico->alpha[calculcase(mot[0])],mot,0);
  //     mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0);
      
46b621e7   rsimonin   chargementdu deux...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
      printf("%s:%d \n",mot,mot_existe(mondico->alpha[calculcase(mot[0])],mot,0));
      printf("%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");
  //         int *res[2];
  //         analyse_fichier(fichier_utilisateur,mondico,res);
  //         printf("juste:%d \n",*res[0]);
  //         printf("faux:%d \n",*res[1]);
  //     }
      
      printf("fin programme \n");
      
      
c62798e8   rsimonin   ajout fonction mo...
219
      free_dico(mondico);
46b621e7   rsimonin   chargementdu deux...
220
221
222
223
      if (fichier_utilisateur!=NULL){
          fclose(fichier_utilisateur);
      }
             
c62798e8   rsimonin   ajout fonction mo...
224
225
      return 0;
      }