Advertisement
SKTLV

Speller

Mar 6th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7.  
  8. #include "dictionary.h"
  9.  
  10. // Represents number of buckets in a hash table
  11. #define N 26
  12.  
  13. // Represents a node in a hash table
  14. typedef struct node
  15. {
  16.     char word[LENGTH + 1];
  17.     struct node *next;
  18. }
  19. node;
  20.  
  21. // Represents a hash table
  22. node *hashtable[N];
  23. // Keeps track of the words that we loaded
  24. int words = 0;
  25.  
  26. // Creates the head of the hashtable pointer
  27. node *head;
  28.  
  29. // Hashes word to a number between 0 and 25, inclusive, based on its first letter
  30. unsigned int hash(const char *word)
  31. {
  32.     return tolower(word[0]) - 'a';
  33. }
  34.  
  35. // Loads dictionary into memory, returning true if successful else false
  36. bool load (const char *dictionary)
  37. {
  38.     // Initialize hash table
  39.     for (int i = 0; i < N; i++)
  40.     {
  41.         hashtable[i] = NULL;
  42.     }
  43.  
  44.     // Open dictionary
  45.     FILE *file = fopen(dictionary, "r");
  46.     if (file == NULL)
  47.     {
  48.         unload();
  49.         return false;
  50.     }
  51.  
  52.     // Buffer for a word
  53.     char word[LENGTH + 1];
  54.  
  55.     int index;
  56.  
  57.     // Insert words into hash table
  58.     while (fscanf(file, "%s", word) != EOF)
  59.     {
  60.         // Saves the hash of dictionary words into a variable of index
  61.         index = hash(word);
  62.         // Create nodes with a numbered name based on how many times while has ran
  63.         node *newNode = malloc(sizeof(node));
  64.         if (newNode == NULL)
  65.         {
  66.             unload();
  67.             return false;
  68.         }
  69.  
  70.         // Copies word from dictionary into the new node created
  71.         strcpy(newNode->word, word);
  72.  
  73.         // first one
  74.         if(newNode == NULL)
  75.         {
  76.             head = newNode;
  77.             hashtable[index] = newNode;
  78.         }
  79.  
  80.         else
  81.         {
  82.             newNode->next = hashtable[index];
  83.             hashtable[index] = newNode;
  84.         }
  85.         // Increment after every word added
  86.         words++;
  87.     }
  88.  
  89.     printf("Counter: %i\n", words);
  90.     // Close dictionary
  91.     fclose(file);
  92.  
  93.     // Indicate success
  94.     return true;
  95. }
  96.  
  97. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  98. unsigned int size(void)
  99. {
  100.     if (words > 0)
  101.     {
  102.         return words;
  103.     }
  104.     else
  105.  
  106.         return 0;
  107. }
  108.  
  109. // Returns true if word is in dictionary else false
  110. bool check(const char *word)
  111. {
  112.     int index = hash(word);
  113.     if (strcasecmp(hashtable[index]->word, word) == 0)
  114.     {
  115.         return true;
  116.     }
  117.  
  118.     return false;
  119. }
  120.  
  121. // Unloads dictionary from memory, returning true if successful else false
  122. bool unload(void)
  123. {
  124.     // TODO
  125.     node *trav = head;
  126.     while(trav != NULL)
  127.     {
  128.         node *tmp = trav;
  129.         trav = trav->next;
  130.         free(tmp);
  131.     }
  132.     if(head == NULL)
  133.     {
  134.         return true;
  135.     }
  136.     return false;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement