Advertisement
tkamiten

dictinary.c

May 3rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 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 <stdbool.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "dictionary.h"
  17.  
  18. // linked list for words
  19. typedef struct node
  20. {
  21. char word[46];
  22. struct node* next;
  23. }
  24. node;
  25.  
  26. node* hashtable[19683];
  27.  
  28. // numbers of words loaded from dictionary
  29. int dic_num = 0;
  30.  
  31. /**
  32. * Returns true if word is in dictionary else false.
  33. */
  34. bool check(const char* word)
  35. {
  36. char l_word[46];
  37. double num = 0;
  38. int idx = 0;
  39.  
  40. for(int i = 0; i < 46; i++)
  41. {
  42. if(word[i] == '\0')
  43. {
  44. l_word[i] = '\0';
  45. break;
  46. }
  47.  
  48. else
  49. {
  50. if(isalpha(word[i]) != 0 || word[i] == 39)
  51. l_word[i] = tolower(word[i]);
  52. }
  53. }
  54.  
  55. for(int i = 0; i < 4; i++)
  56. {
  57. if(l_word[i] == '\0')
  58. break;
  59. else
  60. num += pow (27.0, (4.0 - i - 1))*(1 + l_word[i] - 'a');
  61. }
  62.  
  63. idx = (int)num % 19683;
  64.  
  65. node* n_ptr;
  66.  
  67. for(n_ptr = hashtable[idx]; n_ptr != NULL; n_ptr = n_ptr->next)
  68. {
  69. if(strcmp(l_word, n_ptr->word) == 0)
  70. return true;
  71.  
  72. }
  73. return false;
  74. }
  75.  
  76. /**
  77. * Loads dictionary into memory. Returns true if successful else false.
  78. */
  79. bool load(const char* dictionary)
  80. {
  81.  
  82.  
  83. // initialize hashtable
  84. for(int i = 0; i < 19683; i++)
  85. hashtable[i] = NULL;
  86.  
  87.  
  88. // file pointer to dictionary
  89. FILE *fptr;
  90.  
  91. // node pointer
  92. node *n;
  93.  
  94. fptr = fopen(dictionary,"r");
  95.  
  96. while(fgetc(fptr) != EOF)
  97. {
  98. node* new_node = malloc(sizeof(node));
  99. new_node->next = NULL;
  100.  
  101. fscanf(fptr, "%s", new_node->word);
  102.  
  103. double num = 0;
  104.  
  105. for(int i = 0; i < 4; i++)
  106. {
  107. if(new_node->word[i] == '\0')
  108. break;
  109. else
  110. num += pow (27.0, (4.0 - i - 1))*(1 + new_node->word[i] - 'a');
  111. }
  112.  
  113. int index = (int)num % 19683;
  114.  
  115. if (hashtable[index] == NULL)
  116. {
  117. hashtable[index] = new_node;
  118. dic_num++;
  119. }
  120. else
  121. {
  122. // find end of linked list
  123. for(n = hashtable[index]; n != NULL; n = n-> next)
  124. {
  125. if(n -> next == NULL)
  126. {
  127. n->next = new_node;
  128. dic_num++;
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136.  
  137. /**
  138. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  139. */
  140. unsigned int size(void)
  141. {
  142. if(dic_num == 0)
  143. return 0;
  144. else
  145. return dic_num;
  146. }
  147.  
  148. /**
  149. * Unloads dictionary from memory. Returns true if successful else false.
  150. */
  151. bool unload(void)
  152. {
  153. for(int i = 0; i < 19683; i++)
  154. {
  155. node* cursor = hashtable[i];
  156.  
  157. while(cursor != NULL)
  158. {
  159. node* temp = cursor;
  160. cursor = cursor->next;
  161. free(temp);
  162. }
  163. }
  164. return true;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement