Commit 2c6f0266899e2b572205033d2251b35b0dbc6e6d

Authored by bjeanlou
1 parent 08822d77

load and print OK

Showing 5 changed files with 25 additions and 16 deletions   Show diff stats
@@ -14,16 +14,17 @@ void delete_dico(dico d){ @@ -14,16 +14,17 @@ void delete_dico(dico d){
14 byte addto_dico(dico d,const string word){ 14 byte addto_dico(dico d,const string word){
15 //-1 if word can't enter, 0if allready in dico, 1 if added 15 //-1 if word can't enter, 0if allready in dico, 1 if added
16 int i; 16 int i;
17 - byte isIn, endKind=end_kind(word);  
18 - printf("%x\n",endKind); 17 + byte isIn=0, endKind=0;
  18 + endKind=end_kind(word);
19 if(!is_word(endKind)){ 19 if(!is_word(endKind)){
20 printf("%s is incorrect\n",word); 20 printf("%s is incorrect\n",word);
21 return -1; 21 return -1;
22 } 22 }
23 - tree*tmp=&d[hash(word[0])],*temp; 23 + tree*tmp=&d[hash(word[0])],*temp=tmp;
24 //1st, let's go through the tree, until the word is not in tree 24 //1st, let's go through the tree, until the word is not in tree
25 for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){ 25 for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
26 temp=tmp; 26 temp=tmp;
  27 + if(isalpha(word[i+1]))
27 tmp=&((*tmp)->next[hash(word[i+1])]); 28 tmp=&((*tmp)->next[hash(word[i+1])]);
28 } 29 }
29 //if word is not ended at the end of the tree 30 //if word is not ended at the end of the tree
@@ -32,7 +33,8 @@ byte addto_dico(dico d,const string word){ @@ -32,7 +33,8 @@ byte addto_dico(dico d,const string word){
32 for(; word[i]!='\0' && word[i]!='\'';i++){ 33 for(; word[i]!='\0' && word[i]!='\'';i++){
33 *tmp=make_node(tolower(word[i]),NOT_AN_END); 34 *tmp=make_node(tolower(word[i]),NOT_AN_END);
34 temp=tmp; 35 temp=tmp;
35 - tmp=&((*tmp)->next[hash(word[i])]); 36 + if(isalpha(word[i+1]))
  37 + tmp=&((*tmp)->next[hash(word[i+1])]);
36 } 38 }
37 (*temp)->isEnd = endKind; 39 (*temp)->isEnd = endKind;
38 printf("%s added\n",word); 40 printf("%s added\n",word);
@@ -41,8 +43,8 @@ byte addto_dico(dico d,const string word){ @@ -41,8 +43,8 @@ byte addto_dico(dico d,const string word){
41 //if word is ended 43 //if word is ended
42 else{ 44 else{
43 isIn=endKind & (*temp)->isEnd; 45 isIn=endKind & (*temp)->isEnd;
44 - (*temp)->isEnd = endKind | is_end(*temp);  
45 - printf("%s allready in\n",word); 46 + (*temp)->isEnd = endKind | (*temp)->isEnd;
  47 + printf("%s %s in\n",word,(isIn!=0)?("allready"):("not yet"));
46 fflush(stdout); 48 fflush(stdout);
47 } 49 }
48 return isIn; 50 return isIn;
@@ -4,7 +4,6 @@ HELLO @@ -4,7 +4,6 @@ HELLO
4 hello's 4 hello's
5 Hello's 5 Hello's
6 HELLO's 6 HELLO's
7 -A  
8 hello 7 hello
9 Hello 8 Hello
10 HELLO 9 HELLO
1 -baraban 1 +A
  2 +A's
  3 +a
  4 +a's
@@ -8,18 +8,21 @@ int main(int argc, char* argv[]){ @@ -8,18 +8,21 @@ int main(int argc, char* argv[]){
8 printf("not enough arguments\n"); 8 printf("not enough arguments\n");
9 return EXIT_FAILURE; 9 return EXIT_FAILURE;
10 } 10 }
11 - FILE*f=fopen(argv[1],"r");  
12 - if(f==NULL){ 11 + FILE*fileIn=fopen(argv[1],"r");
  12 + if(fileIn==NULL){
13 printf("wrong arguments\n"); 13 printf("wrong arguments\n");
14 return EXIT_FAILURE; 14 return EXIT_FAILURE;
15 } 15 }
  16 + FILE*fileOut=fopen("out.txt","w");
16 17
17 printf("bienvenue dans le dictonnaire\n\n"); 18 printf("bienvenue dans le dictonnaire\n\n");
18 dico monDico; 19 dico monDico;
19 make_empty_dico(monDico); 20 make_empty_dico(monDico);
20 - loadfrom_file(monDico,f);  
21 - printto_terminal(monDico); 21 + loadfrom_file(monDico,fileIn);
  22 + printto_file(monDico,stdout);
22 delete_dico(monDico); 23 delete_dico(monDico);
23 - fclose(f); 24 + fclose(fileIn);
  25 + fclose(fileOut);
  26 +
24 return EXIT_SUCCESS; 27 return EXIT_SUCCESS;
25 } 28 }
@@ -5,6 +5,7 @@ tree make_empty_tree(){ @@ -5,6 +5,7 @@ tree make_empty_tree(){
5 return NULL; 5 return NULL;
6 } 6 }
7 node* make_empty_node(){ 7 node* make_empty_node(){
  8 + //<=> make_node('\0',NOT_AN_END)
8 node*n=malloc(sizeof(node)); 9 node*n=malloc(sizeof(node));
9 n->letter='\0'; 10 n->letter='\0';
10 n->isEnd=NOT_AN_END; 11 n->isEnd=NOT_AN_END;
@@ -60,7 +61,7 @@ int hash(char c){return c%32-1;} @@ -60,7 +61,7 @@ int hash(char c){return c%32-1;}
60 bool ischar_of_word(char c){return isalpha(c) || c=='\'';} 61 bool ischar_of_word(char c){return isalpha(c) || c=='\'';}
61 62
62 byte end_kind(const string s){ 63 byte end_kind(const string s){
63 - byte endKind; 64 + byte endKind=NOT_AN_END;
64 int i=1; 65 int i=1;
65 //1st, let's consider all letters 66 //1st, let's consider all letters
66 if(!isalpha(s[0])) 67 if(!isalpha(s[0]))
@@ -71,16 +72,17 @@ byte end_kind(const string s){ @@ -71,16 +72,17 @@ byte end_kind(const string s){
71 while(islower(s[i]))i++; 72 while(islower(s[i]))i++;
72 } 73 }
73 else {//if isupper(s[0]) 74 else {//if isupper(s[0])
  75 + endKind=PROPER_END;
74 if(isalpha(s[1])){ 76 if(isalpha(s[1])){
75 - i++;  
76 if(islower(s[1])){ 77 if(islower(s[1])){
77 endKind=PROPER_END; 78 endKind=PROPER_END;
  79 + i=2;
78 while(islower(s[i]))i++; 80 while(islower(s[i]))i++;
79 } 81 }
80 else{//if isupper(s[1]) 82 else{//if isupper(s[1])
81 endKind=ACRONYME_END; 83 endKind=ACRONYME_END;
  84 + i=2;
82 while(isupper(s[i]))i++; 85 while(isupper(s[i]))i++;
83 - endKind=16;  
84 } 86 }
85 } 87 }
86 } 88 }