Advertisement
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 <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define ALPHA 27
- #include "dictionary.h"
- /**
- * Returns true if word is in dictionary else false.
- */
- int NUM_OF_WORD = 0;
- typedef struct trie
- {
- bool is_word;
- struct trie* ALPHABET[ALPHA];
- }trie;
- trie* ptr_del = NULL;
- trie* ptr = NULL;
- trie* head = NULL;
- trie* copy_ptr = NULL;
- bool check(const char* word)
- {
- int index = 0;
- int i = 0;
- while(word[i] != '\0')
- {
- i++;
- }
- ptr = head;
- for(int a = 0; a < i; a++)
- {
- if(word[a] >= 'a' && word[a] <= 'z')
- {
- index = word[a] - 'a';
- }
- else if(word[a] >= 'A' && word[a] <= 'Z')
- {
- index = word[a] - 'A';
- }
- else if(word[a] == 39)
- {
- index = 26;
- }
- if(ptr -> ALPHABET[index] != NULL)
- {
- ptr = ptr -> ALPHABET[index];
- }
- else if(ptr -> ALPHABET[index] == NULL)
- {
- return false;
- }
- }
- if(ptr -> is_word == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- int WORD_SIZE = 0;
- char c;
- char word[50];
- int index = 0;
- FILE* fp = fopen(dictionary, "r");
- if(fp == NULL)
- {
- printf("file not open");
- return 1;
- }
- head = malloc(sizeof(trie));
- ptr = head;
- head -> is_word = false;
- for(int init = 0; init < ALPHA; init++)
- {
- head -> ALPHABET[init] = NULL;
- }
- while(true)
- {
- WORD_SIZE = 0;
- memset(word, 0, 50);
- ptr = head;
- NUM_OF_WORD++;
- while((c = fgetc(fp)) != EOF)
- {
- if(isalpha(c) == 0)
- {
- if(c != 39)
- {
- break;
- }
- }
- word[WORD_SIZE] = c;
- WORD_SIZE++;
- }
- for(int i = 0; i < WORD_SIZE; i++)
- {
- if(word[i] >= 'a' && word[i] <= 'z')
- {
- index = word[i] - 'a';
- }
- else
- {
- index = 26;
- }
- if(ptr -> ALPHABET[index] == NULL)
- {
- trie* new_node = malloc(sizeof(trie));
- for(int init = 0; init < ALPHA; init++)
- {
- new_node -> ALPHABET[init] = NULL;
- }
- new_node -> is_word = NULL;
- ptr -> ALPHABET[index] = new_node;
- ptr = ptr -> ALPHABET[index];
- }
- else
- {
- ptr = ptr -> ALPHABET[index];
- }
- }
- ptr -> is_word = true;
- if(c == EOF)
- break;
- }
- fclose(fp);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- return NUM_OF_WORD - 1;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- ptr = head;
- copy_ptr = ptr;
- int num_p = 0;
- for(int switcher = 0; switcher < 27; switcher++)
- {
- if(ptr -> ALPHABET[switcher] != NULL)
- {
- copy_ptr = ptr;
- num_p = switcher;
- ptr = ptr -> ALPHABET[switcher];
- switcher = -1;
- }
- if(switcher == 26 && ptr -> ALPHABET[switcher] == NULL)
- {
- copy_ptr -> ALPHABET[num_p] = NULL;
- if(switcher == 26 && ptr == head)
- {
- free(head);
- break;
- }
- free(ptr);
- ptr = head;
- switcher = -1;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement