Advertisement
Guest User

Untitled

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