Guest User

cs50 speller

a guest
Aug 29th, 2023
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <strings.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. // Represents a node in a hash table
  13. typedef struct node
  14. {
  15.     char word[LENGTH + 1];
  16.     struct node *next;
  17. }
  18. node;
  19.  
  20. // variables
  21. unsigned int counter;
  22. unsigned int word_count = 0;
  23.  
  24. // TODO: Choose number of buckets in hash table
  25. const unsigned int N = 26;
  26.  
  27. // Hash table
  28. node *table[N];
  29.  
  30. // Returns true if word is in dictionary, else false
  31. bool check(const char *word)
  32. {
  33.     unsigned int mav = hash(word);
  34.  
  35.     node *trav = table[mav];
  36.  
  37.     while (strcasecmp(trav->word, word) != 0)
  38.     {
  39.         trav = trav->next;
  40.     }
  41.  
  42.     if (strcasecmp(trav->word, word) == 0)
  43.     {
  44.         return true;
  45.     }
  46.  
  47.     return false;
  48. }
  49.  
  50. // Hashes word to a number
  51. unsigned int hash(const char *word)
  52. {
  53.     // TODO: Improve this hash function
  54.     return toupper(word[0]) - 'A'; // first draft. just make it from letter A to letter Z (i.e 26 buckets). more buckets means faster time, but i'll focus on completion first
  55. }
  56.  
  57. // Loads dictionary into memory, returning true if successful, else false
  58. bool load(const char *dictionary)
  59. {
  60.     int swag = 0;
  61.  
  62.     FILE *ptrone = fopen(dictionary, "r");
  63.     if (ptrone == NULL)
  64.     {
  65.         return false;
  66.     }
  67.  
  68.     char buffer[LENGTH + 1];
  69.  
  70.     while (fscanf(ptrone, "%s", buffer) != EOF)
  71.     {
  72.         node *new = malloc(sizeof(node));
  73.         if (new == NULL)
  74.         {
  75.             return false;
  76.         }
  77.  
  78.         strcpy(new->word, buffer);
  79.         counter = hash(new->word);
  80.  
  81.         if (swag == 0)
  82.         {
  83.             new->next = NULL;
  84.             table[counter] = new;
  85.  
  86.             swag++;
  87.             word_count++;
  88.         }
  89.  
  90.         else
  91.         {
  92.             new->next = table[counter];
  93.             table[counter] = new;
  94.  
  95.             word_count++;
  96.         }
  97.     }
  98.  
  99.     fclose(ptrone);
  100.     return true;
  101. }
  102.  
  103. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  104. unsigned int size(void)
  105. {
  106.     if (word_count > 0)
  107.     {
  108.         return word_count;
  109.     }
  110.  
  111.     else
  112.     {
  113.         return 0;
  114.     }
  115. }
  116.  
  117. // Unloads dictionary from memory, returning true if successful, else false
  118. bool unload(void)
  119. {
  120.     for (int x = 0; x < 26; x++)
  121.     {
  122.         node *trav = table[x];
  123.  
  124.         while (trav != NULL)
  125.         {
  126.             table[x] = trav->next;
  127.             free(trav);
  128.             trav = table[x];
  129.         }
  130.     }
  131.  
  132.     return true;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment