Blame view

treeh.c 2.12 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
  //Casual functions
f48297ea   bjeanlou   Update1 treeh
33
  bool is_empty(const tree t){
4043090f   bjeanlou   update2 withHash
34
35
    return t==NULL;
  }
f48297ea   bjeanlou   Update1 treeh
36
  bool is_followed(const tree t){
a637cab8   bjeanlou   update1 withHash
37
38
39
    int i;
    for(i=0;i<NBCHAR;i++){
      if(t->next[i]!=NULL)
4043090f   bjeanlou   update2 withHash
40
        return true;
a637cab8   bjeanlou   update1 withHash
41
    }
4043090f   bjeanlou   update2 withHash
42
    return false;
a637cab8   bjeanlou   update1 withHash
43
  }
de5faa60   bjeanlou   Update isEnd
44
  
f48297ea   bjeanlou   Update1 treeh
45
  //functions is_end
fd1ee590   bjeanlou   Update7 withHash
46
  bool is_end(const tree t){return t->isEnd;}
08822d77   bjeanlou   Update 9 withHash
47
48
  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
49
   
08822d77   bjeanlou   Update 9 withHash
50
51
52
  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
53
  
11c78e6d   bjeanlou   update5 withHash
54
55
  
  
fb520d8f   bjeanlou   v1 ended
56
57
58
59
  //functions not directly related to the structure
  int hash(char c){return c%32-1;}//needs to check c wether isalpha
  bool ischar_of_word(char c){return (c=='\'' || isalpha(c));}
  bool is_word(const byte endKind){return endKind!=NOT_AN_END;}
11c78e6d   bjeanlou   update5 withHash
60
61
  
  byte end_kind(const string s){
2c6f0266   bjeanlou   load and print OK
62
    byte endKind=NOT_AN_END;
11c78e6d   bjeanlou   update5 withHash
63
    int i=1;
a89bb625   bjeanlou   Update 8 withHash
64
    //1st, let's consider all letters
11c78e6d   bjeanlou   update5 withHash
65
    if(!isalpha(s[0]))
a89bb625   bjeanlou   Update 8 withHash
66
      return NOT_AN_END;
11c78e6d   bjeanlou   update5 withHash
67
68
    
    if(islower(s[0])){
a89bb625   bjeanlou   Update 8 withHash
69
      endKind=COMMON_END;
8c671959   bjeanlou   No more seg fault
70
      while(islower(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
71
72
    }
    else {//if isupper(s[0])
2c6f0266   bjeanlou   load and print OK
73
      endKind=PROPER_END;
11c78e6d   bjeanlou   update5 withHash
74
      if(isalpha(s[1])){
a89bb625   bjeanlou   Update 8 withHash
75
76
        if(islower(s[1])){
  	endKind=PROPER_END;
2c6f0266   bjeanlou   load and print OK
77
  	i=2;
8c671959   bjeanlou   No more seg fault
78
  	while(islower(s[i]))i++;
a89bb625   bjeanlou   Update 8 withHash
79
        }
11c78e6d   bjeanlou   update5 withHash
80
        else{//if isupper(s[1])
a89bb625   bjeanlou   Update 8 withHash
81
  	endKind=ACRONYME_END;
2c6f0266   bjeanlou   load and print OK
82
  	i=2;
8c671959   bjeanlou   No more seg fault
83
  	while(isupper(s[i]))i++;
11c78e6d   bjeanlou   update5 withHash
84
85
86
        }
      }
    }
a89bb625   bjeanlou   Update 8 withHash
87
    //then let's consider the end
08822d77   bjeanlou   Update 9 withHash
88
89
90
91
92
93
    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
94
    return NOT_AN_END;
a637cab8   bjeanlou   update1 withHash
95
  }