diff --git a/dico.c b/dico.c index 21ba900..8b0a6ef 100644 --- a/dico.c +++ b/dico.c @@ -11,16 +11,47 @@ void delete_dico(dico d){ -bool addto_dico(dico d,const string s){ - byte endKind=end_kind(s); +byte addto_dico(dico d,const string word){ + //-1 if word can't enter, 0if allready in dico, 1 if added + int i; + byte isIn, endKind=end_kind(word); if(!is_word(endKind)){ printf("incorrect word\n"); - return true; + return -1; } - return addto_treeIT(&(d[hash(s[0])]),s,endKind); - + tree*tmp=&d[hash(word[0])],*temp; + //1st, let's go through the tree, until the word is not in tree + for(i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){ + temp=tmp; + tmp=&((*tmp)->next[hash(word[i])]); + } + //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; + tmp=&((*tmp)->next[hash(word[i])]); + } + (*temp)->isEnd = endKind; + printf("actions ended\n"); + fflush(stdout); + } + //if word is ended + else{ + isIn=endKind & is_end(*tmp); + (*tmp)->isEnd = endKind | is_end(*tmp); + printf("action ended\n"); + fflush(stdout); + } + return isIn; } + + + + + void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} void loadfrom_file(dico d,FILE*stream){ if(stream==NULL){ @@ -28,10 +59,7 @@ void loadfrom_file(dico d,FILE*stream){ return; } char word[30]={0}; - while(fgets(word,30,stream)!=NULL){ - if(word[0]=='\n')break; - printf("ok. next :"); - fflush(stdout); + while(fscanf(stream,"%30s",word)!=EOF){ addto_dico(d,word); } printf("load success\n"); diff --git a/dico.h b/dico.h index 18f02f1..495feb4 100644 --- a/dico.h +++ b/dico.h @@ -7,7 +7,7 @@ typedef tree dico[NBCHAR]; void make_empty_dico(dico); void delete_dico(dico); -bool addto_dico(dico,const string); +byte addto_dico(dico,const string); void loadfrom_file(dico,FILE*); diff --git a/main.c b/main.c index aaab4f2..de72dbd 100644 --- a/main.c +++ b/main.c @@ -21,4 +21,4 @@ int main(int argc, char* argv[]){ printto_terminal(monDico); delete_dico(monDico); return EXIT_SUCCESS; -} +} diff --git a/makefile b/makefile index a85d202..3005d75 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,10 @@ #makefile pour la branche withHash #nom de l'executeble -EXEC = dictionnaire +EXEC = dico.run #nom du compilateur CC = gcc #warnings utilisés -WARN = -W -Wall -Wextra +WARN = -W -Wall -Wextra -g #librairies #sources diff --git a/treeh.c b/treeh.c index 98bf877..0a9633f 100644 --- a/treeh.c +++ b/treeh.c @@ -68,18 +68,18 @@ byte end_kind(const string s){ if(islower(s[0])){ endKind=COMMON_END; - while(islower(s[i])); + while(islower(s[i]))i++; } else {//if isupper(s[0]) if(isalpha(s[1])){ i++; if(islower(s[1])){ endKind=PROPER_END; - while(islower(s[i])); + while(islower(s[i]))i++; } else{//if isupper(s[1]) endKind=ACRONYME_END; - while(isupper(s[i])); + while(isupper(s[i]))i++; endKind=16; } } @@ -98,35 +98,6 @@ bool is_word(const byte endKind){ -//loading functions -bool addto_tree(tree t,const string s,char endKind){ - //recursive, need to check all letter in s are alpha or "\'s" - //return wether s is already in t or not - bool ret; - if(s[0]=='\0' || s[0]=='\''){ - ret=t->isEnd & endKind; - t->isEnd=t->isEnd|endKind; - return ret; - } - if(is_empty(t->next[hash(s[0])])){ - t->next[hash(s[0])]=make_node(s[0],0); - addto_tree2(t->next[hash(s[0])],s+1,endKind); - return false; - } - else - return addto_tree(t->next[hash(s[0])],s+1,endKind); -} -void addto_tree2(tree t,const string s,char endKind){ - //faster than addto_tree - //used when it is known the word is not yet in dictionnary - if(s[0]=='\0' || s[0]=='\''){ - t->isEnd=endKind; - return; - } - t->next[hash(s[0])]=make_node(s[0],0); - addto_tree2(t->next[hash(s[0])],s+1,endKind); -} - bool addto_treeIT(tree*t,const string word ,byte endKind){ bool isIn; @@ -154,3 +125,4 @@ bool addto_treeIT(tree*t,const string word ,byte endKind){ } return isIn; } + diff --git a/treeh.h b/treeh.h index f38b2c2..a73f722 100644 --- a/treeh.h +++ b/treeh.h @@ -47,9 +47,9 @@ bool is_word(byte endKind);//pass end_kind() as parameter byte end_kind(string); //recursive can only be called in a addto_dico -bool addto_tree(tree,const string,byte); -void addto_tree2(tree,const string,byte); -bool addto_treeIT(tree*,const string,byte); +//bool addto_tree(tree,const string,byte); +//void addto_tree2(tree,const string,byte); +//bool addto_treeIT(tree*,const string,byte); #endif //TREEH_H -- libgit2 0.21.2