Advertisement
rushk

Untitled

Oct 3rd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int i;
  7. FILE *dict;
  8. typedef struct node
  9. {
  10. bool is_word;
  11. struct node *children[27];
  12. }
  13. node;
  14.  
  15. node *root;
  16. // Loads dictionary into memory, returning true if successful else false
  17. bool load(const char *dictionary)
  18. {
  19. // open the dictionary
  20. dict = fopen(dictionary, "r");
  21. if (!dict)
  22. {
  23. fprintf(stderr, "Could not open\n");
  24. return 2;
  25. }
  26.  
  27. root = malloc(sizeof(node));
  28. root->is_word = false;
  29.  
  30. node *current_node;
  31. current_node = root;
  32.  
  33. char s[45];
  34. // go through every list in dictionary
  35. for(i=0; fgets(s, 45, dict) != NULL; i++)
  36. {
  37. current_node = root;
  38. // find every letter of each word
  39. for(int j=0, n = strlen(s); j < n - 1; j++)
  40. {
  41. // turn the character to ascii
  42. char character = s[j];
  43. int ascii = 0;
  44. if(character >= 'a' && character <= 'z')
  45. {
  46. ascii = character - 97;
  47. }
  48. else if(character == '\'')
  49. {
  50. ascii = character - 13;
  51. }
  52. // if the char doesnt exist then make a new node
  53. if (current_node->children[ascii] == NULL)
  54. {
  55. node *new_node = malloc(sizeof(node));
  56. current_node->children[ascii] = new_node;
  57. current_node = new_node;
  58. }
  59. // if char exists then point current_node to the new node
  60. else if (current_node->children[ascii] != NULL)
  61. {
  62. current_node = current_node->children[ascii];
  63. }
  64. }
  65. // at the end of every word say that current_node is a word (because you got to the end of a word)
  66. current_node->is_word = true;
  67. }
  68. return true;
  69. }
  70.  
  71. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  72. unsigned int size(void)
  73. {
  74. return i;
  75. }
  76.  
  77. // Returns true if word is in dictionary else false
  78. bool check(const char *word)
  79. {
  80. node *temp;
  81. temp = root;
  82. // go over every char of the word
  83. for (int k = 0, length = strlen(word); k < length; k++)
  84. {
  85. char lower_case_char = tolower(word[k]);
  86. int ascii = 0;
  87.  
  88. if(lower_case_char >= 'a' && lower_case_char <= 'z')
  89. {
  90. ascii = lower_case_char - 97;
  91. }
  92. else if(lower_case_char == '\'')
  93. {
  94. ascii = lower_case_char - 13;
  95. }
  96.  
  97. // if the character doesn't exist then return false
  98. if (temp->children[ascii] == NULL)
  99. {
  100. printf("mispelled: %d, %c\n", ascii, word[k]);
  101. return false;
  102. }
  103. // if the character does exist have temp point to the new character
  104. else if (temp->children[ascii] != NULL)
  105. {
  106. printf("maybe a word: %d\n", ascii);
  107. temp = temp->children[ascii];
  108. }
  109. }
  110. return temp->is_word;
  111. }
  112. bool unload1(node *current_node)
  113. {
  114. for(int j = 0; j < 27; j++)
  115. {
  116. if (current_node -> children[j] != NULL)
  117. {
  118. current_node = current_node->children[j];
  119. unload1(current_node);
  120. }
  121. }
  122. free(current_node);
  123. current_node = NULL;
  124. return false;
  125. }
  126. // Unloads dictionary from memory, returning true if successful else false
  127. bool unload(void)
  128. {
  129. // need to unload each node
  130. node *current_node;
  131. current_node = root;
  132. unload1(current_node);
  133.  
  134. // need to close the file
  135. fclose(dict);
  136. return true;
  137. }
  138.  
  139.  
  140. int main(int argc, char *argv[])
  141. {
  142. if (argc != 2)
  143. {
  144. printf("error");
  145. return 1;
  146. }
  147. char *dictionary = argv[1];
  148. bool loaded = load(dictionary);
  149. printf(loaded ? "true\n" : "false\n");
  150. int n = size();
  151. printf("n = %i\n", n);
  152. char *word = "Ca'ts";
  153. check(word);
  154. unload();
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement