Advertisement
ehahehah

dictionary1.c

Jun 11th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 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.             else if (word[i + 1] == '\0')
  43.             {
  44.                 if (ptr->is_word == true)
  45.                 {
  46.                     return true;
  47.                 }
  48.                
  49.                 ptr = ptr->children[letter  - 'A'];
  50.             }
  51.         }
  52.        
  53.         else if (islower(word[i]))
  54.         {
  55.             if (ptr->children[letter  - 'a'] == NULL)
  56.             {
  57.                 return false;
  58.             }
  59.            
  60.             else if (word[i + 1] == '\0')
  61.             {
  62.                 if (ptr->is_word == true)
  63.                 {
  64.                     return true;
  65.                 }
  66.                
  67.                 ptr = ptr->children[letter  - 'A'];
  68.             }
  69.         }
  70.        
  71.         else if (word[i] == '\'')
  72.         {
  73.             if (ptr->children[26] == NULL)
  74.             {
  75.                 return false;
  76.             }
  77.              
  78.             else if (word[i + 1] == '\0')
  79.             {
  80.                 if (ptr->is_word == true)
  81.                 {
  82.                     return true;
  83.                 }
  84.                
  85.                 ptr = ptr->children[letter  - 'A'];
  86.             }
  87.         }
  88.        
  89.         i++;
  90.     }
  91. }
  92.  
  93. /**
  94.  * Loads dictionary into memory.  Returns true if successful else false.
  95.  */
  96. bool load(const char* dictionary)
  97. {
  98.     FILE* dict = fopen(dictionary, "r");
  99.     if (dict == NULL)
  100.     {
  101.         printf("Dictionary could not be opened.\n");
  102.         return false;
  103.     }
  104.    
  105.     root = calloc(1, sizeof(node));
  106.     node* ptr = root;
  107.     for (char c = fgetc(dict); c != EOF; c = fgetc(dict))
  108.     {  
  109.         if (ptr->children[c - 'a'] == NULL)
  110.         {
  111.             ptr->children[c - 'a'] = calloc(1, sizeof(node));
  112.             ptr = ptr->children[c - 'a'];
  113.         }
  114.        
  115.         else
  116.         {
  117.             ptr = ptr->children[c - 'a'];
  118.         }
  119.        
  120.         //check for end of word
  121.         if (c == '\0')
  122.         {
  123.             ptr->is_word = true;
  124.             ptr = root;
  125.             words_loaded++;
  126.         }
  127.     }
  128.     return true;
  129. }
  130.  
  131. /**
  132.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  133.  */
  134. unsigned int size(void)
  135. {
  136.     return words_loaded;
  137. }
  138.  
  139. void unloader(node* track)
  140. {
  141.     node* ptr = track;
  142.     int i = 0;
  143.    
  144.     while (i != 26)
  145.     {
  146.         if (ptr->children[i] != NULL)
  147.         {
  148.             unloader(ptr->children[i]);
  149.         }
  150.  
  151.         i++;
  152.     }
  153.  
  154.     node* temp = ptr;
  155.     free(temp);
  156. }
  157.  
  158. /**
  159.  * Unloads dictionary from memory.  Returns true if successful else false.
  160.  */
  161. bool unload(void)
  162. {
  163.     unloader(root);
  164.     return true;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement