dict_t dict_remove(dict_t dict, word_t word) { assert(dict != NULL && word != NULL && dict_exists(dict, word)); index_t index = index_from_string(word); if(dict_exists(dict,word)) { dict->length = dict->length-1; dict->data = list_remove(dict->data,index); free(index); index = NULL; } return dict; }
dict_t dict_remove(dict_t dict, word_t word) { assert((dict != NULL) && (word != NULL) && (dict_exists(dict, word))); index_t index_word = index_from_string(word); list_t new_list = list_remove(dict->list, index_word); index_word = index_destroy(index_word); dict->list = new_list; dict->length--; return (dict); }
dict_t dict_add(dict_t dict, word_t word, def_t def) { assert(dict != NULL && word != NULL && def != NULL && (!dict_exists(dict, word))); index_t index_word = index_from_string(word); data_t data_def = data_from_string(def); list_t new_list = list_append(dict->list, index_word, data_def); dict->list = new_list; dict->length++; return (dict); }
def_t dict_search(dict_t dict, word_t word) { assert(dict != NULL && word != NULL && dict_exists(dict, word)); index_t palabra = NULL; palabra = index_from_string(word); data_t definition = list_search(dict->list, palabra); def_t result = data_to_string(definition); palabra = index_destroy(palabra); assert(result != NULL); return (result); }
dict_t dict_add(dict_t dict, word_t word, def_t def) { assert(dict != NULL && word != NULL && def != NULL && !dict_exists(dict,word)); index_t index = index_from_string(word); data_t data = data_from_string(def); dict->length = dict->length +1; dict->data = list_add(dict->data,index,data); return dict; }
def_t dict_search(dict_t dict, word_t word) { index_t index = index_from_string(word); assert(dict != NULL && word != NULL && dict_exists(dict,word)); def_t result = NULL; result = data_to_string(list_search(dict->data,index)); free(index); index=NULL; assert(result != NULL); return result; }
dict_t dict_remove(dict_t dict, word_t word) { /*Precondition verification*/ assert(word != NULL); assert(dict != NULL); assert(dict_exists(dict, word)); index_t index = index_from_string(word); dict->data = bst_remove(dict->data, index); dict->length -= 1; index_destroy(index); return (dict); }
def_t dict_search(dict_t dict, word_t word) { /*Precondition verification*/ assert(dict != NULL); assert(word != NULL); assert( dict_exists(dict, word) ); index_t index = index_from_string(word); data_t data = bst_search(dict->data, index); def_t definicion = data_to_string(data); index_destroy(index); /*Postcondition verification*/ assert(definicion != NULL); return (definicion); }
dict_t dict_add(dict_t dict, word_t word, def_t def) { /*Precondition verification*/ assert(dict != NULL); assert(word != NULL); assert(def != NULL); assert(!dict_exists(dict, word)); index_t index = index_from_string(word); data_t data = data_from_string(def); dict->length += 1; dict->data = bst_add(dict->data, index, data); /* POST: the elements of the result are the same as the one in 'dict' with * the new pair ('word', 'def') added.*/ return (dict); }
int main(void) { char *option = NULL, *word = NULL, *def = NULL, *filename = NULL; bool exit = false; dict_t dict = NULL,copy = NULL; dict = dict_empty(); do { option = print_menu(); switch (*option) { case ADD: printf("\tPlease enter the word to add to the dict: "); word = readline_from_stdin(); if (!dict_exists(dict, word)) { printf("\tPlease enter the definition: "); def = readline_from_stdin(); dict_add(dict,word,def); printf("\t-> The word and definition were added.\n"); free(def); def = NULL; } else { printf("\t-> The word %s already exists.\n", word); } assert(dict_exists(dict,word)); free(word); word = NULL; break; case COPY: copy = dict_copy(dict); printf("\t-> The dictionary was duplicated. Showing the duplicated dict:\n"); printf("{\n"); dict_dump(copy, stdout); printf("\n}\n"); copy = dict_destroy(copy); break; case DELETE: printf("\tPlease enter the word to delete from the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { dict = dict_remove(dict,word); printf("\t-> The word was successfully removed.\n"); } else { printf("\t-> The word %s does not exist.\n", word); } assert(!dict_exists(dict, word)); free(word); word = NULL; break; case DUMP: printf("\tPlease enter the filename to dump the dict to: "); filename = readline_from_stdin(); dict_to_file(dict,filename); printf("\t-> The dictionary was successfully dumped.\n"); free(filename); filename = NULL; break; case EMPTY: dict = dict_destroy(dict); dict = dict_empty(); printf("\t-> The dictionary was successfully emptied.\n"); break; case EXIT: printf("\t-> Exiting.\n"); exit = true; break; case LOAD: printf("\tPlease enter the filename to load the dict from: "); filename = readline_from_stdin(); copy = dict_from_file(filename); if (copy == NULL) { printf("Can not load dict from filename %s\n", filename); } else { dict = dict_destroy(dict); dict = copy; printf("\t-> The dictionary was successfully loaded.\n"); } free(filename); filename = NULL; break; case SEARCH: printf("\tPlease enter the word to search in the dict: "); word = readline_from_stdin(); if(dict_exists(dict,word)) { def = dict_search(dict,word); printf("\t-> The definition for \"%s\" is \"%s\".\n", word, def); free(def); def = NULL; } else { printf("\t-> The word \"%s\" does not exist in the dict.\n", word); } free(word); word = NULL; break; case SHOW: printf("{\n"); dict_dump(dict, stdout); printf("\n}\n"); break; case SIZE: printf("\t-> The size is %u.\n", dict_length(dict)); break; default: printf("\n\"%c\" is invalid. Please choose a valid option.\n\n", *option); } free(option); option = NULL; } while (!exit); dict = dict_destroy(dict); return (0); }