Advertisement
ehahehah

dictionary.c

Jun 10th, 2016
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. /**
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  */
  9.  
  10. #include <ctype.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #include "dictionary.h"
  16.  
  17. node* root;
  18. unsigned int words_loaded = 0;
  19.  
  20. /**
  21.  * Returns true if word is in dictionary else false.
  22.  */
  23. bool check(const char* word)
  24. {
  25.     // ptr to traverse trie starting from root
  26.     node* ptr = root;
  27.     int i = 0;
  28.     int letter;
  29.    
  30.     // loops through input word
  31.     while (true)
  32.     {  
  33.         letter = (int) *(word + i);
  34.        
  35.         if (isupper(word[i]))
  36.         {
  37.             if (ptr->children[letter - 'A'] == NULL)
  38.             {
  39.                 return false;
  40.             }
  41.            
  42.             ptr = ptr->children[letter  - 'A'];
  43.         }
  44.        
  45.         else if (islower(word[i]))
  46.         {
  47.             if (ptr->children[letter  - 'a'] == NULL)
  48.             {
  49.                 return false;
  50.             }
  51.            
  52.             ptr = ptr->children[letter  - 'a'];
  53.         }
  54.        
  55.         // word[i] must be an apostrophe
  56.         else
  57.         {
  58.             if (ptr->children[26] == NULL)
  59.             {
  60.                 return false;
  61.             }
  62.              
  63.             ptr = ptr->children[26];
  64.         }
  65.        
  66.         i++;
  67.     }
  68.    
  69.     if (ptr->is_word == true)
  70.     {
  71.         return true;
  72.     }
  73.            
  74.     return false;
  75. }
  76.  
  77. /**
  78.  * Loads dictionary into memory.  Returns true if successful else false.
  79.  */
  80. bool load(const char* dictionary)
  81. {
  82.     FILE* dict = fopen(dictionary, "r");
  83.     if (dict == NULL)
  84.     {
  85.         printf("Dictionary could not be opened.\n");
  86.         return false;
  87.     }
  88.    
  89.     root = calloc(1, sizeof(node));
  90.     node* ptr = root;
  91.     for (char c = fgetc(dict); c != EOF; c = fgetc(dict))
  92.     {  
  93.         if (ptr->children[c - 'a'] == NULL)
  94.         {
  95.             ptr->children[c - 'a'] = calloc(1, sizeof(node));
  96.             ptr = ptr->children[c - 'a'];
  97.         }
  98.        
  99.         else
  100.         {
  101.             ptr = ptr->children[c - 'a'];
  102.         }
  103.        
  104.         //check for end of word
  105.         if (c == '\0')
  106.         {
  107.             ptr->is_word = true;
  108.             ptr = root;
  109.             words_loaded++;
  110.         }
  111.     }
  112.     return true;
  113. }
  114.  
  115. /**
  116.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  117.  */
  118. unsigned int size(void)
  119. {
  120.     return words_loaded;
  121. }
  122.  
  123. void unloader(node* track)
  124. {
  125.     node* ptr = track;
  126.     int i = 0;
  127.    
  128.     while (i != 26)
  129.     {
  130.         if (ptr->children[i] != NULL)
  131.         {
  132.             ptr = ptr->children[i];
  133.             unloader(ptr);
  134.         }
  135.  
  136.         i++;
  137.     }
  138.  
  139.     node* temp = ptr;
  140.     free(temp);
  141. }
  142.  
  143. /**
  144.  * Unloads dictionary from memory.  Returns true if successful else false.
  145.  */
  146. bool unload(void)
  147. {
  148.     unloader(root);
  149.     return true;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement