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 <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include "dictionary.h"
- int count = 0;
- bool opened = false;
- struct dict_node *wordArr[LLLength];
- struct dict_node *temp;
- struct dict_node *prev;
- struct dict_node *start[LLLength];
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char* word){
- char converted[LENGTH] = "";
- bool found = true;
- // TODO
- if(strlen(word) == 0){
- return false;
- }
- for(int i = 0; i < strlen(word); i++){
- converted[i] = tolower(word[i]);
- }
- for(int i = 0; i < LLLength; i++){
- wordArr[i] = start[i];
- while(wordArr[i]->next != NULL){
- if(strcmp(wordArr[i]->word,converted) == 0){
- break;
- }
- else{
- wordArr[i] = wordArr[i]->next;
- }
- }
- }
- return found;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary){
- // TODO
- FILE *dict = fopen(dictionary, "r");
- if(opened){
- fclose(dict);
- return false;
- }
- opened = true;
- if(dict == NULL || !dict){
- return false;
- }
- char line[LENGTH];
- bool filling;
- for(int i = 0; i < LLLength; i++){
- struct dict_node *newNode;
- newNode = malloc(sizeof(dict_node));
- wordArr[i] = newNode;
- start[i] = wordArr[i];
- }
- while(!feof(dict)){
- for(int i = 0; i < LLLength; i++){
- filling = true;
- while(filling)
- fgets(line,LENGTH,dict);
- strcpy(wordArr[i]->word,line);
- prev = wordArr[i];
- wordArr[i]->next = malloc(sizeof(dict_node));
- wordArr[i] = wordArr[i]->next;
- fgets(line,LENGTH,dict);
- strcpy(wordArr[i]->word,line);
- }
- }
- fclose(dict);
- for(int i = 0; i < LLLength; i++){
- wordArr[i] = start[i];
- while(wordArr[i]->next != NULL){
- wordArr[i] = wordArr[i]->next;
- count++;
- }
- }
- int test = 0;
- wordArr[test] = start[test];
- while(wordArr[test]->next != NULL){
- printf("%s",wordArr[test]->word);
- wordArr[test] = wordArr[test]->next;
- }
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void){
- // TODO
- if(opened){
- return count;
- }
- else{
- return 0;
- }
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void){
- // TODO
- for(int i = 0; i < LENGTH; i++){
- while(wordArr[i] != NULL){
- temp = wordArr[i];
- wordArr[i] = wordArr[i]->next;
- free(temp);
- }
- }
- for(int i = 0; i < LENGTH; i++){
- if(wordArr[i] != NULL){
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement