Advertisement
IrinaPenzina

pset5 speller

Sep 16th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include "dictionary.h"
  10.  
  11. typedef struct node
  12. {
  13.     bool is_word;
  14.     struct node *children[27];
  15. }
  16. node;
  17.  
  18. node *root;
  19.  
  20. // Returns true if word is in dictionary else false
  21. bool check(const char *word)
  22. {
  23.     int index = 0;
  24.     node *cursor = root;
  25.     //for each letter in input word
  26.     for(int n = 0; n < strlen(word); n++)
  27.     {
  28.         if (tolower(word[n]))
  29.         {
  30.             //index = (tolower(word[n]) - 'a');
  31.             if (cursor -> children[index] == NULL)
  32.             {
  33.                 return false;
  34.             }
  35.             else if (cursor -> children[index] != NULL)
  36.             {
  37.                 cursor = cursor -> children[index];
  38.             }
  39.         }
  40.          else if (word[n] == '\'')
  41.         {
  42.             if (cursor -> children[26] == NULL)
  43.             {
  44.                return false;
  45.             }
  46.             else if (cursor -> children[26] != NULL)
  47.             {
  48.                 cursor = cursor -> children[index];
  49.             }
  50.         }
  51.         else if (word[n] == '\0')
  52.         {
  53.             cursor -> is_word = true;
  54.             return true;
  55.         }
  56.     }
  57.     return true;
  58. }
  59.  
  60. // Loads dictionary into memory, returning true if successful else false
  61. bool load(const char *dictionary)
  62. {
  63.     //open dictionary
  64.     FILE *file = fopen(dictionary, "r");
  65.     if (!file)
  66.     {
  67.         printf("Could not open %s.\n", dictionary);
  68.         unload();
  69.         return false;
  70.     }
  71.     //declaration party
  72.     int index = 0;
  73.     root = malloc(sizeof(node));
  74.     //struct node *children;
  75.     node *cursor = root;
  76.  
  77.     //for every dictionary word
  78.     for (int c = fgetc(file); c != EOF; c = fgetc(file))
  79.     {
  80.          //if the end of the word
  81.         if (c == '\0')
  82.         {
  83.             cursor -> is_word = true;
  84.             cursor = root;
  85.             return true;
  86.         }
  87.         else if (c == '\'')
  88.         {
  89.             if (cursor -> children[26] == NULL)
  90.             {
  91.                 cursor -> children[index] = malloc(sizeof(node));
  92.             }
  93.             else if (cursor -> children[26] != NULL)
  94.             {
  95.                 cursor = cursor -> children[index];
  96.             }
  97.         }
  98.         //checking every element
  99.         else if (tolower(c))
  100.         {
  101.             //index = (tolower(c) - 'a');
  102.             //if cursor is null
  103.             if (cursor -> children[index] == NULL)
  104.             {
  105.                 cursor -> children[index] = malloc(sizeof(node));
  106.                 //cursor = cursor -> children[index];
  107.             }
  108.             //if not null move to a new node and continue
  109.             else if (cursor -> children[index] != NULL)
  110.             {
  111.                 cursor = cursor -> children[index];
  112.             }
  113.         }
  114.  
  115.     }
  116.     fclose(file);
  117.     return true;
  118. }
  119.  
  120. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  121. unsigned int size(void)
  122. {
  123.     return false;
  124. }
  125.  
  126. // Unloads dictionary from memory, returning true if successful else false
  127. bool unload(void)
  128. {
  129.     node *cursor = root;
  130.     //each element in children corresponds to a different letter
  131.     for (int index = 0; index < 27; index++)
  132.     {
  133.         //if null malloc a new node have children[i] point to it
  134.         if (cursor -> children[index] != NULL)
  135.         {
  136.             free(cursor -> children[index]);
  137.             return true;
  138.         }
  139.         //if not null move to a new node and continue
  140.         else if (cursor != NULL)
  141.         {
  142.             cursor = cursor -> children[index];
  143.             unload();
  144.             return true;
  145.         }
  146.         else
  147.         {
  148.             return true;
  149.         }
  150.     }
  151. return true;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement