Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <ctype.h>
- #include <stdbool.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "dictionary.h"
- typedef struct node
- {
- bool is_word;
- struct node *children[27];
- }
- node;
- node *root;
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- int index = 0;
- node *cursor = root;
- //for each letter in input word
- for(int n = 0; n < strlen(word); n++)
- {
- if (tolower(word[n]))
- {
- //index = (tolower(word[n]) - 'a');
- if (cursor -> children[index] == NULL)
- {
- return false;
- }
- else if (cursor -> children[index] != NULL)
- {
- cursor = cursor -> children[index];
- }
- }
- else if (word[n] == '\'')
- {
- if (cursor -> children[26] == NULL)
- {
- return false;
- }
- else if (cursor -> children[26] != NULL)
- {
- cursor = cursor -> children[index];
- }
- }
- else if (word[n] == '\0')
- {
- cursor -> is_word = true;
- return true;
- }
- }
- return true;
- }
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- //open dictionary
- FILE *file = fopen(dictionary, "r");
- if (!file)
- {
- printf("Could not open %s.\n", dictionary);
- unload();
- return false;
- }
- //declaration party
- int index = 0;
- root = malloc(sizeof(node));
- //struct node *children;
- node *cursor = root;
- //for every dictionary word
- for (int c = fgetc(file); c != EOF; c = fgetc(file))
- {
- //if the end of the word
- if (c == '\0')
- {
- cursor -> is_word = true;
- cursor = root;
- return true;
- }
- else if (c == '\'')
- {
- if (cursor -> children[26] == NULL)
- {
- cursor -> children[index] = malloc(sizeof(node));
- }
- else if (cursor -> children[26] != NULL)
- {
- cursor = cursor -> children[index];
- }
- }
- //checking every element
- else if (tolower(c))
- {
- //index = (tolower(c) - 'a');
- //if cursor is null
- if (cursor -> children[index] == NULL)
- {
- cursor -> children[index] = malloc(sizeof(node));
- //cursor = cursor -> children[index];
- }
- //if not null move to a new node and continue
- else if (cursor -> children[index] != NULL)
- {
- cursor = cursor -> children[index];
- }
- }
- }
- fclose(file);
- return true;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- return false;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- node *cursor = root;
- //each element in children corresponds to a different letter
- for (int index = 0; index < 27; index++)
- {
- //if null malloc a new node have children[i] point to it
- if (cursor -> children[index] != NULL)
- {
- free(cursor -> children[index]);
- return true;
- }
- //if not null move to a new node and continue
- else if (cursor != NULL)
- {
- cursor = cursor -> children[index];
- unload();
- return true;
- }
- else
- {
- return true;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement