Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * dictionary.c
- *
- * Computer Science 50
- * Problem Set 6
- *
- * Implements a dictionary's functionality.
- ***************************************************************************/
- #include <stdbool.h>
- #include<stdio.h>
- #include "dictionary.h"
- #include<stdlib.h>
- #include<string.h>
- /**
- * Returns true if word is in dictionary else false.
- */
- typedef struct node
- {
- int check ;
- struct node* letter[10];
- }node;
- struct node* root = NULL;
- struct node* createnode()
- {
- struct node* new_node = malloc(sizeof(node));
- for(int i = 0 ; i < 10 ;i++)
- new_node->letter[i] = NULL;
- return new_node;
- }
- int i = 0 ;
- bool check(const char* word)
- {
- // TODO
- struct node* check = root;
- for(i = 0 ; i < strlen(word) ; i++)
- {
- int temp = atoi(&word[i]);
- if(check->letter[i] != NULL)
- check = check->letter[temp];
- else
- break;
- }
- if(check->check==1)
- return true;
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- // TODO
- FILE* fp = NULL;
- fp = fopen(dictionary,"r");
- if(fp == NULL)
- return false;
- char word[20];
- while(fscanf(fp,"%s",word) != EOF)
- {
- if(root == NULL)
- root = createnode();
- struct node* next_node = root;
- for(i= 0; i < strlen(word) ; i++)
- {
- int temp = atoi(&word[i]) - 'a' ;
- if(next_node->letter[temp] == NULL)
- next_node->letter[temp] = createnode();
- next_node = next_node->letter[temp];
- }
- next_node->check = 1;
- }
- fclose(fp);
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- // TODO
- return 0;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment