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,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 if(!is_word(endKind)){ 18 if(!is_word(endKind)){
17 printf("incorrect word\n"); 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 void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} 55 void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
25 void loadfrom_file(dico d,FILE*stream){ 56 void loadfrom_file(dico d,FILE*stream){
26 if(stream==NULL){ 57 if(stream==NULL){
@@ -28,10 +59,7 @@ void loadfrom_file(dico d,FILE*stream){ @@ -28,10 +59,7 @@ void loadfrom_file(dico d,FILE*stream){
28 return; 59 return;
29 } 60 }
30 char word[30]={0}; 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 addto_dico(d,word); 63 addto_dico(d,word);
36 } 64 }
37 printf("load success\n"); 65 printf("load success\n");
@@ -7,7 +7,7 @@ typedef tree dico[NBCHAR]; @@ -7,7 +7,7 @@ typedef tree dico[NBCHAR];
7 7
8 void make_empty_dico(dico); 8 void make_empty_dico(dico);
9 void delete_dico(dico); 9 void delete_dico(dico);
10 -bool addto_dico(dico,const string); 10 +byte addto_dico(dico,const string);
11 11
12 12
13 void loadfrom_file(dico,FILE*); 13 void loadfrom_file(dico,FILE*);
@@ -21,4 +21,4 @@ int main(int argc, char* argv[]){ @@ -21,4 +21,4 @@ int main(int argc, char* argv[]){
21 printto_terminal(monDico); 21 printto_terminal(monDico);
22 delete_dico(monDico); 22 delete_dico(monDico);
23 return EXIT_SUCCESS; 23 return EXIT_SUCCESS;
24 -} 24 +}
1 #makefile pour la branche withHash 1 #makefile pour la branche withHash
2 #nom de l'executeble 2 #nom de l'executeble
3 -EXEC = dictionnaire 3 +EXEC = dico.run
4 #nom du compilateur 4 #nom du compilateur
5 CC = gcc 5 CC = gcc
6 #warnings utilisés 6 #warnings utilisés
7 -WARN = -W -Wall -Wextra 7 +WARN = -W -Wall -Wextra -g
8 #librairies 8 #librairies
9 9
10 #sources 10 #sources
@@ -68,18 +68,18 @@ byte end_kind(const string s){ @@ -68,18 +68,18 @@ byte end_kind(const string s){
68 68
69 if(islower(s[0])){ 69 if(islower(s[0])){
70 endKind=COMMON_END; 70 endKind=COMMON_END;
71 - while(islower(s[i])); 71 + while(islower(s[i]))i++;
72 } 72 }
73 else {//if isupper(s[0]) 73 else {//if isupper(s[0])
74 if(isalpha(s[1])){ 74 if(isalpha(s[1])){
75 i++; 75 i++;
76 if(islower(s[1])){ 76 if(islower(s[1])){
77 endKind=PROPER_END; 77 endKind=PROPER_END;
78 - while(islower(s[i])); 78 + while(islower(s[i]))i++;
79 } 79 }
80 else{//if isupper(s[1]) 80 else{//if isupper(s[1])
81 endKind=ACRONYME_END; 81 endKind=ACRONYME_END;
82 - while(isupper(s[i])); 82 + while(isupper(s[i]))i++;
83 endKind=16; 83 endKind=16;
84 } 84 }
85 } 85 }
@@ -98,35 +98,6 @@ bool is_word(const byte endKind){ @@ -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 bool addto_treeIT(tree*t,const string word ,byte endKind){ 102 bool addto_treeIT(tree*t,const string word ,byte endKind){
132 bool isIn; 103 bool isIn;
@@ -154,3 +125,4 @@ bool addto_treeIT(tree*t,const string word ,byte endKind){ @@ -154,3 +125,4 @@ bool addto_treeIT(tree*t,const string word ,byte endKind){
154 } 125 }
155 return isIn; 126 return isIn;
156 } 127 }
  128 +
@@ -47,9 +47,9 @@ bool is_word(byte endKind);//pass end_kind() as parameter @@ -47,9 +47,9 @@ bool is_word(byte endKind);//pass end_kind() as parameter
47 byte end_kind(string); 47 byte end_kind(string);
48 48
49 //recursive can only be called in a addto_dico 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 #endif //TREEH_H 55 #endif //TREEH_H