diff --git a/dico.c b/dico.c index aafa812..4184c2a 100644 --- a/dico.c +++ b/dico.c @@ -9,33 +9,50 @@ void delete_dico(dico d){ delete_tree(d[i]); } -byte end_kind(const string s){ - byte endKind=0; - int i=1; - if(!isalpha(s[0])) - return 0; - if(islower(s[0])){ - endKind=1; - while(islower(s[i])); + + +bool addto_dico(dico d,string s){ + byte endKind=end_kind(s); + if(!is_word(endKind)){ + printf("incorrect word"); + return true; } - else {//if isupper(s[0]) - endKind=2; - if(!isalpha(s[1])){ - - } + 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); } - endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') ); - return endKind; } -bool is_word(const byte endKind){ - return end_Kind!=0; +bool lire(string s,FILE*stream){ + //need to check if stream !=NULL + //ret true if error else false + char* posRet=NULL; + if (fscanf(stream,"%s",s) != NULL){ + posRet=strchr(chaine, '\n'); + if (posRet!= NULL){ + *posRet = '\0'; + } + return false; + } + else + return true; } -bool addto_dico(dico d,string s){ - if(!is_word(end_kind(s))){ - printf("incorrect word"); - return true; +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"); + return; } - + string str=calloc(30*sizeof(char)); + while(fscanf(""strstream)!=NULL) +} + +void printto_terminal(dico d){printto_file(dico,stdout);} +void printto_file(dico d,FILE*f){ + ; } diff --git a/dico.h b/dico.h index 172394a..1d12267 100644 --- a/dico.h +++ b/dico.h @@ -9,14 +9,12 @@ void make_empty_dico(dico); void delete_dico(dico); bool addto_dico(dico,string); -bool is_word(byte endKind);//pass end_kind() as parameter -byte end_kind(string); void loadfrom_file(dico,FILE*); -void loadfrom_keyboard(dico); +void loadfrom_keyboard(dico);//i.e. loadfrom_file(dico,stdin); void printto_file(dico,FILE*); -void printto_terminal(dico); +void printto_terminal(dico);//i.e. printto_file(dico,stdout); #endif diff --git a/treeh.c b/treeh.c index b66e97e..c86dafc 100644 --- a/treeh.c +++ b/treeh.c @@ -45,17 +45,48 @@ bool is_followed(const tree t){ } //functions is_end -bool is_end(const tree t){return t->isEnd & 13;}//0b00010101 -bool ends_with_apostrophe(const tree t){return t->isEnd & 26;}//0b00101010 +bool is_end(const tree t){return t->isEnd & 21;}//0b00010101 +bool ends_with_apostrophe(const tree t){return t->isEnd & 42;}//0b00101010 bool is_common_end(const tree t){return t->isEnd & 3;}//0b00000011 bool is_proper_end(const tree t){return t->isEnd & 12;}//0b00001100 bool is_acronyme_end(const tree t){return t->isEnd & 48;}//0b00110000 -int hash(char c){ - //needs to check c wether isalpha - return c%32-1; + + +//needs to check c wether isalpha +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; + int i=1; + if(!isalpha(s[0])) + return 0; + + if(islower(s[0])){ + endKind=1; + while(islower(s[i])); + } + else {//if isupper(s[0]) + endKind=4; + if(isalpha(s[1])){ + i++; + if(islower(s[1])) + while(islower[i]); + else{//if isupper(s[1]) + while(isupper(s[i])); + endKind=16; + } + } + } + endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') ); + return endKind; } +bool is_word(const byte endKind){ + return end_Kind!=0; +} + @@ -64,17 +95,12 @@ 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'){ - ret=is_end(t); + if(s[0]=='\0' || s[0]=='\''){ + ret=t->isEnd & endKind; t->isEnd=t->isEnd|endKind; return ret; } - if(s[0]=='\''){ - ret=ends_with_apostrophe(t); - t->isEnd=t->isEnd|endKind; - return ret; - } - if(t->next[hash(s[0])]==NULL){ + 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; diff --git a/treeh.h b/treeh.h index a004979..0bc7604 100644 --- a/treeh.h +++ b/treeh.h @@ -33,9 +33,11 @@ bool is_proper_end(const tree);//if true 1st letter is upper bool is_acronyme_end(const tree);//if true all letters are upper int hash(char);//need to check if isalpha +bool ischar_of_word(char);//tells if char can be in a word +bool is_word(byte endKind);//pass end_kind() as parameter +byte end_kind(string); -//recursive can only be called in a dico instance -// +//recursive can only be called in a addto_dico bool addto_tree(tree,const string,byte); void addto_tree2(tree,const string,byte); -- libgit2 0.21.2