dictionary.c

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
4.0 kB
5
Indexable
#include <string.h>

#define WORD_LEN 20
#define TEXT_LEN 80
#define MAX_DEFS 10
#define MAX_WORDS 100

struct definition {
    char part_of_speech[WORD_LEN + 1];
    char text[TEXT_LEN + 1];
};

struct word {
    char theWord[WORD_LEN + 1];
    int num_of_defs;
    struct definition defs[MAX_DEFS];
};

struct dictionary {
    int num_of_words;
    struct word words[MAX_WORDS];
};

// Function that takes as input a dictionary and a word
// If the word does not already exist and dictionary has room, then
// * the function adds the word and returns 1
// If the word does exist or the dictionary is full, then
// * the dictionary is not modified and return 0
int addWord(struct dictionary * dict, char word[]);

// Function that takes as input
// * a dictionary,
// * a word,
// * the part of speech of the word, and
// * the text of the definition
// If the word does not exist and the dictionary has room, then
// * the function adds the word with the given definition & returns 1
// If the word does exist and can have the definition added, then
// * the function adds the definition
// If there is no room, then
// * the function does not modify the dictionary and returns 0
int addDef(struct dictionary * dict, char word[],
           char part_of_speech[], char text[]);
// Function that takes as input
// * a dictionary,
// * a word,
// If the word exists in the dictionary, then
// * return the address to the word in the dictionary
// If the word does not exist in the dictionary, then
// * return NULL
struct word * lookUp(struct dictionary * dict, char word[]);

/////////////////////////////////////////////////////////
////////////////// FUNCTION DEFINITION //////////////////
/////////////////////////////////////////////////////////

int addWord(struct dictionary * dict, char word[]) {
    // dictionary is full -> returns 0
    if (dict->num_of_words == MAX_WORDS) return 0;
    // check if word does exist with strcmp
    for (int i = 0; i < dict->num_of_words; i++) {
        if (strcmp(word, dict->words[i].theWord) == 0) {
            // word exists (same as input word) -> returns 0
            return 0;
        }
    }
    // word does not exist and dictionary has room
    // create new word
    struct word newWord;
    strcpy(newWord.theWord, word);
    // add new word to dict and increment word count, then return 1
    dict->words[dict->num_of_words++] = newWord;
    return 1;
}

int addDef(struct dictionary * dict, char word[],
           char part_of_speech[], char text[]) {
    for (int i = 0; i < dict->num_of_words; i++) {
        if (strcmp(word, dict->words[i].theWord) == 0) {
            // word exists
            // cannot add any definition (full) -> returns 1
            if (dict->words[i].num_of_defs == MAX_DEFS) return 0;
            // adding possible; create new definition
            struct definition newDef;
            strcpy(newDef.part_of_speech, part_of_speech);
            strcpy(newDef.text, text);
            // add to definition lists and increment count, then return 1
            dict->words[i].defs[dict->words[i].num_of_defs++] = newDef;
            return 1;
        }
    }
    // word does not exist & must be added
    // dict is full -> returns 0
    if (dict->num_of_words == MAX_WORDS) return 0;
    // dict has room -> add word to dict, add definition and return 1
    // (also return value of successful addDef)
    addWord(dict, word);
    return addDef(dict, word, part_of_speech, text);
}

struct word * lookUp(struct dictionary * dict, char word[]) {
    // simply loop through dict and check with strcmp
    for (int i = 0; i < dict->num_of_words; i++) {
        if (strcmp(word, dict->words[i].theWord) == 0) {
            // word exists; returns address
            return &dict->words[i];
        }
    }
    // word does not exist
    return NULL;
}