diff --git a/dico.c b/dico.c index 049e3ee..21ba900 100644 --- a/dico.c +++ b/dico.c @@ -14,41 +14,53 @@ void delete_dico(dico d){ bool addto_dico(dico d,const string s){ byte endKind=end_kind(s); if(!is_word(endKind)){ - printf("incorrect word"); + printf("incorrect word\n"); return true; } - if(is_empty(d[hash(s[0])])){ - d[hash(s[0])]=make_node(s[0],0); - addto_tree2(d[hash(s[0])],s+1,endKind); - return false; - } - else { - return addto_tree(d[hash(s[0])],s+1,endKind); - } + return addto_treeIT(&(d[hash(s[0])]),s,endKind); + } void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} void loadfrom_file(dico d,FILE*stream){ if(stream==NULL){ - printf("sorry, we can't open the file"); + printf("sorry, we can't open the file\n"); return; } char word[30]={0}; - while(fscanf(stream,"%s",word)!=EOF) + while(fgets(word,30,stream)!=NULL){ + if(word[0]=='\n')break; + printf("ok. next :"); + fflush(stdout); addto_dico(d,word); - printf("load success"); + } + printf("load success\n"); } + + + + + + + + + + + + + void printto_terminal(dico d){printto_file(d,stdout);} void printto_file(dico d,FILE*stream){ if(stream==NULL){ - printf("sorry, we can't open the file"); + printf("sorry, we can't open the file\n"); return; } if(d==NULL){ - printf("sorry, we can't open the dictionary"); + printf("sorry, we can't open the dictionary\n"); return; } + printf("This is what the dictionnary contains :\n"); for(int i=0;inext[hash(word[i])]; } return !is_empty(tmp) && (endKind & is_end(tmp)); diff --git a/dico.h b/dico.h index 4b5eafd..18f02f1 100644 --- a/dico.h +++ b/dico.h @@ -11,10 +11,10 @@ bool addto_dico(dico,const string); void loadfrom_file(dico,FILE*); -void loadfrom_keyboard(dico);//i.e. loadfrom_file(dico,stdin); +void loadfrom_keyboard(dico); void printto_file(dico,FILE*); -void printto_terminal(dico);//i.e. printto_file(dico,stdout); +void printto_terminal(dico); void print(tree,FILE*,string); void strupper(string str); diff --git a/essai.txt b/essai.txt new file mode 100644 index 0000000..4bf530a --- /dev/null +++ b/essai.txt @@ -0,0 +1,6 @@ +hello +Hello +gangbang +goodbye + +hon diff --git a/main.c b/main.c new file mode 100644 index 0000000..aaab4f2 --- /dev/null +++ b/main.c @@ -0,0 +1,24 @@ +#include "dico.h" + + + + +int main(int argc, char* argv[]){ + if(argc<2){ + printf("not enough arguments\n"); + return EXIT_FAILURE; + } + FILE*f=fopen(argv[1],"r"); + if(f==NULL){ + printf("wrong arguments\n"); + return EXIT_FAILURE; + } + + printf("hello world\n"); + dico monDico; + make_empty_dico(monDico); + loadfrom_file(monDico,f); + printto_terminal(monDico); + delete_dico(monDico); + return EXIT_SUCCESS; +} diff --git a/treeh.c b/treeh.c index ecd1f2a..98bf877 100644 --- a/treeh.c +++ b/treeh.c @@ -18,7 +18,7 @@ node* make_node(char l,byte end){ n->isEnd=end; for(int i=0;inext[i]=make_empty_tree(); - return n; + return n; } void delete_tree(tree t){ if(is_empty(t))return; @@ -60,29 +60,36 @@ int hash(char c){return c%32-1;} bool ischar_of_word(char c){return isalpha(c) || c=='\'';} byte end_kind(const string s){ - byte endKind=0; + byte endKind; int i=1; + //1st, let's consider all letters if(!isalpha(s[0])) - return 0; + return NOT_AN_END; if(islower(s[0])){ - endKind=1; + endKind=COMMON_END; while(islower(s[i])); } else {//if isupper(s[0]) - endKind=4; if(isalpha(s[1])){ i++; - if(islower(s[1])) + if(islower(s[1])){ + endKind=PROPER_END; while(islower(s[i])); + } else{//if isupper(s[1]) + endKind=ACRONYME_END; while(isupper(s[i])); endKind=16; } } } - endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') ); - return endKind; + //then let's consider the end + if(s[i]=='\0') + return endKind & STRAIGHT_END; + if(s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') + return endKind & APOSTROPHE_END; + return NOT_AN_END; } bool is_word(const byte endKind){ return endKind!=0; @@ -120,3 +127,30 @@ void addto_tree2(tree t,const string s,char endKind){ addto_tree2(t->next[hash(s[0])],s+1,endKind); } + +bool addto_treeIT(tree*t,const string word ,byte endKind){ + bool isIn; + int i; + //1st, let's go through the tree, until the word is not in tree + for(i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(*t) ;i++){ + t=&((*t)->next[hash(word[i])]); + } + printf("hi\n"); + //if word is not ended at the end of the tree + if(is_empty(*t)){ + isIn=false; + for(; word[i]!='\0' && word[i]!='\'';i++){ + *t=make_node(tolower(word[i]),NOT_AN_END); + t=&((*t)->next[hash(word[i])]); + } + (*t)->isEnd=endKind; + printf("actions ended\n"); + } + //if word is ended + else{ + isIn=endKind & is_end(*t); + (*t)->isEnd=endKind | is_end(*t); + printf("action ended\n"); + } + return isIn; +} diff --git a/treeh.h b/treeh.h index 971ba7d..f38b2c2 100644 --- a/treeh.h +++ b/treeh.h @@ -9,11 +9,12 @@ #define NBCHAR 26 //A-Z +#define NOT_AN_END 0x00 #define COMMON_END 0x03 #define PROPER_END 0x0B #define ACRONYME_END 0x30 -#define STRAIGHT_END 0X15 -#define APOSTROPHE_END 0X2A +#define STRAIGHT_END 0x15 +#define APOSTROPHE_END 0x2A typedef char *string,byte; typedef struct _node node, *tree; @@ -48,6 +49,7 @@ 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); #endif //TREEH_H -- libgit2 0.21.2