joseleeph

Untitled

Sep 30th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2. #include <stdio.h>
  3. #include <strings.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <cs50.h>
  8. #include "dictionary.h"
  9.  
  10. // Represents a node in a hash table
  11. typedef struct node
  12. {
  13. char word[LENGTH + 1];
  14. struct node *next;
  15. }
  16. node;
  17. const unsigned int N = 26;
  18.  
  19. //create a hash table, an array of hash values
  20.  
  21. node *table[N];
  22.  
  23. bool check(const char *word)
  24. {
  25.  
  26. //int hval = hash(word);
  27. //node *cursor = table[hval];
  28. node *cursor = table[hash(word)];
  29. while (cursor != NULL)
  30. {
  31. if (strcasecmp(cursor->word, word) == false)
  32. {
  33. cursor = cursor->next;
  34. }
  35. else
  36. {
  37. return true;
  38. }
  39. }
  40. //char *cursor = word[0];
  41. // TODO
  42. return false;
  43. }
  44.  
  45.  
  46.  
  47. // Hashes word to a number
  48. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  49. {
  50. int val = 0;
  51. //int cval;
  52. for (int i = 0; word[i] != '\0'; i++)
  53. {
  54. //val += word[i] - 97;
  55. //val += word[i] - 13; ... 13 is the value of '
  56. val += word[i];
  57. }
  58. // TODO
  59. return val%26;
  60. // maybe try a hash table that will use the first 3 letters
  61.  
  62. //return 0;
  63. }
  64. //char *name = "jacob";
  65. //int h = hash(name);
  66. //printf("name prints %s and the hash value of name is: %i\n",name,h);
  67.  
  68.  
  69. // Loads dictionary into memory, returning true if successful else false
  70. bool load(const char *dictionary)
  71. {
  72. // TODO
  73.  
  74. //node *table[N];
  75.  
  76. node *n = malloc(sizeof(node));
  77.  
  78. char word[LENGTH + 1];
  79. FILE *dictr = fopen(dictionary,"r");
  80. //FILE *dictr = fopen("dictionaries/large","r");
  81. //FILE *dictr = fopen(argv[1],"r");
  82. // open the file
  83. if (dictr == NULL)
  84. {
  85. return false;
  86. }
  87.  
  88. while (fscanf(dictr,"%s", word) != EOF)//read strings one at a time
  89. {
  90. fscanf(dictr,"%s",word);
  91. //printf("%s",word);
  92. }
  93. //fread(hash(dictr),sizeof(char),1,dictr);
  94.  
  95. // malloc?
  96. // int *dictr = malloc(3*sizeof(int));
  97.  
  98. if (dictr != NULL)
  99. {
  100. return true;
  101. }
  102. else
  103. return false;
  104.  
  105. char wordnow[LENGTH+1];
  106.  
  107. node *myword = NULL;
  108.  
  109. while (fscanf(dictr,"%s\n", wordnow) != EOF)
  110. {
  111. myword = malloc(sizeof(node));
  112. if (myword == NULL)
  113. {
  114. return false;
  115. }
  116. //strcpy((*myword).word, wordnow);
  117. strcpy(myword->word, wordnow);// copy wordnow to the word field of the myword node
  118. //(*myword).next = NULL;
  119. }
  120.  
  121. }
  122.  
  123. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  124. unsigned int size(void)
  125. {
  126. // TODO
  127. int count = 0;
  128. //for (int i = 0; table[i] != NULL;i++)
  129. int i = 0;
  130. //while (table[i] != NULL)
  131. while (table[i]->next != NULL)
  132. {
  133. count++;
  134. i++;
  135. }
  136. if (count == 0)
  137. {
  138. return 0;
  139. }
  140.  
  141. else
  142. {
  143. return count;
  144. }
  145.  
  146. }
  147.  
  148. // Unloads dictionary from memory, returning true if successful else false
  149. bool unload(void)
  150. {
  151. //int i = 0;
  152. node *cursor = table[hash(0)];
  153. //node *tmp = table[hash(i)];
  154. //node *tmp = cursor;?
  155. //while (table[i]->next != NULL)
  156. while (cursor->next != NULL)
  157. {
  158. node *tmp = cursor;
  159. cursor = cursor->next;
  160. free(tmp);
  161. }
  162. return false;
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment