Blame view

arbre.c 3.1 KB
c62798e8   rsimonin   ajout fonction mo...
1
2
3
4
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  #include <string.h>
5e43fa77   rsSimonin   arbre.c
5
  
5e43fa77   rsSimonin   arbre.c
6
  
fde8b39d   rsSimonin   fini
7
  #include "arbre.h"
5e43fa77   rsSimonin   arbre.c
8
  
5e43fa77   rsSimonin   arbre.c
9
10
  
  
38be3329   rsSimonin   makefile et main
11
12
  int calculcase(char c)
  {
7d2c7bff   rsSimonin   implementation de...
13
14
15
16
17
18
19
      if(c==39){
          return 26;
      }
      else if(c<97){
          return c-'A';
      }
      else {
6ab96f9e   rsSimonin   tout marche
20
      return c-'a';
7d2c7bff   rsSimonin   implementation de...
21
      }
5e43fa77   rsSimonin   arbre.c
22
  }
d6eeddaa   rsimonin   toutes les foncti...
23
      
c62798e8   rsimonin   ajout fonction mo...
24
25
  bool mot_existe(struct arbre *monarbre,char *mot,int i){
      if (monarbre==NULL){
20cb4a11   rsimonin   modif_affichage
26
          //printf("%s n'est pas dans le dictionnaire\n",mot);
c62798e8   rsimonin   ajout fonction mo...
27
28
          return false;
      }
c62798e8   rsimonin   ajout fonction mo...
29
      if (mot[i+1]=='\0'){
20cb4a11   rsimonin   modif_affichage
30
          //printf("%s %d\n",mot, monarbre->finmot);
c62798e8   rsimonin   ajout fonction mo...
31
32
          return monarbre->finmot;
      }
d6eeddaa   rsimonin   toutes les foncti...
33
34
      i++;
      return mot_existe(monarbre->suite[calculcase(mot[i])],mot,i);
5e43fa77   rsSimonin   arbre.c
35
  }
5e43fa77   rsSimonin   arbre.c
36
  
38be3329   rsSimonin   makefile et main
37
38
  void ini_dico(struct dico *pt_dico)
  {
20cb4a11   rsimonin   modif_affichage
39
      //printf("Initialisation du dictionnaire\n");
7d2c7bff   rsSimonin   implementation de...
40
      for(int i=0;i<TAILLE;i++){
7d0a6328   rsimonin   modif creation
41
          pt_dico->alpha[i]=NULL;        
d6bcca7a   rsSimonin   initialisation
42
43
      }
  }
38be3329   rsSimonin   makefile et main
44
45
  void creation_arbre(Arbre **ppt_arbre,char c)
  {
c62798e8   rsimonin   ajout fonction mo...
46
  //     printf("creation arbre\n");
7d2c7bff   rsSimonin   implementation de...
47
48
49
50
51
52
53
54
55
      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
56
  
38be3329   rsSimonin   makefile et main
57
58
  void ajout_mot(struct arbre **arbrecourant,char *mot,int i)
  {
6ab96f9e   rsSimonin   tout marche
59
  
c62798e8   rsimonin   ajout fonction mo...
60
  //     printf("%s:length:%zu \n",mot,strlen(mot));
7d0a6328   rsimonin   modif creation
61
      
6ab96f9e   rsSimonin   tout marche
62
      if(mot[i]!='\0'){
c62798e8   rsimonin   ajout fonction mo...
63
  //         printf("\nlettre:%c; i=%d\n",mot[i],i);
6ab96f9e   rsSimonin   tout marche
64
          if (*arbrecourant==NULL){
7d2c7bff   rsSimonin   implementation de...
65
              creation_arbre(arbrecourant,mot[i]);
2e977567   rsSimonin   2 commit
66
          }
c62798e8   rsimonin   ajout fonction mo...
67
          if(mot[i+1]=='\0'){
0164603a   rsimonin   affichage
68
              //printf("mot:%s %c\n",mot,mot[i]);
38be3329   rsSimonin   makefile et main
69
70
              (*arbrecourant)->finmot=true;
          }
d6eeddaa   rsimonin   toutes les foncti...
71
72
          i++;
          ajout_mot(&((*arbrecourant)->suite[calculcase(mot[i])]),mot,i);
2e977567   rsSimonin   2 commit
73
      }
d6eeddaa   rsimonin   toutes les foncti...
74
      else return ;
fc2a89b1   rsimonin   con
75
  
2e977567   rsSimonin   2 commit
76
77
  }
  
46b621e7   rsimonin   chargementdu deux...
78
  void charger_dico(FILE *fp, struct dico **ppt_dico)
2e977567   rsSimonin   2 commit
79
  {   char mot[20];
41bf8d90   rsSimonin   correction de bug
80
      ini_dico(*ppt_dico);
fc2a89b1   rsimonin   con
81
      while (fscanf(fp, "%s", mot)==1){
c62798e8   rsimonin   ajout fonction mo...
82
83
  //         printf("mot:%s\n\n\n",mot);
  //         printf("\nlettre:%c\n",mot[0]);
6ab96f9e   rsSimonin   tout marche
84
          ajout_mot(&((*ppt_dico)->alpha[calculcase(mot[0])]),mot,0);
2e977567   rsSimonin   2 commit
85
86
      }
      return ;
2e977567   rsSimonin   2 commit
87
  }
d6bcca7a   rsSimonin   initialisation
88
  
41bf8d90   rsSimonin   correction de bug
89
  
38be3329   rsSimonin   makefile et main
90
91
  void free_arbre(struct arbre *pt_arbre)
  {
41bf8d90   rsSimonin   correction de bug
92
93
94
     if (pt_arbre==NULL){
          return ;
      }
7d2c7bff   rsSimonin   implementation de...
95
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
96
          free_arbre((pt_arbre->suite[i]));
41bf8d90   rsSimonin   correction de bug
97
      }
41bf8d90   rsSimonin   correction de bug
98
      free(pt_arbre);
075f46e2   rsimonin   affichage et corr...
99
  }
d6bcca7a   rsSimonin   initialisation
100
  
38be3329   rsSimonin   makefile et main
101
102
  void free_dico(struct dico *pt_dico)
  {
41bf8d90   rsSimonin   correction de bug
103
     if (pt_dico==NULL){
075f46e2   rsimonin   affichage et corr...
104
105
          return ;
      }
6ab96f9e   rsSimonin   tout marche
106
    
7d2c7bff   rsSimonin   implementation de...
107
      for(int i=0;i<TAILLE;i++){
6ab96f9e   rsSimonin   tout marche
108
          free_arbre((pt_dico->alpha[i]));
075f46e2   rsimonin   affichage et corr...
109
      }
41bf8d90   rsSimonin   correction de bug
110
  
fde8b39d   rsSimonin   fini
111
      //free(pt_dico);
075f46e2   rsimonin   affichage et corr...
112
113
  }
  
38be3329   rsSimonin   makefile et main
114
115
  void affiche_arbre(struct arbre *arbre)
  {
075f46e2   rsimonin   affichage et corr...
116
117
118
      if(arbre==NULL){
          return ;
      }
4a1bc8c3   rsSimonin   affichage free et...
119
      printf("%c:",arbre->val);
7d2c7bff   rsSimonin   implementation de...
120
      for(int i=0;i<TAILLE;i++){
075f46e2   rsimonin   affichage et corr...
121
122
123
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
124
  
38be3329   rsSimonin   makefile et main
125
126
  void affiche_dico(struct dico *dico)
  {
41bf8d90   rsSimonin   correction de bug
127
128
129
130
      if(dico==NULL){
          return ;
      }
      printf("---------------------------------------\n");
7d2c7bff   rsSimonin   implementation de...
131
      for(int i=0;i<TAILLE;i++){
fc2a89b1   rsimonin   con
132
          printf("%d:",i);
41bf8d90   rsSimonin   correction de bug
133
134
135
136
137
138
          affiche_arbre(dico->alpha[i]);
          printf("\n");
      }
     
  }
  
d6eeddaa   rsimonin   toutes les foncti...
139
  void analyse_fichier(FILE *fp,Dico *pt_dico, int *nb_t, int *nb_f){
46b621e7   rsimonin   chargementdu deux...
140
141
142
143
144
145
      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...
146
          if(mot_existe(pt_dico->alpha[calculcase(mot[0])],mot,0)==1){
46b621e7   rsimonin   chargementdu deux...
147
148
149
150
              nbmot_t++;
          }
          else nbmot_f++;
      }
d6eeddaa   rsimonin   toutes les foncti...
151
152
      *nb_t=nbmot_t;
      *nb_f=nbmot_f;    
46b621e7   rsimonin   chargementdu deux...
153
  }