Blame view

treeh.c 2.06 KB
a4ef278c   bjeanlou   init withHash
1
2
  #include "treeh.h"
  
4043090f   bjeanlou   update2 withHash
3
  //initializers & destroyer
a637cab8   bjeanlou   update1 withHash
4
5
6
  tree make_empty_tree(){
    return NULL;
  }
4043090f   bjeanlou   update2 withHash
7
  node* make_empty_node(){
2c6f0266   bjeanlou   load and print OK
8
    //<=> make_node('\0',NOT_AN_END)
4043090f   bjeanlou   update2 withHash
9
10
    node*n=malloc(sizeof(node));
    n->letter='\0';
08822d77   bjeanlou   Update 9 withHash
11
    n->isEnd=NOT_AN_END;
4043090f   bjeanlou   update2 withHash
12
13
    for(int i=0;i<NBCHAR;i++)
      n->next[i]=make_empty_tree();
f48297ea   bjeanlou   Update1 treeh
14
  	return n;
4043090f   bjeanlou   update2 withHash
15
  }
fd1ee590   bjeanlou   Update7 withHash
16
  node* make_node(char l,byte end){
4043090f   bjeanlou   update2 withHash
17
18
19
20
21
    node*n=malloc(sizeof(node));
    n->letter=l;
    n->isEnd=end;
    for(int i=0;i<NBCHAR;i++)
      n->next[i]=make_empty_tree();
a89bb625   bjeanlou   Update 8 withHash
22
    return n;
4043090f   bjeanlou   update2 withHash
23
  }
ccb47d03   bjeanlou   menu quit ok
24
25
  void delete_tree(tree *t){
    if(is_empty(*t))return;
4043090f   bjeanlou   update2 withHash
26
    for(int i=0;i<NBCHAR;i++)
ccb47d03   bjeanlou   menu quit ok
27
28
      delete_tree(&((*t)->next[i]));
    free(*t);
b2638a8a   bjeanlou   création rapport
29
    *t=NULL;
a637cab8   bjeanlou   update1 withHash
30
  }
a637cab8   bjeanlou   update1 withHash
31
  
4043090f   bjeanlou   update2 withHash
32
33
  
  
435232c3   bjeanlou   update4 withHash
34
35
  
  
4043090f   bjeanlou   update2 withHash
36
  //Casual functions
f48297ea   bjeanlou   Update1 treeh
37
  bool is_empty(const tree t){
4043090f   bjeanlou   update2 withHash
38
39
    return t==NULL;
  }
f48297ea   bjeanlou   Update1 treeh
40
  bool is_followed(const tree t){
a637cab8   bjeanlou   update1 withHash
41
42
43
    int i;
    for(i=0;i<NBCHAR;i++){
      if(t->next[i]!=NULL)
4043090f   bjeanlou   update2 withHash
44
        return true;
a637cab8   bjeanlou   update1 withHash
45
    }
4043090f   bjeanlou   update2 withHash
46
    return false;
a637cab8   bjeanlou   update1 withHash
47
  }
de5faa60   bjeanlou   Update isEnd
48
  
f48297ea   bjeanlou   Update1 treeh
49
  //functions is_end
fd1ee590   bjeanlou   Update7 withHash
50
  bool is_end(const tree t){return t->isEnd;}
08822d77   bjeanlou   Update 9 withHash
51
52
  bool is_straight_end(const tree t){return (t->isEnd & STRAIGHT_END);}
  bool ends_with_apostrophe(const tree t){return (t->isEnd & APOSTROPHE_END);}
de5faa60   bjeanlou   Update isEnd
53
   
08822d77   bjeanlou   Update 9 withHash
54
55
56
  bool is_common_end(const tree t){return (t->isEnd & COMMON_END);}
  bool is_proper_end(const tree t){return (t->isEnd & PROPER_END);}
  bool is_acronyme_end(const tree t){return (t->isEnd & ACRONYME_END);}
f48297ea   bjeanlou   Update1 treeh
57
  
11c78e6d   bjeanlou   update5 withHash
58
59
60
61
62
63
64
  
  
  //needs to check c wether isalpha
  int hash(char c){return c%32-1;}
  bool ischar_of_word(char c){return isalpha(c) || c=='\'';}
  
  byte end_kind(const string s){
2c6f0266   bjeanlou   load and print OK
65
    byte endKind=NOT_AN_END;
11c78e6d   bjeanlou   update5 withHash
66
    int i=1;
a89bb625   bjeanlou   Update 8 withHash
67
    //1st, let's consider all letters
11c78e6d   bjeanlou   update5 withHash
68
    if(!isalpha(s[0]))
a89bb625   bjeanlou   Update 8 withHash
69
      return NOT_AN_END;
11c78e6d   bjeanlou   update5 withHash
70
71
    
    if(islower(s[0])){
a89bb625   bjeanlou   Update 8 withHash
72
      endKind=COMMON_END;
8c671959   bjeanlou   No more seg fault
73
      while(islower(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
74
75
    }
    else {//if isupper(s[0])
2c6f0266   bjeanlou   load and print OK
76
      endKind=PROPER_END;
11c78e6d   bjeanlou   update5 withHash
77
      if(isalpha(s[1])){
a89bb625   bjeanlou   Update 8 withHash
78
79
        if(islower(s[1])){
  	endKind=PROPER_END;
2c6f0266   bjeanlou   load and print OK
80
  	i=2;
8c671959   bjeanlou   No more seg fault
81
  	while(islower(s[i]))i++;
a89bb625   bjeanlou   Update 8 withHash
82
        }
11c78e6d   bjeanlou   update5 withHash
83
        else{//if isupper(s[1])
a89bb625   bjeanlou   Update 8 withHash
84
  	endKind=ACRONYME_END;
2c6f0266   bjeanlou   load and print OK
85
  	i=2;
8c671959   bjeanlou   No more seg fault
86
  	while(isupper(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
87
88
89
        }
      }
    }
a89bb625   bjeanlou   Update 8 withHash
90
    //then let's consider the end
08822d77   bjeanlou   Update 9 withHash
91
92
93
94
95
96
    if(s[i]=='\0'){
      return (endKind & STRAIGHT_END);
    }
    if(s[i]=='\'' && s[i+1]=='s' && s[i+2]=='\0'){
      return (endKind & APOSTROPHE_END);
    }
a89bb625   bjeanlou   Update 8 withHash
97
    return NOT_AN_END;
a637cab8   bjeanlou   update1 withHash
98
  }
11c78e6d   bjeanlou   update5 withHash
99
  bool is_word(const byte endKind){
fd1ee590   bjeanlou   Update7 withHash
100
    return endKind!=0;
11c78e6d   bjeanlou   update5 withHash
101
  }