Commit 8c671959dd58d71c95499a4c758d1cc6cd83f2b3

Authored by bjeanlou
1 parent a89bb625

No more seg fault

Showing 6 changed files with 48 additions and 48 deletions   Show diff stats
... ... @@ -11,16 +11,47 @@ void delete_dico(dico d){
11 11  
12 12  
13 13  
14   -bool addto_dico(dico d,const string s){
15   - byte endKind=end_kind(s);
  14 +byte addto_dico(dico d,const string word){
  15 + //-1 if word can't enter, 0if allready in dico, 1 if added
  16 + int i;
  17 + byte isIn, endKind=end_kind(word);
16 18 if(!is_word(endKind)){
17 19 printf("incorrect word\n");
18   - return true;
  20 + return -1;
19 21 }
20   - return addto_treeIT(&(d[hash(s[0])]),s,endKind);
21   -
  22 + tree*tmp=&d[hash(word[0])],*temp;
  23 + //1st, let's go through the tree, until the word is not in tree
  24 + for(i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
  25 + temp=tmp;
  26 + tmp=&((*tmp)->next[hash(word[i])]);
  27 + }
  28 + //if word is not ended at the end of the tree
  29 + if(is_empty(*tmp)){
  30 + isIn=0;
  31 + for(; word[i]!='\0' && word[i]!='\'';i++){
  32 + *tmp=make_node(tolower(word[i]),NOT_AN_END);
  33 + temp=tmp;
  34 + tmp=&((*tmp)->next[hash(word[i])]);
  35 + }
  36 + (*temp)->isEnd = endKind;
  37 + printf("actions ended\n");
  38 + fflush(stdout);
  39 + }
  40 + //if word is ended
  41 + else{
  42 + isIn=endKind & is_end(*tmp);
  43 + (*tmp)->isEnd = endKind | is_end(*tmp);
  44 + printf("action ended\n");
  45 + fflush(stdout);
  46 + }
  47 + return isIn;
22 48 }
23 49  
  50 +
  51 +
  52 +
  53 +
  54 +
24 55 void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
25 56 void loadfrom_file(dico d,FILE*stream){
26 57 if(stream==NULL){
... ... @@ -28,10 +59,7 @@ void loadfrom_file(dico d,FILE*stream){
28 59 return;
29 60 }
30 61 char word[30]={0};
31   - while(fgets(word,30,stream)!=NULL){
32   - if(word[0]=='\n')break;
33   - printf("ok. next :");
34   - fflush(stdout);
  62 + while(fscanf(stream,"%30s",word)!=EOF){
35 63 addto_dico(d,word);
36 64 }
37 65 printf("load success\n");
... ...
... ... @@ -7,7 +7,7 @@ typedef tree dico[NBCHAR];
7 7  
8 8 void make_empty_dico(dico);
9 9 void delete_dico(dico);
10   -bool addto_dico(dico,const string);
  10 +byte addto_dico(dico,const string);
11 11  
12 12  
13 13 void loadfrom_file(dico,FILE*);
... ...
... ... @@ -21,4 +21,4 @@ int main(int argc, char* argv[]){
21 21 printto_terminal(monDico);
22 22 delete_dico(monDico);
23 23 return EXIT_SUCCESS;
24   -}
  24 +}
... ...
makefile
1 1 #makefile pour la branche withHash
2 2 #nom de l'executeble
3   -EXEC = dictionnaire
  3 +EXEC = dico.run
4 4 #nom du compilateur
5 5 CC = gcc
6 6 #warnings utilisรฉs
7   -WARN = -W -Wall -Wextra
  7 +WARN = -W -Wall -Wextra -g
8 8 #librairies
9 9  
10 10 #sources
... ...
... ... @@ -68,18 +68,18 @@ byte end_kind(const string s){
68 68  
69 69 if(islower(s[0])){
70 70 endKind=COMMON_END;
71   - while(islower(s[i]));
  71 + while(islower(s[i]))i++;
72 72 }
73 73 else {//if isupper(s[0])
74 74 if(isalpha(s[1])){
75 75 i++;
76 76 if(islower(s[1])){
77 77 endKind=PROPER_END;
78   - while(islower(s[i]));
  78 + while(islower(s[i]))i++;
79 79 }
80 80 else{//if isupper(s[1])
81 81 endKind=ACRONYME_END;
82   - while(isupper(s[i]));
  82 + while(isupper(s[i]))i++;
83 83 endKind=16;
84 84 }
85 85 }
... ... @@ -98,35 +98,6 @@ bool is_word(const byte endKind){
98 98  
99 99  
100 100  
101   -//loading functions
102   -bool addto_tree(tree t,const string s,char endKind){
103   - //recursive, need to check all letter in s are alpha or "\'s"
104   - //return wether s is already in t or not
105   - bool ret;
106   - if(s[0]=='\0' || s[0]=='\''){
107   - ret=t->isEnd & endKind;
108   - t->isEnd=t->isEnd|endKind;
109   - return ret;
110   - }
111   - if(is_empty(t->next[hash(s[0])])){
112   - t->next[hash(s[0])]=make_node(s[0],0);
113   - addto_tree2(t->next[hash(s[0])],s+1,endKind);
114   - return false;
115   - }
116   - else
117   - return addto_tree(t->next[hash(s[0])],s+1,endKind);
118   -}
119   -void addto_tree2(tree t,const string s,char endKind){
120   - //faster than addto_tree
121   - //used when it is known the word is not yet in dictionnary
122   - if(s[0]=='\0' || s[0]=='\''){
123   - t->isEnd=endKind;
124   - return;
125   - }
126   - t->next[hash(s[0])]=make_node(s[0],0);
127   - addto_tree2(t->next[hash(s[0])],s+1,endKind);
128   -}
129   -
130 101  
131 102 bool addto_treeIT(tree*t,const string word ,byte endKind){
132 103 bool isIn;
... ... @@ -154,3 +125,4 @@ bool addto_treeIT(tree*t,const string word ,byte endKind){
154 125 }
155 126 return isIn;
156 127 }
  128 +
... ...
... ... @@ -47,9 +47,9 @@ bool is_word(byte endKind);//pass end_kind() as parameter
47 47 byte end_kind(string);
48 48  
49 49 //recursive can only be called in a addto_dico
50   -bool addto_tree(tree,const string,byte);
51   -void addto_tree2(tree,const string,byte);
52   -bool addto_treeIT(tree*,const string,byte);
  50 +//bool addto_tree(tree,const string,byte);
  51 +//void addto_tree2(tree,const string,byte);
  52 +//bool addto_treeIT(tree*,const string,byte);
53 53  
54 54  
55 55 #endif //TREEH_H
... ...