From 94026cf1d5bfe86b04e9b139f9fc18949b00898c Mon Sep 17 00:00:00 2001 From: Fudgerboy <91767657+Fudgerboy@users.noreply.github.com> Date: Mon, 29 Apr 2024 03:05:54 +0000 Subject: Sun, Apr 28, 2024, 8:05 PM -07:00 --- test.c | 144 ++++++++++++++++++++++++++++++++++++++++++ wk5/pset/speller/dictionary.c | 110 +++----------------------------- wk5/pset/speller/test.c | 144 ------------------------------------------ 3 files changed, 154 insertions(+), 244 deletions(-) create mode 100644 test.c delete mode 100644 wk5/pset/speller/test.c diff --git a/test.c b/test.c new file mode 100644 index 0000000..7e5cc61 --- /dev/null +++ b/test.c @@ -0,0 +1,144 @@ +// Implements a dictionary's functionality + +#include +#include +#include +#include +#include +#include "dictionary.h" + +// Represents a node in a hash table +typedef struct node +{ + char word[LENGTH + 1]; + struct node *next; +} node; + +// TODO: Choose number of buckets in hash table +const unsigned int N = 26; + +// Hash table +node *table[N]; + +// Size integer +int siz = 0; + +// Returns true if word is in dictionary, else false +bool check(const char *word) +{ + // hash word to find bucket + int val = hash(word); + node *current = table[val]; + + // check all nodes in the bucket + while (current != NULL) + { + // check if this is the word + if (strcmp(current->word, word) == 0) + { + return true; + } + current = current->next; + } + + return false; +} + +// Hashes word to a number +unsigned int hash(const char *word) +{ + // (sum of (letter - 'A') % 26) of a word to get a value of where to store it in the hash table + int val = 0; + for (int i = 0; word[i] != '\0'; i++) + { + val += toupper(word[i]) - 'A'; + } + return val %= 26; +} + +// Loads dictionary into memory, returning true if successful, else false +bool load(const char *dictionary) +{ + // initialize the table + for (int i = 0; i < N; i++) { + // malloc size of next + table[i] = NULL; + } + + // Open dictionary file + FILE *source = fopen(dictionary, "r"); + if (source == NULL) + { + return false; + } + + //read each word in dictionary + char word[LENGTH + 1]; + // use fscanf(file, "%s", word) to grab words + // check for ended file + while(fscanf(source, "%s", word) == 1) + { + // update size int + siz++; + // create new node + // use malloc + node *ptr = malloc(sizeof(node)); + // check if return is NULL + if (ptr == NULL) + { + fclose(source); + return false; + } + // copy word from fscanf into node using strcpy + strcpy(ptr->word, word); + // set the new ptr + ptr->next = NULL; + // hash the word to find the bucket it goes in + int val = hash(ptr->word); + // put new node at begining of bucket + if (table[val] == NULL) + { + // if empty put it there + table[val] = ptr; + } + else + { + // if not empty move the current first one down, + // then put the new one there + ptr->next = table[val]; + table[val] = ptr; + } + + } + + // Close the dictionary file + fclose(source); + return true; +} + +// Returns number of words in dictionary if loaded, else 0 if not yet loaded +unsigned int size(void) +{ + return siz; +} + +// Unloads dictionary from memory, returning true if successful, else false +bool unload(void) +{ + // for every bucket + for (int i = 0; i < N; i++) + { + // while there's more in the bucket + node *current = table[i]; + while (current != NULL) + { + // record position of this node + node *this = current; + // record position of next node + current = current->next; + // free this node + free(this); + } + } + return true; +} diff --git a/wk5/pset/speller/dictionary.c b/wk5/pset/speller/dictionary.c index 7e5cc61..a20c467 100644 --- a/wk5/pset/speller/dictionary.c +++ b/wk5/pset/speller/dictionary.c @@ -2,9 +2,7 @@ #include #include -#include -#include -#include + #include "dictionary.h" // Represents a node in a hash table @@ -20,125 +18,37 @@ const unsigned int N = 26; // Hash table node *table[N]; -// Size integer -int siz = 0; - // Returns true if word is in dictionary, else false bool check(const char *word) { - // hash word to find bucket - int val = hash(word); - node *current = table[val]; - - // check all nodes in the bucket - while (current != NULL) - { - // check if this is the word - if (strcmp(current->word, word) == 0) - { - return true; - } - current = current->next; - } - + // TODO return false; } // Hashes word to a number unsigned int hash(const char *word) { - // (sum of (letter - 'A') % 26) of a word to get a value of where to store it in the hash table - int val = 0; - for (int i = 0; word[i] != '\0'; i++) - { - val += toupper(word[i]) - 'A'; - } - return val %= 26; + // TODO: Improve this hash function + return toupper(word[0]) - 'A'; } // Loads dictionary into memory, returning true if successful, else false bool load(const char *dictionary) { - // initialize the table - for (int i = 0; i < N; i++) { - // malloc size of next - table[i] = NULL; - } - - // Open dictionary file - FILE *source = fopen(dictionary, "r"); - if (source == NULL) - { - return false; - } - - //read each word in dictionary - char word[LENGTH + 1]; - // use fscanf(file, "%s", word) to grab words - // check for ended file - while(fscanf(source, "%s", word) == 1) - { - // update size int - siz++; - // create new node - // use malloc - node *ptr = malloc(sizeof(node)); - // check if return is NULL - if (ptr == NULL) - { - fclose(source); - return false; - } - // copy word from fscanf into node using strcpy - strcpy(ptr->word, word); - // set the new ptr - ptr->next = NULL; - // hash the word to find the bucket it goes in - int val = hash(ptr->word); - // put new node at begining of bucket - if (table[val] == NULL) - { - // if empty put it there - table[val] = ptr; - } - else - { - // if not empty move the current first one down, - // then put the new one there - ptr->next = table[val]; - table[val] = ptr; - } - - } - - // Close the dictionary file - fclose(source); - return true; + // TODO + return false; } // Returns number of words in dictionary if loaded, else 0 if not yet loaded unsigned int size(void) { - return siz; + // TODO + return 0; } // Unloads dictionary from memory, returning true if successful, else false bool unload(void) { - // for every bucket - for (int i = 0; i < N; i++) - { - // while there's more in the bucket - node *current = table[i]; - while (current != NULL) - { - // record position of this node - node *this = current; - // record position of next node - current = current->next; - // free this node - free(this); - } - } - return true; + // TODO + return false; } diff --git a/wk5/pset/speller/test.c b/wk5/pset/speller/test.c deleted file mode 100644 index 7e5cc61..0000000 --- a/wk5/pset/speller/test.c +++ /dev/null @@ -1,144 +0,0 @@ -// Implements a dictionary's functionality - -#include -#include -#include -#include -#include -#include "dictionary.h" - -// Represents a node in a hash table -typedef struct node -{ - char word[LENGTH + 1]; - struct node *next; -} node; - -// TODO: Choose number of buckets in hash table -const unsigned int N = 26; - -// Hash table -node *table[N]; - -// Size integer -int siz = 0; - -// Returns true if word is in dictionary, else false -bool check(const char *word) -{ - // hash word to find bucket - int val = hash(word); - node *current = table[val]; - - // check all nodes in the bucket - while (current != NULL) - { - // check if this is the word - if (strcmp(current->word, word) == 0) - { - return true; - } - current = current->next; - } - - return false; -} - -// Hashes word to a number -unsigned int hash(const char *word) -{ - // (sum of (letter - 'A') % 26) of a word to get a value of where to store it in the hash table - int val = 0; - for (int i = 0; word[i] != '\0'; i++) - { - val += toupper(word[i]) - 'A'; - } - return val %= 26; -} - -// Loads dictionary into memory, returning true if successful, else false -bool load(const char *dictionary) -{ - // initialize the table - for (int i = 0; i < N; i++) { - // malloc size of next - table[i] = NULL; - } - - // Open dictionary file - FILE *source = fopen(dictionary, "r"); - if (source == NULL) - { - return false; - } - - //read each word in dictionary - char word[LENGTH + 1]; - // use fscanf(file, "%s", word) to grab words - // check for ended file - while(fscanf(source, "%s", word) == 1) - { - // update size int - siz++; - // create new node - // use malloc - node *ptr = malloc(sizeof(node)); - // check if return is NULL - if (ptr == NULL) - { - fclose(source); - return false; - } - // copy word from fscanf into node using strcpy - strcpy(ptr->word, word); - // set the new ptr - ptr->next = NULL; - // hash the word to find the bucket it goes in - int val = hash(ptr->word); - // put new node at begining of bucket - if (table[val] == NULL) - { - // if empty put it there - table[val] = ptr; - } - else - { - // if not empty move the current first one down, - // then put the new one there - ptr->next = table[val]; - table[val] = ptr; - } - - } - - // Close the dictionary file - fclose(source); - return true; -} - -// Returns number of words in dictionary if loaded, else 0 if not yet loaded -unsigned int size(void) -{ - return siz; -} - -// Unloads dictionary from memory, returning true if successful, else false -bool unload(void) -{ - // for every bucket - for (int i = 0; i < N; i++) - { - // while there's more in the bucket - node *current = table[i]; - while (current != NULL) - { - // record position of this node - node *this = current; - // record position of next node - current = current->next; - // free this node - free(this); - } - } - return true; -} -- cgit v1.2.3