ahmedraza

dictionary.c

Jan 7th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 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.  //Global variable to count words in the Dictionary
  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.     //opening dictionary file to read
  32.   FILE *fp = fopen (dictionary, "r");
  33.   if (fp == NULL)
  34.   {
  35.       return false;
  36.   }
  37.   //Allocatng memory for root pointer.
  38.   root = calloc (sizeof (node),1);
  39.   //running pointer
  40.   node* cursor;
  41.   cursor = root;
  42.   //Loop to take chars untill EOF.
  43.   for (int k = fgetc (fp); k != EOF; k = fgetc (fp) )
  44.   {
  45.       //For converting chars to its position in array
  46.       int i = position(k);
  47.       //If it is not end of word.
  48.       if (k != '\n')
  49.       {
  50.           //If cursor->children[i] don't exist malloc these.
  51.           if (cursor->children[i] == NULL)
  52.           {
  53.                 cursor->children[i] = calloc (sizeof (node),1);
  54.                 if (cursor -> children[i] == NULL)
  55.                 {
  56.                     return false;
  57.                 }
  58.                 cursor = cursor->children[i];
  59.           }
  60.           else
  61.           {
  62.               cursor = cursor -> children[i];
  63.           }
  64.       }
  65.       else
  66.       {
  67.           //Word is complete.
  68.           cursor -> isWord = true;
  69.           //Count words
  70.           words++;
  71.           //Set cuersor again back to root.
  72.           cursor = root;
  73.       }
  74.   }
  75.   //Close file.
  76.   fclose (fp);
  77.  
  78.     return true;
  79. }
  80.  
  81. bool check(const char* word)
  82. {
  83.     int i = 0;
  84.     node* cursor = root;
  85.     while (cursor != NULL)
  86.     {
  87.         char c = word[i];
  88.         if (c == '\0')
  89.         {
  90.             return cursor->isWord;
  91.         }
  92.         int p = position(c);
  93.         cursor = cursor->children[p];
  94.         i++;
  95.     }
  96.     return false;
  97. }
  98.  
  99.  
  100. /**
  101.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  102.  */
  103. unsigned int size(void)
  104. {
  105.    
  106.     return words;
  107. }
  108.  
  109. /**
  110.  * Unloads dictionary from memory.  Returns true if successful else false.
  111.  */
  112. bool unload(void)
  113. {
  114.     node* cursor = root;
  115.     for (int i = 0;i<27;i++)
  116.     {
  117.         if (cursor->children[i] != NULL)
  118.         {
  119.             cursor  = cursor->children[i];
  120.         }
  121.         free (cursor);
  122.     }
  123.     return true;
  124. }
  125.  
  126. int position (int a)
  127. {
  128.     int c = 0;
  129.     if (isupper (a))
  130.     {
  131.          c = a-65;
  132.          return c;
  133.     }
  134.     else if (islower(a))
  135.     {
  136.         c = a-97;
  137.         return c;
  138.     }
  139.     else
  140.     return -1;
  141. }
Add Comment
Please, Sign In to add comment