Commit a89bb625e2ac106e19f35cb7f5e74ba16b2b7bf4

Authored by bjeanlou
1 parent fd1ee590

Update 8 withHash

Showing 6 changed files with 105 additions and 27 deletions   Show diff stats
@@ -14,41 +14,53 @@ void delete_dico(dico d){ @@ -14,41 +14,53 @@ void delete_dico(dico d){
14 bool addto_dico(dico d,const string s){ 14 bool addto_dico(dico d,const string s){
15 byte endKind=end_kind(s); 15 byte endKind=end_kind(s);
16 if(!is_word(endKind)){ 16 if(!is_word(endKind)){
17 - printf("incorrect word"); 17 + printf("incorrect word\n");
18 return true; 18 return true;
19 } 19 }
20 - if(is_empty(d[hash(s[0])])){  
21 - d[hash(s[0])]=make_node(s[0],0);  
22 - addto_tree2(d[hash(s[0])],s+1,endKind);  
23 - return false;  
24 - }  
25 - else {  
26 - return addto_tree(d[hash(s[0])],s+1,endKind);  
27 - } 20 + return addto_treeIT(&(d[hash(s[0])]),s,endKind);
  21 +
28 } 22 }
29 23
30 void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} 24 void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
31 void loadfrom_file(dico d,FILE*stream){ 25 void loadfrom_file(dico d,FILE*stream){
32 if(stream==NULL){ 26 if(stream==NULL){
33 - printf("sorry, we can't open the file"); 27 + printf("sorry, we can't open the file\n");
34 return; 28 return;
35 } 29 }
36 char word[30]={0}; 30 char word[30]={0};
37 - while(fscanf(stream,"%s",word)!=EOF) 31 + while(fgets(word,30,stream)!=NULL){
  32 + if(word[0]=='\n')break;
  33 + printf("ok. next :");
  34 + fflush(stdout);
38 addto_dico(d,word); 35 addto_dico(d,word);
39 - printf("load success"); 36 + }
  37 + printf("load success\n");
40 } 38 }
41 39
  40 +
  41 +
  42 +
  43 +
  44 +
  45 +
  46 +
  47 +
  48 +
  49 +
  50 +
  51 +
  52 +
42 void printto_terminal(dico d){printto_file(d,stdout);} 53 void printto_terminal(dico d){printto_file(d,stdout);}
43 void printto_file(dico d,FILE*stream){ 54 void printto_file(dico d,FILE*stream){
44 if(stream==NULL){ 55 if(stream==NULL){
45 - printf("sorry, we can't open the file"); 56 + printf("sorry, we can't open the file\n");
46 return; 57 return;
47 } 58 }
48 if(d==NULL){ 59 if(d==NULL){
49 - printf("sorry, we can't open the dictionary"); 60 + printf("sorry, we can't open the dictionary\n");
50 return; 61 return;
51 } 62 }
  63 + printf("This is what the dictionnary contains :\n");
52 for(int i=0;i<NBCHAR;i++){ 64 for(int i=0;i<NBCHAR;i++){
53 print(d[i],stream,""); 65 print(d[i],stream,"");
54 } 66 }
@@ -115,7 +127,7 @@ bool is_in(dico d,string word){ @@ -115,7 +127,7 @@ bool is_in(dico d,string word){
115 return false; 127 return false;
116 } 128 }
117 tree tmp=d[hash(word[0])]; 129 tree tmp=d[hash(word[0])];
118 - for(int i=1;word[i]!='\0' && word[i]!='\'' && !is_empty(tmp);i++){ 130 + for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){
119 tmp=tmp->next[hash(word[i])]; 131 tmp=tmp->next[hash(word[i])];
120 } 132 }
121 return !is_empty(tmp) && (endKind & is_end(tmp)); 133 return !is_empty(tmp) && (endKind & is_end(tmp));
@@ -11,10 +11,10 @@ bool addto_dico(dico,const string); @@ -11,10 +11,10 @@ bool addto_dico(dico,const string);
11 11
12 12
13 void loadfrom_file(dico,FILE*); 13 void loadfrom_file(dico,FILE*);
14 -void loadfrom_keyboard(dico);//i.e. loadfrom_file(dico,stdin); 14 +void loadfrom_keyboard(dico);
15 15
16 void printto_file(dico,FILE*); 16 void printto_file(dico,FILE*);
17 -void printto_terminal(dico);//i.e. printto_file(dico,stdout); 17 +void printto_terminal(dico);
18 void print(tree,FILE*,string); 18 void print(tree,FILE*,string);
19 19
20 void strupper(string str); 20 void strupper(string str);
essai.txt 0 โ†’ 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +hello
  2 +Hello
  3 +gangbang
  4 +goodbye
  5 +
  6 +hon
main.c 0 โ†’ 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +#include "dico.h"
  2 +
  3 +
  4 +
  5 +
  6 +int main(int argc, char* argv[]){
  7 + if(argc<2){
  8 + printf("not enough arguments\n");
  9 + return EXIT_FAILURE;
  10 + }
  11 + FILE*f=fopen(argv[1],"r");
  12 + if(f==NULL){
  13 + printf("wrong arguments\n");
  14 + return EXIT_FAILURE;
  15 + }
  16 +
  17 + printf("hello world\n");
  18 + dico monDico;
  19 + make_empty_dico(monDico);
  20 + loadfrom_file(monDico,f);
  21 + printto_terminal(monDico);
  22 + delete_dico(monDico);
  23 + return EXIT_SUCCESS;
  24 +}
@@ -18,7 +18,7 @@ node* make_node(char l,byte end){ @@ -18,7 +18,7 @@ node* make_node(char l,byte end){
18 n->isEnd=end; 18 n->isEnd=end;
19 for(int i=0;i<NBCHAR;i++) 19 for(int i=0;i<NBCHAR;i++)
20 n->next[i]=make_empty_tree(); 20 n->next[i]=make_empty_tree();
21 - return n; 21 + return n;
22 } 22 }
23 void delete_tree(tree t){ 23 void delete_tree(tree t){
24 if(is_empty(t))return; 24 if(is_empty(t))return;
@@ -60,29 +60,36 @@ int hash(char c){return c%32-1;} @@ -60,29 +60,36 @@ int hash(char c){return c%32-1;}
60 bool ischar_of_word(char c){return isalpha(c) || c=='\'';} 60 bool ischar_of_word(char c){return isalpha(c) || c=='\'';}
61 61
62 byte end_kind(const string s){ 62 byte end_kind(const string s){
63 - byte endKind=0; 63 + byte endKind;
64 int i=1; 64 int i=1;
  65 + //1st, let's consider all letters
65 if(!isalpha(s[0])) 66 if(!isalpha(s[0]))
66 - return 0; 67 + return NOT_AN_END;
67 68
68 if(islower(s[0])){ 69 if(islower(s[0])){
69 - endKind=1; 70 + endKind=COMMON_END;
70 while(islower(s[i])); 71 while(islower(s[i]));
71 } 72 }
72 else {//if isupper(s[0]) 73 else {//if isupper(s[0])
73 - endKind=4;  
74 if(isalpha(s[1])){ 74 if(isalpha(s[1])){
75 i++; 75 i++;
76 - if(islower(s[1])) 76 + if(islower(s[1])){
  77 + endKind=PROPER_END;
77 while(islower(s[i])); 78 while(islower(s[i]));
  79 + }
78 else{//if isupper(s[1]) 80 else{//if isupper(s[1])
  81 + endKind=ACRONYME_END;
79 while(isupper(s[i])); 82 while(isupper(s[i]));
80 endKind=16; 83 endKind=16;
81 } 84 }
82 } 85 }
83 } 86 }
84 - endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') );  
85 - return endKind; 87 + //then let's consider the end
  88 + if(s[i]=='\0')
  89 + return endKind & STRAIGHT_END;
  90 + if(s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0')
  91 + return endKind & APOSTROPHE_END;
  92 + return NOT_AN_END;
86 } 93 }
87 bool is_word(const byte endKind){ 94 bool is_word(const byte endKind){
88 return endKind!=0; 95 return endKind!=0;
@@ -120,3 +127,30 @@ void addto_tree2(tree t,const string s,char endKind){ @@ -120,3 +127,30 @@ void addto_tree2(tree t,const string s,char endKind){
120 addto_tree2(t->next[hash(s[0])],s+1,endKind); 127 addto_tree2(t->next[hash(s[0])],s+1,endKind);
121 } 128 }
122 129
  130 +
  131 +bool addto_treeIT(tree*t,const string word ,byte endKind){
  132 + bool isIn;
  133 + int i;
  134 + //1st, let's go through the tree, until the word is not in tree
  135 + for(i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(*t) ;i++){
  136 + t=&((*t)->next[hash(word[i])]);
  137 + }
  138 + printf("hi\n");
  139 + //if word is not ended at the end of the tree
  140 + if(is_empty(*t)){
  141 + isIn=false;
  142 + for(; word[i]!='\0' && word[i]!='\'';i++){
  143 + *t=make_node(tolower(word[i]),NOT_AN_END);
  144 + t=&((*t)->next[hash(word[i])]);
  145 + }
  146 + (*t)->isEnd=endKind;
  147 + printf("actions ended\n");
  148 + }
  149 + //if word is ended
  150 + else{
  151 + isIn=endKind & is_end(*t);
  152 + (*t)->isEnd=endKind | is_end(*t);
  153 + printf("action ended\n");
  154 + }
  155 + return isIn;
  156 +}
@@ -9,11 +9,12 @@ @@ -9,11 +9,12 @@
9 9
10 #define NBCHAR 26 //A-Z 10 #define NBCHAR 26 //A-Z
11 11
  12 +#define NOT_AN_END 0x00
12 #define COMMON_END 0x03 13 #define COMMON_END 0x03
13 #define PROPER_END 0x0B 14 #define PROPER_END 0x0B
14 #define ACRONYME_END 0x30 15 #define ACRONYME_END 0x30
15 -#define STRAIGHT_END 0X15  
16 -#define APOSTROPHE_END 0X2A 16 +#define STRAIGHT_END 0x15
  17 +#define APOSTROPHE_END 0x2A
17 18
18 typedef char *string,byte; 19 typedef char *string,byte;
19 typedef struct _node node, *tree; 20 typedef struct _node node, *tree;
@@ -48,6 +49,7 @@ byte end_kind(string); @@ -48,6 +49,7 @@ byte end_kind(string);
48 //recursive can only be called in a addto_dico 49 //recursive can only be called in a addto_dico
49 bool addto_tree(tree,const string,byte); 50 bool addto_tree(tree,const string,byte);
50 void addto_tree2(tree,const string,byte); 51 void addto_tree2(tree,const string,byte);
  52 +bool addto_treeIT(tree*,const string,byte);
51 53
52 54
53 #endif //TREEH_H 55 #endif //TREEH_H