Blame view

arbre.c 3.54 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
52
53
  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++;
          mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
      }
5e43fa77   rsSimonin   arbre.c
54
  }
5e43fa77   rsSimonin   arbre.c
55
  
38be3329   rsSimonin   makefile et main
56
57
  void ini_dico(struct dico *pt_dico)
  {
fc2a89b1   rsimonin   con
58
      printf("ini_dico\n");
7d2c7bff   rsSimonin   implementation de...
59
      for(int i=0;i<TAILLE;i++){
7d0a6328   rsimonin   modif creation
60
          pt_dico->alpha[i]=NULL;        
d6bcca7a   rsSimonin   initialisation
61
62
      }
  }
38be3329   rsSimonin   makefile et main
63
64
  void creation_arbre(Arbre **ppt_arbre,char c)
  {
c62798e8   rsimonin   ajout fonction mo...
65
  //     printf("creation arbre\n");
7d2c7bff   rsSimonin   implementation de...
66
67
68
69
70
71
72
73
74
      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
75
  
38be3329   rsSimonin   makefile et main
76
77
  void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
  {
6ab96f9e   rsSimonin   tout marche
78
  
c62798e8   rsimonin   ajout fonction mo...
79
  //     printf("%s:length:%zu \n",mot,strlen(mot));
7d0a6328   rsimonin   modif creation
80
      
6ab96f9e   rsSimonin   tout marche
81
      if(mot[i]!='\0'){
c62798e8   rsimonin   ajout fonction mo...
82
  //         printf("\nlettre:%c; i=%d\n",mot[i],i);
6ab96f9e   rsSimonin   tout marche
83
          if (*arbrecourant==NULL){
7d2c7bff   rsSimonin   implementation de...
84
              creation_arbre(arbrecourant,mot[i]);
2e977567   rsSimonin   2 commit
85
          }
2e977567   rsSimonin   2 commit
86
          i++;
c62798e8   rsimonin   ajout fonction mo...
87
88
          if(mot[i+1]=='\0'){
              printf("findemot\n");
38be3329   rsSimonin   makefile et main
89
90
              (*arbrecourant)->finmot=true;
          }
6ab96f9e   rsSimonin   tout marche
91
          ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
2e977567   rsSimonin   2 commit
92
      }
fc2a89b1   rsimonin   con
93
  
2e977567   rsSimonin   2 commit
94
95
  }
  
41bf8d90   rsSimonin   correction de bug
96
  void charger_arbre(FILE *fp, struct dico **ppt_dico)
2e977567   rsSimonin   2 commit
97
  {   char mot[20];
41bf8d90   rsSimonin   correction de bug
98
      ini_dico(*ppt_dico);
fc2a89b1   rsimonin   con
99
      while (fscanf(fp, "%s", mot)==1){
c62798e8   rsimonin   ajout fonction mo...
100
101
  //         printf("mot:%s\n\n\n",mot);
  //         printf("\nlettre:%c\n",mot[0]);
6ab96f9e   rsSimonin   tout marche
102
          ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
2e977567   rsSimonin   2 commit
103
104
      }
      return ;
2e977567   rsSimonin   2 commit
105
  }
d6bcca7a   rsSimonin   initialisation
106
  
41bf8d90   rsSimonin   correction de bug
107
  
38be3329   rsSimonin   makefile et main
108
109
  void free_arbre(struct arbre *pt_arbre)
  {
41bf8d90   rsSimonin   correction de bug
110
111
112
     if (pt_arbre==NULL){
          return ;
      }
7d2c7bff   rsSimonin   implementation de...
113
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
114
          free_arbre((pt_arbre->suite[i]));
41bf8d90   rsSimonin   correction de bug
115
      }
41bf8d90   rsSimonin   correction de bug
116
      free(pt_arbre);
075f46e2   rsimonin   affichage et corr...
117
  }
d6bcca7a   rsSimonin   initialisation
118
  
38be3329   rsSimonin   makefile et main
119
120
  void free_dico(struct dico *pt_dico)
  {
41bf8d90   rsSimonin   correction de bug
121
     if (pt_dico==NULL){
075f46e2   rsimonin   affichage et corr...
122
123
          return ;
      }
6ab96f9e   rsSimonin   tout marche
124
    
7d2c7bff   rsSimonin   implementation de...
125
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
126
          free_arbre((pt_dico->alpha[i]));
075f46e2   rsimonin   affichage et corr...
127
      }
41bf8d90   rsSimonin   correction de bug
128
  
6ab96f9e   rsSimonin   tout marche
129
      free(pt_dico);
075f46e2   rsimonin   affichage et corr...
130
131
  }
  
38be3329   rsSimonin   makefile et main
132
133
  void affiche_arbre(struct arbre *arbre)
  {
075f46e2   rsimonin   affichage et corr...
134
135
136
      if(arbre==NULL){
          return ;
      }
4a1bc8c3   rsSimonin   affichage free et...
137
      printf("%c:",arbre->val);
7d2c7bff   rsSimonin   implementation de...
138
      for(int i=0;i<TAILLE;i++){
075f46e2   rsimonin   affichage et corr...
139
140
141
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
142
  
38be3329   rsSimonin   makefile et main
143
144
  void affiche_dico(struct dico *dico)
  {
41bf8d90   rsSimonin   correction de bug
145
146
147
148
      if(dico==NULL){
          return ;
      }
      printf("---------------------------------------\n");
7d2c7bff   rsSimonin   implementation de...
149
      for(int i=0;i<TAILLE;i++){
fc2a89b1   rsimonin   con
150
          printf("%d:",i);
41bf8d90   rsSimonin   correction de bug
151
152
153
154
155
156
          affiche_arbre(dico->alpha[i]);
          printf("\n");
      }
     
  }
  
d6bcca7a   rsSimonin   initialisation
157
  
c62798e8   rsimonin   ajout fonction mo...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  int main (){
      FILE* fp = fopen("words-no-accents","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);
      char mot[]="zombie";
      char mot2[]="zombbbiiee";
  //     mot_existe(mondico->alpha[calculcase(mot[0])],mot,0);
  //     mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0);
      
      printf("1:%d \n",mot_existe(mondico->alpha[calculcase(mot[0])],mot,0));
      printf("3:%d \n",mot_existe(mondico->alpha[calculcase(mot2[0])],mot2,0));
      free_dico(mondico);
      fclose(fp);    
      return 0;
      }