Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. /**
  2. * Implements a dictionary's functionality.
  3. */
  4.  
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #include "dictionary.h"
  12.  
  13.  
  14. // own functions and variables
  15. int counter = 0;
  16.  
  17. // amount of children for the node
  18. #define amount_children 27
  19.  
  20. // Struct for the node
  21. typedef struct node
  22. {
  23. bool is_word;
  24.  
  25. struct node* children[amount_children];
  26. }
  27. node;
  28.  
  29. // root declared as a global variabel
  30. node* root = NULL;
  31.  
  32. // function for unloading using recursion
  33. bool uload(node*ptr);
  34.  
  35. /**
  36. * Returns true if word is in dictionary else false.
  37. */
  38. bool check(const char *word)
  39. {
  40. node * ptr = root;
  41.  
  42. for (int i=0; i<=strlen(word); i++)
  43. {
  44.  
  45. if (word[i]=='\0' && ptr->is_word==true)
  46. return true;
  47.  
  48. if (islower(word[i]))
  49. {
  50. if (ptr->children[word[i]-'a'+1] == NULL)
  51. return false;
  52.  
  53. ptr = ptr->children[word[i]-'a'+1];
  54. }
  55.  
  56. if (isupper(word[i]))
  57. {
  58. if (ptr->children[word[i]-'A'+1] == NULL)
  59. return false;
  60.  
  61. ptr = ptr->children[word[i]-'A'+1];
  62. }
  63.  
  64. if (word[i]=='a'-1)
  65. {
  66. if (ptr->children[0] == NULL)
  67. return false;
  68.  
  69. ptr = ptr->children[word[i]-'a'+1];
  70. }
  71.  
  72. /*
  73. if (islower(word[i]) && ptr->children[word[i]-'a'+1] == NULL)
  74. return false;
  75.  
  76. if (isupper(word[i]) && ptr->children[word[i]-'B'] == NULL)
  77. return false;
  78.  
  79. if (word[i]=='a'-1 && ptr->children[word[i]-'a'+1] == NULL)
  80. return false;
  81.  
  82. if (islower(word[i]))
  83. ptr = ptr->children[word[i]-'a'+1];
  84.  
  85. if (isupper(word[i]))
  86. ptr = ptr->children[word[i]-'a'+1];
  87.  
  88. if (word[i]=='a'-1)
  89. ptr = ptr->children[word[i]-'a'+1];
  90. */
  91. }
  92. return false;
  93. }
  94.  
  95. /**
  96. * Loads dictionary into memory. Returns true if successful else false.
  97. */
  98. bool load(const char *dictionary)
  99. {
  100. FILE* input = fopen(dictionary, "r");
  101.  
  102. if (input==NULL)
  103. return false;
  104.  
  105. char word[LENGTH+1];
  106.  
  107. root = calloc(1, sizeof(node));
  108. root->is_word=false;
  109.  
  110.  
  111. int n = 0;
  112.  
  113. while (true)
  114. {
  115. memset(word, '\0', sizeof(word));
  116. fscanf(input, "%s", word);
  117.  
  118.  
  119. if (word[0]=='\0')
  120. return true;
  121.  
  122. node *ptr = root;
  123.  
  124. for (int i=0; i<=strlen(word); i++)
  125. {
  126. if (word[i]=='\0')
  127. {
  128. ptr->is_word = true;
  129. break;
  130. }
  131. n = word[i]-'a'+1;
  132.  
  133. if (ptr->children[n]==NULL)
  134. {
  135. ptr->children[n] = calloc(1, sizeof(node));
  136. }
  137.  
  138. ptr = ptr->children[n];
  139.  
  140. }
  141.  
  142. counter++;
  143. }
  144.  
  145. }
  146.  
  147. /**
  148. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  149. */
  150. unsigned int size(void)
  151. {
  152. if (counter>0)
  153. return counter;
  154.  
  155. else
  156. return 0;
  157. }
  158.  
  159. /**
  160. * Unloads dictionary from memory. Returns true if successful else false.
  161. */
  162. bool unload(void)
  163. {
  164. return uload(root);
  165. }
  166.  
  167.  
  168. bool uload(struct node *ptr)
  169. {
  170. for (int i = 0; i < amount_children; i++)
  171. {
  172. if(ptr->children[i]!=NULL)
  173. uload(ptr->children[i]);
  174. }
  175. free(ptr);
  176. return true;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement