Blame view

dico.c 4.96 KB
0bc3d1ad   bjeanlou   dico created
1
2
3
  #include "dico.h"
  
  void make_empty_dico(dico d){
003d3e48   bjeanlou   update6 withHash
4
    for(int i=0;i<NBCHAR;i++)
0bc3d1ad   bjeanlou   dico created
5
6
      d[i]=NULL;
  }
a0fe64d2   bjeanlou   Update dico
7
  void delete_dico(dico d){
cf53766d   bjeanlou   update2 dico
8
    for(int i=0;i<NBCHAR;i++)
ccb47d03   bjeanlou   menu quit ok
9
      delete_tree(d+i);
0bc3d1ad   bjeanlou   dico created
10
11
  }
  
11c78e6d   bjeanlou   update5 withHash
12
13
  
  
8c671959   bjeanlou   No more seg fault
14
  byte addto_dico(dico d,const string word){
ccb47d03   bjeanlou   menu quit ok
15
    //return values : -1 if word can't enter, 0if allready in dico, 1 if added
8c671959   bjeanlou   No more seg fault
16
    int i;
2c6f0266   bjeanlou   load and print OK
17
18
    byte isIn=0, endKind=0;
    endKind=end_kind(word);
11c78e6d   bjeanlou   update5 withHash
19
    if(!is_word(endKind)){
8c671959   bjeanlou   No more seg fault
20
      return -1;
a0fe64d2   bjeanlou   Update dico
21
    }
2c6f0266   bjeanlou   load and print OK
22
    tree*tmp=&d[hash(word[0])],*temp=tmp;
fb520d8f   bjeanlou   v1 ended
23
    
8c671959   bjeanlou   No more seg fault
24
    //1st, let's go through the tree, until the word is not in tree
08822d77   bjeanlou   Update 9 withHash
25
    for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
8c671959   bjeanlou   No more seg fault
26
      temp=tmp;
2c6f0266   bjeanlou   load and print OK
27
      if(isalpha(word[i+1]))
08822d77   bjeanlou   Update 9 withHash
28
      tmp=&((*tmp)->next[hash(word[i+1])]);
8c671959   bjeanlou   No more seg fault
29
    }
fb520d8f   bjeanlou   v1 ended
30
    
8c671959   bjeanlou   No more seg fault
31
32
33
34
35
36
    //if word is not ended at the end of the tree
    if(is_empty(*tmp)){
      isIn=0;
      for(; word[i]!='\0' && word[i]!='\'';i++){
        *tmp=make_node(tolower(word[i]),NOT_AN_END);
        temp=tmp;
2c6f0266   bjeanlou   load and print OK
37
38
        if(isalpha(word[i+1]))
  	tmp=&((*tmp)->next[hash(word[i+1])]);
8c671959   bjeanlou   No more seg fault
39
40
      }
      (*temp)->isEnd = endKind;
8c671959   bjeanlou   No more seg fault
41
    }
fb520d8f   bjeanlou   v1 ended
42
    
8c671959   bjeanlou   No more seg fault
43
44
    //if word is ended
    else{
08822d77   bjeanlou   Update 9 withHash
45
      isIn=endKind & (*temp)->isEnd;
2c6f0266   bjeanlou   load and print OK
46
      (*temp)->isEnd = endKind | (*temp)->isEnd;
8c671959   bjeanlou   No more seg fault
47
48
    }
    return isIn;
a0fe64d2   bjeanlou   Update dico
49
50
  }
  
8c671959   bjeanlou   No more seg fault
51
52
53
54
55
   
  
  
  
  
11c78e6d   bjeanlou   update5 withHash
56
57
58
  void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} 
  void loadfrom_file(dico d,FILE*stream){
    if(stream==NULL){
a89bb625   bjeanlou   Update 8 withHash
59
      printf("sorry, we can't open the file\n");
11c78e6d   bjeanlou   update5 withHash
60
      return;
62535895   bjeanlou   Update1 dico
61
    }
003d3e48   bjeanlou   update6 withHash
62
    char word[30]={0};
fb520d8f   bjeanlou   v1 ended
63
    while(fscanf(stream,"%30s",word)==1){
003d3e48   bjeanlou   update6 withHash
64
      addto_dico(d,word);
a89bb625   bjeanlou   Update 8 withHash
65
66
    }
    printf("load success\n");
11c78e6d   bjeanlou   update5 withHash
67
68
  }
  
a89bb625   bjeanlou   Update 8 withHash
69
70
71
72
73
74
75
76
  
  
  
  
  
  
  
  
cf53766d   bjeanlou   update2 dico
77
  void printto_terminal(dico d){printto_file(d,stdout);} 
003d3e48   bjeanlou   update6 withHash
78
79
  void printto_file(dico d,FILE*stream){
    if(stream==NULL){
a89bb625   bjeanlou   Update 8 withHash
80
      printf("sorry, we can't open the file\n");
003d3e48   bjeanlou   update6 withHash
81
82
83
      return;
    }
    if(d==NULL){
a89bb625   bjeanlou   Update 8 withHash
84
      printf("sorry, we can't open the dictionary\n");
003d3e48   bjeanlou   update6 withHash
85
86
      return;
    }
08822d77   bjeanlou   Update 9 withHash
87
    printf("\n\nThis is what the dictionnary contains :\n");
003d3e48   bjeanlou   update6 withHash
88
89
90
91
92
93
94
95
96
    for(int i=0;i<NBCHAR;i++){
      print(d[i],stream,"");
    }
  }
  
  void print(tree t,FILE*stream,string prefix){
    //needs to check stream!=NULL
    if(is_empty(t))
      return;
cf53766d   bjeanlou   update2 dico
97
98
99
100
101
    
    string word=calloc((strlen(prefix)+2),sizeof(char));
    strcpy(word,prefix);
    strncat(word,&(t->letter),1);
    
003d3e48   bjeanlou   update6 withHash
102
    if(is_end(t)){
cf53766d   bjeanlou   update2 dico
103
104
105
      string word2=calloc((strlen(prefix)+4),sizeof(char));
      strcpy(word2,word);
      //common_end
08822d77   bjeanlou   Update 9 withHash
106
107
      if(is_common_end(t) && is_straight_end(t)){
        fprintf(stream,"%s\n",word2);
003d3e48   bjeanlou   update6 withHash
108
      }
08822d77   bjeanlou   Update 9 withHash
109
110
111
112
113
114
115
116
117
118
      if(is_common_end(t) && ends_with_apostrophe(t)){
        fprintf(stream,"%s's\n",word2);
      }
      //proper_end1
      word2[0]=toupper(word2[0]);
      if(is_proper_end(t) && is_straight_end(t)){
        fprintf(stream,"%s\n",word2);
      }
      if(is_proper_end(t) && ends_with_apostrophe(t)){
        fprintf(stream,"%s's\n",word2);
003d3e48   bjeanlou   update6 withHash
119
      }
cf53766d   bjeanlou   update2 dico
120
      //acronyme_end
08822d77   bjeanlou   Update 9 withHash
121
122
123
124
125
126
      strupper(word2);
      if(is_acronyme_end(t) && is_straight_end(t)){
        fprintf(stream,"%s\n",word2);
      }
      if(is_acronyme_end(t) && ends_with_apostrophe(t)){
        fprintf(stream,"%s's\n",word2);
003d3e48   bjeanlou   update6 withHash
127
      }
08822d77   bjeanlou   Update 9 withHash
128
      
cf53766d   bjeanlou   update2 dico
129
      free(word2);
003d3e48   bjeanlou   update6 withHash
130
131
    }
    for(int i=0;i<NBCHAR;i++){
cf53766d   bjeanlou   update2 dico
132
      print(t->next[i],stream,word);
003d3e48   bjeanlou   update6 withHash
133
    }
cf53766d   bjeanlou   update2 dico
134
135
136
      free(word);
  }
  
ccb47d03   bjeanlou   menu quit ok
137
  //uppers all letters in str
cf53766d   bjeanlou   update2 dico
138
139
140
  void strupper(string str){
    for(int i=0;str[i]!='\0';i++)
      str[i]=toupper(str[i]);
0bc3d1ad   bjeanlou   dico created
141
  }
fd1ee590   bjeanlou   Update7 withHash
142
143
  
  
ccb47d03   bjeanlou   menu quit ok
144
145
  byte is_in(dico d,string word){
    //return : -1 if incorrect word, 1 if is in, else 0 
fd1ee590   bjeanlou   Update7 withHash
146
147
    byte endKind=end_kind(word);
    if(!is_word(endKind)){
ccb47d03   bjeanlou   menu quit ok
148
      return -1;
fd1ee590   bjeanlou   Update7 withHash
149
150
    }
    tree tmp=d[hash(word[0])];
a89bb625   bjeanlou   Update 8 withHash
151
    for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){
fd1ee590   bjeanlou   Update7 withHash
152
153
154
155
      tmp=tmp->next[hash(word[i])];
    }
    return !is_empty(tmp) && (endKind & is_end(tmp));
  }
ccb47d03   bjeanlou   menu quit ok
156
157
158
159
160
161
  
  void test_words(dico d,FILE*inStream,FILE*outStream){
    stringLIFO absWords=init_stringLIFO();
    char word[30]={0};
    byte isIn=0,someWrong=0;
    while(fscanf(inStream,"%30s",word)!=EOF){
fb520d8f   bjeanlou   v1 ended
162
163
164
165
166
167
      /*//get rid of punctuation marks
      isIn=0;
      while(ischar_of_word(word[(int)isIn]))
        isIn++;
      word[(int)isIn]='\0';
      */
ccb47d03   bjeanlou   menu quit ok
168
169
170
171
      isIn=is_in(d,word);
      if(isIn<1){
        if(someWrong==0){
  	someWrong=1;
b2638a8a   bjeanlou   création rapport
172
  	fprintf(outStream,"Voici les mots absent du dictionnaire\n");
ccb47d03   bjeanlou   menu quit ok
173
174
175
176
177
178
179
180
181
182
        }
        fprintf(outStream,"%s est absent dans le dictionnaire",word);
        if(isIn)
  	fprintf(outStream,", car il est incorrect.\n");
        else{
  	push(&absWords,word);
  	fprintf(outStream,".\n");
        }
      }
    }
fb520d8f   bjeanlou   v1 ended
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    //fermer outStream pour permettre à l'utilisateur de lire le compte rendu des tests
    if(outStream!=stdout)
      fclose(outStream);
    outStream=stdout;
    
    //aucun mot ne peut être ajouté au dictionnaire ?
    if(is_emptyLIFO(absWords)){
      if(someWrong)
        printf("Parmi les mots absents du dictionnaire, aucun ne peut être ajouté au dictionnaire.\n");
      else
        printf("Felicitations !!!\nAucun des mots que vous avez entre n'est absents du dictionnaire.\n");
      return;
    }
  
  
      //ajout des mots absent?
ccb47d03   bjeanlou   menu quit ok
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    printf("Voulez-vous ajouter certains de ces mots au dictionnaire?\n(1 for yes,0 for no)\n");
    someWrong=saisie(0,1);
    
    //ajout de certains mots au cas par cas
    if(someWrong){
      string aword;
      while(!is_emptyLIFO(absWords)){
        aword=pop(&absWords);
        printf("Voulez-vous ajouter %s au dictionnaire?   (1 for yes,0 for no)\n",aword);
        isIn=saisie(0,1);
        if(isIn){
  	addto_dico(d,aword);
        }
        free(aword);
      }
    }
    delete_LIFO(&absWords);
  }