joseleeph

Untitled

Nov 18th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <cs50.h>
  7. #include "dictionary.h"
  8. #include <ctype.h>
  9.  
  10. typedef struct node
  11. {
  12. char word[LENGTH + 1];
  13.  
  14. struct node *next;
  15. }
  16. node;
  17. node *table[26*3];
  18.  
  19. bool check(const char *word)
  20. {
  21.  
  22. node *cursor = table[hash(word)];
  23. while (cursor != NULL)
  24. {
  25. if (strcasecmp(cursor->word, word) == false)
  26. {
  27. cursor = cursor->next;
  28. }
  29. else
  30. {
  31. return true;
  32. }
  33. }
  34.  
  35. return false;
  36. }
  37. unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
  38. {
  39. int val = 0;
  40. for (int i = 0; word[i] != '\0'; i++)
  41. {
  42. char c = word[i];
  43. val += c;
  44. }
  45. }
  46. bool load(const char *dictionary)
  47. {
  48. char word[LENGTH + 1]; // an array of characters
  49. FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
  50. if (dictr == NULL) // if there's nothing there
  51. {
  52. return false;
  53. }
  54. char buffer[LENGTH+1];
  55.  
  56. while (fscanf(dictr,"%s", buffer) == 1)
  57. /*
  58. scan strings one at a time from the pointer to the dictionary
  59. file in the form of a string
  60. while fscanf hasn't reached the end of the file
  61. */
  62. {
  63. fscanf(dictr,"%s",buffer);
  64. node *myword = malloc(sizeof(node));
  65. if (myword == NULL)
  66. {
  67. return false;
  68. }
  69. strcpy(myword->word,buffer);
  70. }
  71. return true;
  72. }
  73. unsigned int size(void)
  74. {
  75. int count = 0;
  76. int i = 0;
  77.  
  78. while (table[i]->next != NULL)
  79. /*
  80. if i remember correctly this doesn't work...
  81. how else am I to know how many words
  82. are in the linked list?
  83. */
  84.  
  85. {
  86. count++;
  87. i++;
  88. }
  89. if (count == 0)
  90. {
  91. return 0;
  92. }
  93.  
  94. else
  95. {
  96. return count;
  97. }
  98.  
  99. }
  100. bool unload(void)
  101. {
  102.  
  103. node *cursor = table[hash(0)];
  104. while (cursor->next != NULL)
  105. {
  106. node *tmp = cursor;
  107. cursor = cursor->next;
  108. free(tmp);
  109. }
  110. return false;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment