Advertisement
Hackirus

dictionary

Feb 20th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.28 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 <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. #define ALPHA 27
  17.  
  18. #include "dictionary.h"
  19. /**
  20.  * Returns true if word is in dictionary else false.
  21.  */
  22. int NUM_OF_WORD = 0;
  23.  
  24. typedef struct trie    
  25. {
  26.     bool is_word;
  27.     struct trie* ALPHABET[ALPHA];          
  28. }trie;
  29.  
  30. trie* ptr_del = NULL;
  31. trie* ptr = NULL;
  32. trie* head = NULL;
  33. trie* copy_ptr = NULL;
  34.    
  35. bool check(const char* word)
  36. {
  37.     int index = 0;
  38.     int i = 0;
  39.     while(word[i] != '\0')
  40.     {
  41.         i++;
  42.     }
  43.    
  44.     ptr = head;
  45.    
  46.     for(int a = 0; a < i; a++)
  47.     {
  48.         if(word[a] >= 'a' && word[a] <= 'z')
  49.         {
  50.             index = word[a] - 'a';
  51.         }
  52.         else if(word[a] >= 'A' && word[a] <= 'Z')
  53.         {
  54.             index = word[a] - 'A';
  55.         }
  56.         else if(word[a] == 39)
  57.         {
  58.             index = 26;
  59.         }
  60.         if(ptr -> ALPHABET[index] != NULL)
  61.         {
  62.             ptr = ptr -> ALPHABET[index];
  63.         }
  64.         else if(ptr -> ALPHABET[index] == NULL)
  65.         {
  66.             return false;
  67.         }
  68.     }
  69.    
  70.     if(ptr -> is_word == true)
  71.     {
  72.         return true;
  73.     }
  74.     else
  75.     {
  76.         return false;
  77.     }
  78. }
  79.  
  80. /**
  81.  * Loads dictionary into memory.  Returns true if successful else false.
  82.  */
  83. bool load(const char* dictionary)
  84. {
  85.     int WORD_SIZE = 0;
  86.     char c;
  87.     char word[50];
  88.     int index = 0;
  89.    
  90.     FILE* fp = fopen(dictionary, "r");
  91.     if(fp == NULL)
  92.     {
  93.         printf("file not open");
  94.         return 1;
  95.     }
  96.    
  97.     head = malloc(sizeof(trie));
  98.     ptr = head;
  99.     head -> is_word = false;
  100.     for(int init = 0; init < ALPHA; init++)
  101.     {
  102.         head -> ALPHABET[init] = NULL;
  103.     }  
  104.  
  105.     while(true)
  106.     {
  107.         WORD_SIZE = 0;
  108.         memset(word, 0, 50);
  109.         ptr = head;
  110.         NUM_OF_WORD++;
  111.         while((c = fgetc(fp)) != EOF)
  112.         {  
  113.             if(isalpha(c) == 0)
  114.             {  
  115.                 if(c != 39)
  116.                 {
  117.                     break;
  118.                 }            
  119.             }
  120.             word[WORD_SIZE] = c;
  121.             WORD_SIZE++;
  122.         }
  123.        
  124.         for(int i = 0; i < WORD_SIZE; i++)
  125.         {
  126.             if(word[i] >= 'a' && word[i] <= 'z')
  127.             {
  128.                 index = word[i] - 'a';
  129.             }
  130.             else
  131.             {
  132.                 index = 26;
  133.             }
  134.            
  135.             if(ptr -> ALPHABET[index] == NULL)
  136.             {
  137.                 trie* new_node = malloc(sizeof(trie));
  138.                 for(int init = 0; init < ALPHA; init++)
  139.                 {
  140.                     new_node -> ALPHABET[init] = NULL;
  141.                 }
  142.                 new_node -> is_word = NULL;
  143.                 ptr -> ALPHABET[index] = new_node;
  144.                
  145.                 ptr = ptr -> ALPHABET[index];
  146.             }
  147.             else
  148.             {
  149.                 ptr = ptr -> ALPHABET[index];
  150.             }
  151.         }
  152.         ptr -> is_word = true;
  153.         if(c == EOF)
  154.         break;
  155.     }
  156.     fclose(fp);
  157.     return true;
  158. }
  159.  
  160. /**
  161.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  162.  */
  163. unsigned int size(void)
  164. {
  165.     return NUM_OF_WORD - 1;
  166. }
  167.  
  168. /**
  169.  * Unloads dictionary from memory.  Returns true if successful else false.
  170.  */
  171. bool unload(void)
  172. {
  173.     ptr = head;
  174.     copy_ptr = ptr;
  175.     int num_p = 0;
  176.     for(int switcher = 0; switcher < 27; switcher++)
  177.     {  
  178.         if(ptr -> ALPHABET[switcher] != NULL)
  179.         {
  180.             copy_ptr = ptr;
  181.             num_p = switcher;
  182.             ptr = ptr -> ALPHABET[switcher];
  183.             switcher = -1;
  184.         }
  185.         if(switcher == 26 && ptr -> ALPHABET[switcher] == NULL)
  186.         {
  187.             copy_ptr -> ALPHABET[num_p] = NULL;
  188.             if(switcher == 26 && ptr == head)
  189.             {
  190.                 free(head);
  191.                 break;
  192.             }
  193.             free(ptr);
  194.             ptr = head;
  195.             switcher = -1;          
  196.         }
  197.     }
  198.     return true;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement