Exemple #1
0
int parse_and_print_to_stdout(const char * operation)
{
    if (NULL == operation)
    {
        skip_line(stdin);
        return -1;
    }
    if (0 == strcmp(OP_INSERT, operation))
    {
        if (0 != parse_insert(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int word_num = trie_insert(word_buffer);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_PREV, operation))
    {
        int number, start, end;
        if (0 != parse_prev(stdin, &number, &start, &end))
            return -1;
        int word_num = trie_prev(number, start, end);
        if (word_num < 0)
            return -1;
        printf("word number: %d\n", word_num);
    }
    if (0 == strcmp(OP_DELETE, operation))
    {
        int number;
        if (0 != parse_delete(stdin, &number))
            return -1;
        int word_num = trie_delete(number);
        if (word_num < 0)
            return -1;
        printf("deleted: %d\n", number);
    }
    if (0 == strcmp(OP_FIND, operation))
    {
        if (0 != parse_find(stdin, word_buffer, MAX_WORD_LEN))
            return -1;
        int find_result = trie_find(word_buffer, strlen(word_buffer));
        if (find_result < 0)
            return -1;
        puts(find_result == 0 ? "YES" : "NO");
    }
    if (0 == strcmp(OP_CLEAR, operation))
    {
        if (0 != parse_clear(stdin))
            return -1;
        if (0 != trie_clear())
            return -1;
        puts("cleared");
    }
    return 0;
}
Exemple #2
0
/* Inserts a fragment of a word to a dictionary and returns true
 * if parameters decribe correct word which doesn't exist in a dictionary;
 * otherwise query is ignored and function returns false. */
bool prev(char *params) {
    unsigned int number, start, end;
    int prev_result;
    if (get_number_from_string(&params, &number)
		&& get_number_from_string(&params, &start)
        && get_number_from_string(&params, &end) && is_string_blank(&params)
        && (prev_result = trie_prev(number, start, end)) != -1) {
            printf("word number: %d\n", prev_result);
            return true;
    }
    return false;
}