ahmedraza

dictionary.c

Jan 7th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 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.  #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <stdbool.h>
  13.  
  14. #include "dictionary.h"
  15.  
  16. /**
  17.  * Returns true if word is in dictionary else false.
  18.  */
  19.  
  20.  
  21.  
  22.  node* root;
  23.  
  24.  int words = 0;
  25.  
  26.  /**
  27.  * Loads dictionary into memory.  Returns true if successful else false.
  28.  */
  29. bool load(const char* dictionary)
  30. {
  31.   FILE *fp = fopen (dictionary, "r");
  32.   if (fp == NULL)
  33.   {
  34.       return false;
  35.   }
  36.   root = calloc (sizeof (node), 1);
  37.   if (root == NULL)
  38.   {
  39.       return false;
  40.   }
  41.   node* cursor;
  42.   cursor = root;
  43.  
  44.   for (int k = fgetc (fp); k != EOF; k = fgetc (fp) )
  45.   {
  46.       int i = position(k);
  47.      
  48.       if (k != '\n')
  49.       {
  50.           if (cursor->children[i] == NULL)
  51.           {
  52.                 cursor->children[i] = calloc (sizeof (node),1);
  53.                 if (cursor -> children[i] == NULL)
  54.                 {
  55.                     return false;
  56.                 }
  57.                 cursor = cursor->children[i];
  58.           }
  59.           else
  60.           {
  61.               cursor = cursor -> children[i];
  62.           }
  63.       }
  64.       else
  65.       {
  66.           cursor -> isWord = true;
  67.           words++;
  68.           cursor = root;
  69.       }
  70.   }
  71.   fclose (fp);
  72.  
  73.     return true;
  74. }
  75.  
  76. bool check(const char* word)
  77. {
  78.     int i = 0;
  79.     node* cursor = root;
  80.     while (cursor != NULL)
  81.     {
  82.         char c = word[i];
  83.         if (c == '\0')
  84.         {
  85.             return cursor->isWord;
  86.         }
  87.         int p = position(c);
  88.         cursor = cursor->children[p];
  89.         i++;
  90.     }
  91.     return false;
  92. }
  93.  
  94.  
  95. /**
  96.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  97.  */
  98. unsigned int size(void)
  99. {
  100.    
  101.     return words;
  102. }
  103.  
  104. /**
  105.  * Unloads dictionary from memory.  Returns true if successful else false.
  106.  */
  107. bool unload(void)
  108. {
  109.     node* cursor = root;
  110.     for (int i = 0;i<27;i++)
  111.     {
  112.         if (cursor->children[i] != NULL)
  113.         {
  114.             cursor  = cursor->children[i];
  115.         }
  116.         free (cursor);
  117.     }
  118.     return true;
  119. }
  120.  
  121. int position (int a)
  122. {
  123.     int c = 0;
  124.     if (isupper (a))
  125.     {
  126.          c = a-65;
  127.          return c;
  128.     }
  129.     else if (islower(a))
  130.     {
  131.         c = a-97;
  132.         return c;
  133.     }
  134.     else
  135.     return -1;
  136. }
Add Comment
Please, Sign In to add comment