From 2c6f0266899e2b572205033d2251b35b0dbc6e6d Mon Sep 17 00:00:00 2001 From: bjeanlou Date: Fri, 3 May 2019 17:38:44 +0200 Subject: [PATCH] load and print OK --- dico.c | 14 ++++++++------ essai.txt | 1 - essai1.txt | 5 ++++- main.c | 13 ++++++++----- treeh.c | 8 +++++--- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/dico.c b/dico.c index 596a19d..698a257 100644 --- a/dico.c +++ b/dico.c @@ -14,16 +14,17 @@ void delete_dico(dico d){ 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); - printf("%x\n",endKind); + byte isIn=0, endKind=0; + endKind=end_kind(word); if(!is_word(endKind)){ printf("%s is incorrect\n",word); return -1; } - tree*tmp=&d[hash(word[0])],*temp; + tree*tmp=&d[hash(word[0])],*temp=tmp; //1st, let's go through the tree, until the word is not in tree for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){ temp=tmp; + if(isalpha(word[i+1])) tmp=&((*tmp)->next[hash(word[i+1])]); } //if word is not ended at the end of the tree @@ -32,7 +33,8 @@ byte addto_dico(dico d,const string word){ for(; word[i]!='\0' && word[i]!='\'';i++){ *tmp=make_node(tolower(word[i]),NOT_AN_END); temp=tmp; - tmp=&((*tmp)->next[hash(word[i])]); + if(isalpha(word[i+1])) + tmp=&((*tmp)->next[hash(word[i+1])]); } (*temp)->isEnd = endKind; printf("%s added\n",word); @@ -41,8 +43,8 @@ byte addto_dico(dico d,const string word){ //if word is ended else{ isIn=endKind & (*temp)->isEnd; - (*temp)->isEnd = endKind | is_end(*temp); - printf("%s allready in\n",word); + (*temp)->isEnd = endKind | (*temp)->isEnd; + printf("%s %s in\n",word,(isIn!=0)?("allready"):("not yet")); fflush(stdout); } return isIn; diff --git a/essai.txt b/essai.txt index a8c7a82..e8be3d9 100644 --- a/essai.txt +++ b/essai.txt @@ -4,7 +4,6 @@ HELLO hello's Hello's HELLO's -A hello Hello HELLO diff --git a/essai1.txt b/essai1.txt index 56735b6..cec973f 100644 --- a/essai1.txt +++ b/essai1.txt @@ -1 +1,4 @@ -baraban +A +A's +a +a's diff --git a/main.c b/main.c index 8f240a5..062b677 100644 --- a/main.c +++ b/main.c @@ -8,18 +8,21 @@ int main(int argc, char* argv[]){ printf("not enough arguments\n"); return EXIT_FAILURE; } - FILE*f=fopen(argv[1],"r"); - if(f==NULL){ + FILE*fileIn=fopen(argv[1],"r"); + if(fileIn==NULL){ printf("wrong arguments\n"); return EXIT_FAILURE; } + FILE*fileOut=fopen("out.txt","w"); printf("bienvenue dans le dictonnaire\n\n"); dico monDico; make_empty_dico(monDico); - loadfrom_file(monDico,f); - printto_terminal(monDico); + loadfrom_file(monDico,fileIn); + printto_file(monDico,stdout); delete_dico(monDico); - fclose(f); + fclose(fileIn); + fclose(fileOut); + return EXIT_SUCCESS; } diff --git a/treeh.c b/treeh.c index 51c1476..d0c8bb2 100644 --- a/treeh.c +++ b/treeh.c @@ -5,6 +5,7 @@ tree make_empty_tree(){ return NULL; } node* make_empty_node(){ + //<=> make_node('\0',NOT_AN_END) node*n=malloc(sizeof(node)); n->letter='\0'; n->isEnd=NOT_AN_END; @@ -60,7 +61,7 @@ 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; + byte endKind=NOT_AN_END; int i=1; //1st, let's consider all letters if(!isalpha(s[0])) @@ -71,16 +72,17 @@ byte end_kind(const string s){ while(islower(s[i]))i++; } else {//if isupper(s[0]) + endKind=PROPER_END; if(isalpha(s[1])){ - i++; if(islower(s[1])){ endKind=PROPER_END; + i=2; while(islower(s[i]))i++; } else{//if isupper(s[1]) endKind=ACRONYME_END; + i=2; while(isupper(s[i]))i++; - endKind=16; } } } -- libgit2 0.21.2