Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * dictionary.c
- *
- * Computer Science 50
- * Problem Set 5
- *
- * Implements a dictionary's functionality.
- */
- #include <stdbool.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #include "dictionary.h"
- /**
- * declaration for trie struct
- * and length of children array
- */
- #define ARRAY_SIZE 27;
- typedef struct node
- {
- bool is_word;
- struct node* children[ARRAY_SIZE];
- }
- node;
- /**
- * Keeping track of first node of trie here
- */
- node* root;
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word)
- {
- for (int index = 0; index < ARRAY_SIZE; index++)
- {
- if (strcmp((char*)cursor->children[index], word) != 0)
- {
- return false;
- }
- }
- return true;
- }
- /**
- * variable to hold size of dictionary
- */
- unsigned int dict_size = 0;
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- node* new_node;
- FILE* dictptr;
- bool load(const char* dictionary)
- {
- dictptr = fopen(dictionary, "r");
- if (dictptr == NULL)
- {
- exit(1);
- }
- root = calloc(ARRAY_SIZE, sizeof(node));
- if (root == NULL)
- {
- exit(1);
- }
- node* cursor = root;
- char word[ARRAY_SIZE + 1];
- for (int index = 0, n = strlen(word); index < n; index++)
- {
- new_node = calloc(ARRAY_SIZE, sizeof(node));
- if (new_node == NULL)
- {
- exit(1);
- }
- fscanf(dictptr, "%s", word);
- if (root->children[index] == NULL && root->children[index] == new_node)
- {
- if (isalpha(word[index]) || word[index] == '\'')
- {
- cursor = cursor->children[index];
- cursor = new_node;
- cursor = root;
- if (word[index] == '\n')
- {
- cursor = cursor->children[index];
- cursor = new_node;
- cursor->is_word = true;
- cursor = root;
- dict_size++;
- }
- }
- else if (root->children[index] != NULL && root->children[index] == new_node)
- {
- if (isalpha(word[index]) || word[index] == '\'')
- {
- cursor = cursor->children[index];
- cursor = new_node;
- cursor = root;
- if (word[index] == '\n')
- {
- cursor = cursor->children[index];
- cursor = new_node;
- cursor->is_word = true;
- cursor = root;
- dict_size++;
- }
- }
- }
- }
- }
- fclose(dictptr);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- return dict_size;
- }
Add Comment
Please, Sign In to add comment