Blame view

arbre.c 2.09 KB
5e43fa77   rsSimonin   arbre.c
1
2
3
4
5
6
7
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  
  
  typedef struct arbre{
      char val;
d6bcca7a   rsSimonin   initialisation
8
      struct arbre *suite[26];
5e43fa77   rsSimonin   arbre.c
9
10
11
12
13
14
15
16
17
18
19
      bool finmot; //1 si fin de mot
  }Arbre;
  
  typedef struct dico {
      Arbre *alpha[26];
  }Dico;
  
  
  
  
  
5e43fa77   rsSimonin   arbre.c
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  void arbre_vide(struct arbre ** pt_arbre)
  {
  	*pt_arbre = NULL;
  }
  
  bool est_vide(struct arbre *arbre)
  {
  	return arbre==NULL;
  }
  
  bool fin_de_mot(struct arbre *arbre)
  {
  	return arbre->finmot;
  }
5e43fa77   rsSimonin   arbre.c
34
35
  
  void cons_dico(struct dico **pt_dico,char val){
d6bcca7a   rsSimonin   initialisation
36
37
38
39
40
41
      Dico *mondico=malloc(sizeof(struct dico));
      mondico->alpha[val-97]->val=val; // (ascii)->a = 97
      for(int i=0;i<26;i++){
          mondico->alpha[val-97]->suite[i]=NULL;        
      }
      mondico->alpha[val-97]->finmot=false;
5e43fa77   rsSimonin   arbre.c
42
43
44
45
46
      (*pt_dico)=mondico;
  }
  
  void cons_arbre(struct arbre **pt_arbre,char val){
      struct arbre *monarbre=malloc(sizeof(struct arbre));
2e977567   rsSimonin   2 commit
47
48
49
50
51
52
      monarbre->val=val; // (ascii)->a = 97
      for(int i=0;i<26;i++){
          monarbre->suite[i]=NULL;        
      }
      monarbre->finmot=false;
      (*pt_arbre)=monarbre;
5e43fa77   rsSimonin   arbre.c
53
54
  }
  
d6bcca7a   rsSimonin   initialisation
55
56
57
58
59
60
61
  
  
  void ini_dico(struct dico * pt_dico){
      for(int i=0;i<26;i++){
          cons_dico(&pt_dico,97+i);        
      }
  }
2e977567   rsSimonin   2 commit
62
63
64
65
66
    
    
  void ajout_mot(struct dico *pt_dico,char mot[]){
      int i=0;
      while(mot[i]!='\0'){
f325dd23   rsimonin   modif init dico
67
          if (pt_dico->alpha[mot[i]-'a']==NULL){
2e977567   rsSimonin   2 commit
68
69
70
71
72
              cons_arbre(&(pt_dico->alpha[mot[i]-'a']),mot[i]);
              
          }
          i++;
      }
f325dd23   rsimonin   modif init dico
73
      pt_dico->alpha[mot[i]-'a']->finmot=true;
d6bcca7a   rsSimonin   initialisation
74
      
2e977567   rsSimonin   2 commit
75
76
77
78
79
80
  }
  
  void charger_arbre(FILE *fp, struct dico **pt_dico)
  {   char mot[20];
      while (fscanf(fp, "%s", mot)!=EOF){
          ajout_mot(*pt_dico,mot);
2e977567   rsSimonin   2 commit
81
82
      }
      return ;
2e977567   rsSimonin   2 commit
83
  }
d6bcca7a   rsSimonin   initialisation
84
  
075f46e2   rsimonin   affichage et corr...
85
86
  void free_arbre(struct dico **pt_dico){
      if (*pt_dico->
d6bcca7a   rsSimonin   initialisation
87
  
075f46e2   rsimonin   affichage et corr...
88
  }
d6bcca7a   rsSimonin   initialisation
89
  
075f46e2   rsimonin   affichage et corr...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  void affiche_dico(struct dico *dico){
      if(dico==NULL){
          return ;
      }
      for(int i=0;i<26;i++){
          affiche_arbre(dico->alpha[i]);
      }
  }
  
  void affiche_arbre(struct arbre *arbre){
      if(arbre==NULL){
          return ;
      }
      printf("%c\n",arbre->val);
      for(int i=0;i<26;i++){
          affiche_arbre(arbre->suite[i]);
      }
  }
d6bcca7a   rsSimonin   initialisation
108
109
  
  
5e43fa77   rsSimonin   arbre.c
110
  int main (){
075f46e2   rsimonin   affichage et corr...
111
112
        Dico *mondico=malloc(sizeof(struct dico));
        charger_arbre("dicotest.txt",mondico);
5e43fa77   rsSimonin   arbre.c
113
114
115
116
117
118
      
      
      
      
      return 0;
  }