Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <stddef.h>
- #include <stdbool.h>
- #include "dictionary.h"
- /**
- * Returns true if word is in dictionary else false.
- */
- node* root;
- unsigned int words;
- bool check(const char *word)
- {
- node* trav = root;
- bool y;
- int n;
- for(int i = 0; word[i]!='\0'; i++)
- {
- char c = tolower(word[i]);
- n = c-97;
- if(word[i]=='\'')
- n=26;
- if(trav->children[n]!= NULL)
- {
- trav= trav->children[n];
- y=true;
- }
- else
- {
- y=false;
- break;
- }
- }
- if(trav->is_word == true && y == true)
- return true;
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- FILE* DI = fopen(dictionary , "r");
- if(DI == NULL)
- return false;
- else
- {
- root = calloc(1,sizeof(node));
- char word[LENGTH+1];
- while(fscanf(DI , "%s" , word)!= EOF)
- {
- node* trav = root;
- for(int i=0 ; word[i]!= '\0' ; i++)
- {
- int n = word[i]-97;
- if(n == -58)
- n=26;
- if(!(trav->children[n]))
- {
- node* newnode;
- newnode = calloc(1,sizeof(node));
- trav->children[n] = newnode;
- if(word[i+1]!='\0')
- trav= newnode;
- else
- newnode->is_word = true;
- }
- else
- trav = trav->children[n];
- }
- words++;
- }
- fclose(DI);
- return true;
- }
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return words;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- delete(root);
- return true;
- }
- void delete(node* x)
- {
- for(int j=0; j<28; j++)
- {
- if(x->children[j])
- delete(x->children[j]);
- }
- free(x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement