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 <ctype.h>
- #include <string.h>
- #include "dictionary.h"
- typedef struct node
- {
- struct node* next;
- char str[LENGTH+1];
- }
- node;
- #define BUCKETS 1000
- node* table;
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word)
- {
- // TODO
- int wordhash = hash(word, strlen(word));
- node* current = table + wordhash;
- //printf("HASH: %i WORD: %s\n", wordhash, word);
- while(streql(current->str, word) == false)
- {
- //printf(" TESTING: %s", current->str);
- if(current->next == NULL)
- {
- return false;
- }
- current = current->next;
- }
- return true;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // TODO
- FILE* dict = fopen(dictionary, "r");
- char* in;
- table = calloc(BUCKETS, sizeof(node));
- int i = 0;
- in = calloc(LENGTH+1, sizeof(char));
- while(fgets(in, LENGTH+1, dict))
- {
- int strhash = hash(in, strlen(in) - 1);
- node* add = table + strhash;
- if(add->str[0] != '\0')
- {
- while(1)
- {
- if(add->next != NULL)
- {
- add = add->next;
- }
- else
- {
- add->next = calloc(1, sizeof(node));
- add = add->next;
- break;
- }
- }
- }
- for(i = 0; i < strlen(in) && in[i] != '\n'; i++)
- {
- add->str[i] = toupper(in[i]);
- }
- for(; i <= LENGTH; i++)
- {
- add->str[i] = '\0';
- }
- //memcpy(table + hash(in, strsize(in) - 1), in, strsize(in));
- //(table + hash(in, strsize(in) - 1))->str = in;
- //table[i].str = in;
- //printf("%i %s\n", strhash, add->str);
- }
- for(int i = 0; i < BUCKETS; i++)
- {
- node* current = table + i;
- //printf("%i ", i);
- while(current != NULL)
- {
- //printf("%s ", current->str);
- current = current->next;
- }
- //printf("\n");
- }
- free(in);
- fclose(dict);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- int count = 0;
- /* for(int i = 0; i < BUCKETS; i++)*/
- /* {*/
- /* node* current = table + i;*/
- /* while(current != NULL)*/
- /* {*/
- /* i++;*/
- /* current = current->next;*/
- /* }*/
- /* }*/
- return count;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- for(int i = 0; i < BUCKETS; i++)
- {
- node* clear = table + i;
- printf("%i\n", i);
- while(clear != NULL)
- {
- node* temp = clear;
- clear = clear->next;
- free(temp);
- }
- }
- free(table);
- return true;
- }
- int hash(const char* s, int length)
- {
- int sum = 0;
- for(int i = 0; i < length; i++)
- {
- sum += toupper(s[i]);
- }
- return sum % BUCKETS;
- }
- int strsize(const char* s)
- {
- /* char* temp = s; */
- /* while(*(temp++));*/
- /* return (temp-s) - 1; */
- /* */
- /* char* i = &s;*/
- /* //for(int i = &s; *(s + i) != '\0'; i++);*/
- /* while(*i != '\0')*/
- /* {*/
- /* i++;*/
- /* }*/
- /* return (i - s);*/
- return 0;
- }
- bool streql(const char* a, const char* b)
- {
- for(int i = 0; a[i] != '\0' && b[i] != '\0'; i++)
- {
- if(toupper(a[i])!=toupper(b[i]))
- {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement