Commit 11c78e6dbe00757abed06cbba83d8313e957d936

Authored by bjeanlou
1 parent 62535895

update5 withHash

Showing 4 changed files with 84 additions and 41 deletions   Show diff stats
@@ -9,33 +9,50 @@ void delete_dico(dico d){ @@ -9,33 +9,50 @@ void delete_dico(dico d){
9 delete_tree(d[i]); 9 delete_tree(d[i]);
10 } 10 }
11 11
12 -byte end_kind(const string s){  
13 - byte endKind=0;  
14 - int i=1;  
15 - if(!isalpha(s[0]))  
16 - return 0;  
17 - if(islower(s[0])){  
18 - endKind=1;  
19 - while(islower(s[i])); 12 +
  13 +
  14 +bool addto_dico(dico d,string s){
  15 + byte endKind=end_kind(s);
  16 + if(!is_word(endKind)){
  17 + printf("incorrect word");
  18 + return true;
20 } 19 }
21 - else {//if isupper(s[0])  
22 - endKind=2;  
23 - if(!isalpha(s[1])){  
24 -  
25 - } 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);
26 } 27 }
27 - endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') );  
28 - return endKind;  
29 } 28 }
30 29
31 -bool is_word(const byte endKind){  
32 - return end_Kind!=0; 30 +bool lire(string s,FILE*stream){
  31 + //need to check if stream !=NULL
  32 + //ret true if error else false
  33 + char* posRet=NULL;
  34 + if (fscanf(stream,"%s",s) != NULL){
  35 + posRet=strchr(chaine, '\n');
  36 + if (posRet!= NULL){
  37 + *posRet = '\0';
  38 + }
  39 + return false;
  40 + }
  41 + else
  42 + return true;
33 } 43 }
34 44
35 -bool addto_dico(dico d,string s){  
36 - if(!is_word(end_kind(s))){  
37 - printf("incorrect word");  
38 - return true; 45 +void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);}
  46 +void loadfrom_file(dico d,FILE*stream){
  47 + if(stream==NULL){
  48 + printf("sorry, we can't open the file");
  49 + return;
39 } 50 }
40 - 51 + string str=calloc(30*sizeof(char));
  52 + while(fscanf(""strstream)!=NULL)
  53 +}
  54 +
  55 +void printto_terminal(dico d){printto_file(dico,stdout);}
  56 +void printto_file(dico d,FILE*f){
  57 + ;
41 } 58 }
@@ -9,14 +9,12 @@ void make_empty_dico(dico); @@ -9,14 +9,12 @@ void make_empty_dico(dico);
9 void delete_dico(dico); 9 void delete_dico(dico);
10 bool addto_dico(dico,string); 10 bool addto_dico(dico,string);
11 11
12 -bool is_word(byte endKind);//pass end_kind() as parameter  
13 -byte end_kind(string);  
14 12
15 void loadfrom_file(dico,FILE*); 13 void loadfrom_file(dico,FILE*);
16 -void loadfrom_keyboard(dico); 14 +void loadfrom_keyboard(dico);//i.e. loadfrom_file(dico,stdin);
17 15
18 void printto_file(dico,FILE*); 16 void printto_file(dico,FILE*);
19 -void printto_terminal(dico); 17 +void printto_terminal(dico);//i.e. printto_file(dico,stdout);
20 18
21 19
22 #endif 20 #endif
@@ -45,17 +45,48 @@ bool is_followed(const tree t){ @@ -45,17 +45,48 @@ bool is_followed(const tree t){
45 } 45 }
46 46
47 //functions is_end 47 //functions is_end
48 -bool is_end(const tree t){return t->isEnd & 13;}//0b00010101  
49 -bool ends_with_apostrophe(const tree t){return t->isEnd & 26;}//0b00101010 48 +bool is_end(const tree t){return t->isEnd & 21;}//0b00010101
  49 +bool ends_with_apostrophe(const tree t){return t->isEnd & 42;}//0b00101010
50 50
51 bool is_common_end(const tree t){return t->isEnd & 3;}//0b00000011 51 bool is_common_end(const tree t){return t->isEnd & 3;}//0b00000011
52 bool is_proper_end(const tree t){return t->isEnd & 12;}//0b00001100 52 bool is_proper_end(const tree t){return t->isEnd & 12;}//0b00001100
53 bool is_acronyme_end(const tree t){return t->isEnd & 48;}//0b00110000 53 bool is_acronyme_end(const tree t){return t->isEnd & 48;}//0b00110000
54 54
55 -int hash(char c){  
56 - //needs to check c wether isalpha  
57 - return c%32-1; 55 +
  56 +
  57 +//needs to check c wether isalpha
  58 +int hash(char c){return c%32-1;}
  59 +bool ischar_of_word(char c){return isalpha(c) || c=='\'';}
  60 +
  61 +byte end_kind(const string s){
  62 + byte endKind=0;
  63 + int i=1;
  64 + if(!isalpha(s[0]))
  65 + return 0;
  66 +
  67 + if(islower(s[0])){
  68 + endKind=1;
  69 + while(islower(s[i]));
  70 + }
  71 + else {//if isupper(s[0])
  72 + endKind=4;
  73 + if(isalpha(s[1])){
  74 + i++;
  75 + if(islower(s[1]))
  76 + while(islower[i]);
  77 + else{//if isupper(s[1])
  78 + while(isupper(s[i]));
  79 + endKind=16;
  80 + }
  81 + }
  82 + }
  83 + endKind*=( (s[i]=='\0') + 2* (s[i]=='\''&&s[i+1]=='s'&&s[i+2]=='\0') );
  84 + return endKind;
58 } 85 }
  86 +bool is_word(const byte endKind){
  87 + return end_Kind!=0;
  88 +}
  89 +
59 90
60 91
61 92
@@ -64,17 +95,12 @@ bool addto_tree(tree t,const string s,char endKind){ @@ -64,17 +95,12 @@ bool addto_tree(tree t,const string s,char endKind){
64 //recursive, need to check all letter in s are alpha or "\'s" 95 //recursive, need to check all letter in s are alpha or "\'s"
65 //return wether s is already in t or not 96 //return wether s is already in t or not
66 bool ret; 97 bool ret;
67 - if(s[0]=='\0'){  
68 - ret=is_end(t); 98 + if(s[0]=='\0' || s[0]=='\''){
  99 + ret=t->isEnd & endKind;
69 t->isEnd=t->isEnd|endKind; 100 t->isEnd=t->isEnd|endKind;
70 return ret; 101 return ret;
71 } 102 }
72 - if(s[0]=='\''){  
73 - ret=ends_with_apostrophe(t);  
74 - t->isEnd=t->isEnd|endKind;  
75 - return ret;  
76 - }  
77 - if(t->next[hash(s[0])]==NULL){ 103 + if(is_empty(t->next[hash(s[0])])){
78 t->next[hash(s[0])]=make_node(s[0],0); 104 t->next[hash(s[0])]=make_node(s[0],0);
79 addto_tree2(t->next[hash(s[0])],s+1,endKind); 105 addto_tree2(t->next[hash(s[0])],s+1,endKind);
80 return false; 106 return false;
@@ -33,9 +33,11 @@ bool is_proper_end(const tree);//if true 1st letter is upper @@ -33,9 +33,11 @@ bool is_proper_end(const tree);//if true 1st letter is upper
33 bool is_acronyme_end(const tree);//if true all letters are upper 33 bool is_acronyme_end(const tree);//if true all letters are upper
34 34
35 int hash(char);//need to check if isalpha 35 int hash(char);//need to check if isalpha
  36 +bool ischar_of_word(char);//tells if char can be in a word
  37 +bool is_word(byte endKind);//pass end_kind() as parameter
  38 +byte end_kind(string);
36 39
37 -//recursive can only be called in a dico instance  
38 -// 40 +//recursive can only be called in a addto_dico
39 bool addto_tree(tree,const string,byte); 41 bool addto_tree(tree,const string,byte);
40 void addto_tree2(tree,const string,byte); 42 void addto_tree2(tree,const string,byte);
41 43