Advertisement
ehahehah

dictionary2a.c

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