Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 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 <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include "dictionary.h"
  16.  
  17. typedef struct node
  18. {
  19. struct node* next;
  20. char str[LENGTH+1];
  21. }
  22. node;
  23.  
  24. #define BUCKETS 1000
  25.  
  26. node* table;
  27.  
  28. /**
  29. * Returns true if word is in dictionary else false.
  30. */
  31. bool check(const char* word)
  32. {
  33. // TODO
  34. int wordhash = hash(word, strlen(word));
  35. node* current = table + wordhash;
  36. //printf("HASH: %i WORD: %s\n", wordhash, word);
  37. while(streql(current->str, word) == false)
  38. {
  39. //printf(" TESTING: %s", current->str);
  40. if(current->next == NULL)
  41. {
  42. return false;
  43. }
  44. current = current->next;
  45. }
  46. return true;
  47. }
  48.  
  49. /**
  50. * Loads dictionary into memory. Returns true if successful else false.
  51. */
  52. bool load(const char* dictionary)
  53. {
  54. // TODO
  55. FILE* dict = fopen(dictionary, "r");
  56. char* in;
  57. table = calloc(BUCKETS, sizeof(node));
  58. int i = 0;
  59. in = calloc(LENGTH+1, sizeof(char));
  60. while(fgets(in, LENGTH+1, dict))
  61. {
  62. int strhash = hash(in, strlen(in) - 1);
  63. node* add = table + strhash;
  64.  
  65. if(add->str[0] != '\0')
  66. {
  67. while(1)
  68. {
  69. if(add->next != NULL)
  70. {
  71. add = add->next;
  72. }
  73. else
  74. {
  75. add->next = calloc(1, sizeof(node));
  76. add = add->next;
  77. break;
  78. }
  79. }
  80. }
  81.  
  82. for(i = 0; i < strlen(in) && in[i] != '\n'; i++)
  83. {
  84. add->str[i] = toupper(in[i]);
  85. }
  86. for(; i <= LENGTH; i++)
  87. {
  88. add->str[i] = '\0';
  89. }
  90. //memcpy(table + hash(in, strsize(in) - 1), in, strsize(in));
  91. //(table + hash(in, strsize(in) - 1))->str = in;
  92. //table[i].str = in;
  93. //printf("%i %s\n", strhash, add->str);
  94. }
  95. for(int i = 0; i < BUCKETS; i++)
  96. {
  97. node* current = table + i;
  98. //printf("%i ", i);
  99. while(current != NULL)
  100. {
  101. //printf("%s ", current->str);
  102. current = current->next;
  103. }
  104. //printf("\n");
  105. }
  106. free(in);
  107. fclose(dict);
  108. return true;
  109. }
  110.  
  111. /**
  112. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  113. */
  114. unsigned int size(void)
  115. {
  116. int count = 0;
  117. /* for(int i = 0; i < BUCKETS; i++)*/
  118. /* {*/
  119. /* node* current = table + i;*/
  120. /* while(current != NULL)*/
  121. /* {*/
  122. /* i++;*/
  123. /* current = current->next;*/
  124. /* }*/
  125. /* }*/
  126. return count;
  127. }
  128.  
  129. /**
  130. * Unloads dictionary from memory. Returns true if successful else false.
  131. */
  132. bool unload(void)
  133. {
  134. // TODO
  135. for(int i = 0; i < BUCKETS; i++)
  136. {
  137. node* clear = table + i;
  138. printf("%i\n", i);
  139. while(clear != NULL)
  140. {
  141. node* temp = clear;
  142. clear = clear->next;
  143. free(temp);
  144. }
  145. }
  146. free(table);
  147. return true;
  148. }
  149.  
  150. int hash(const char* s, int length)
  151. {
  152. int sum = 0;
  153. for(int i = 0; i < length; i++)
  154. {
  155. sum += toupper(s[i]);
  156. }
  157. return sum % BUCKETS;
  158. }
  159.  
  160. int strsize(const char* s)
  161. {
  162. /* char* temp = s; */
  163. /* while(*(temp++));*/
  164. /* return (temp-s) - 1; */
  165. /* */
  166. /* char* i = &s;*/
  167. /* //for(int i = &s; *(s + i) != '\0'; i++);*/
  168. /* while(*i != '\0')*/
  169. /* {*/
  170. /* i++;*/
  171. /* }*/
  172. /* return (i - s);*/
  173. return 0;
  174. }
  175.  
  176. bool streql(const char* a, const char* b)
  177. {
  178. for(int i = 0; a[i] != '\0' && b[i] != '\0'; i++)
  179. {
  180. if(toupper(a[i])!=toupper(b[i]))
  181. {
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement