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