Advertisement
ehahehah

dictionary2.c

Jun 12th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 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.                 return false;
  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.                 return false;
  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.                 return false;
  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 (int c = fgetc(dict); c != EOF; c = fgetc(dict))
  108.     {  
  109.         if (isalpha(c))
  110.         {
  111.             if (ptr->children[c - 'a'] == NULL)
  112.             {
  113.                 ptr->children[c - 'a'] = calloc(1, sizeof(node));
  114.                 ptr = ptr->children[c - 'a'];
  115.             }
  116.            
  117.             else
  118.             {
  119.                 ptr = ptr->children[c - 'a'];
  120.             }
  121.         }
  122.  
  123.         else if (c == '\'')
  124.         {
  125.             if (ptr->children[26] == NULL)
  126.             {
  127.                 ptr->children[26] = calloc(1, sizeof(node));
  128.                 ptr = ptr->children[26];
  129.             }
  130.  
  131.             else
  132.             {
  133.                 ptr = ptr->children[26];
  134.             }
  135.         }
  136.        
  137.         // must be new-line
  138.         else
  139.         {
  140.             ptr->is_word = true;
  141.             ptr = root;
  142.             words_loaded++;
  143.         }
  144.     }
  145.     return true;
  146. }
  147.  
  148. /**
  149.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  150.  */
  151. unsigned int size(void)
  152. {
  153.     return words_loaded;
  154. }
  155.  
  156. void unloader(node* track)
  157. {
  158.     node* ptr = track;
  159.     int i = 0;
  160.    
  161.     while (i != 26)
  162.     {
  163.         if (ptr->children[i] != NULL)
  164.         {
  165.             unloader(ptr->children[i]);
  166.         }
  167.  
  168.         i++;
  169.     }
  170.  
  171.     node* temp = ptr;
  172.     free(temp);
  173. }
  174.  
  175. /**
  176.  * Unloads dictionary from memory.  Returns true if successful else false.
  177.  */
  178. bool unload(void)
  179. {
  180.     unloader(root);
  181.     return true;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement