Advertisement
Karim_ElKobrossy

dictionary.c

Nov 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. /**
  2.  * Implements a dictionary's functionality.
  3.  */
  4. #include <stddef.h>
  5. #include <stdbool.h>
  6.  
  7. #include "dictionary.h"
  8.  
  9. /**
  10.  * Returns true if word is in dictionary else false.
  11.  */
  12.  node* root;
  13.  unsigned int words;
  14.  
  15. bool check(const char *word)
  16. {
  17.    
  18.     node* trav = root;
  19.     bool y;
  20.     int n;
  21.     for(int i = 0; word[i]!='\0'; i++)
  22.     {
  23.         char c = tolower(word[i]);
  24.         n = c-97;
  25.        if(word[i]=='\'')
  26.        n=26;
  27.         if(trav->children[n]!= NULL)
  28.         {
  29.             trav= trav->children[n];
  30.             y=true;
  31.         }
  32.         else
  33.         {
  34.         y=false;
  35.         break;
  36.         }
  37.     }
  38.     if(trav->is_word == true && y == true)
  39.     return true;
  40.     return false;
  41. }
  42.  
  43. /**
  44.  * Loads dictionary into memory. Returns true if successful else false.
  45.  */
  46. bool load(const char *dictionary)
  47. {
  48.     FILE* DI = fopen(dictionary , "r");
  49.     if(DI == NULL)
  50.     return false;
  51.     else
  52.     {
  53.       root = calloc(1,sizeof(node));
  54.       char word[LENGTH+1];
  55.       while(fscanf(DI , "%s" , word)!= EOF)
  56.       {
  57.           node* trav = root;
  58.           for(int i=0 ; word[i]!= '\0' ; i++)
  59.           {
  60.              int n = word[i]-97;
  61.              if(n == -58)
  62.              n=26;
  63.                   if(!(trav->children[n]))
  64.                   {
  65.                       node* newnode;
  66.                       newnode = calloc(1,sizeof(node));
  67.                       trav->children[n] = newnode;
  68.                       if(word[i+1]!='\0')
  69.                       trav= newnode;
  70.                       else
  71.                       newnode->is_word = true;
  72.                   }
  73.                   else
  74.                   trav = trav->children[n];
  75.           }
  76.       words++;
  77.          
  78.       }
  79.       fclose(DI);
  80.       return true;
  81.     }
  82. }
  83.  
  84. /**
  85.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  86.  */
  87. unsigned int size(void)
  88. {
  89.     // TODO
  90.     return words;
  91. }
  92.  
  93. /**
  94.  * Unloads dictionary from memory. Returns true if successful else false.
  95.  */
  96. bool unload(void)
  97. {
  98.  delete(root);
  99.  return true;
  100. }
  101.  
  102. void delete(node* x)
  103. {
  104.  for(int j=0; j<28; j++)
  105.  {
  106.      if(x->children[j])
  107.      delete(x->children[j]);
  108.  }
  109.  free(x);    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement