首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何集成前缀检查器以根据文件读取找到完整的单词

如何集成前缀检查器以根据文件读取找到完整的单词
EN

Stack Overflow用户
提问于 2018-11-14 14:49:31
回答 1查看 110关注 0票数 1

问题:I可以搜索文件并根据用户输入的数字打印单词(文本-消息号转换),“购买”我还应该能够根据输入的数字的一部分找到完整的单词,因此. 72将返回pa、ra、sa和sc.它会在我的文件中找到单词,如partyradiosandwichscanner

我尝试过使用前缀函数,但是我无法正确地集成它们。startsWith函数就是一个例子。

这些单词基于一个编号的键盘,如:字母电话键盘

代码:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdbool.h>


const char numbered_letters[10][5] = {"", "", "abc", "def", "ghi", "jkl",
                                      "mno", "pqrs", "tuv", "wxyz"};



bool startsWith(const char *pre, const char *str) {
    size_t lenpre = strlen(pre),
            lenstr = strlen(str);
    return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}

void isEqual(char input[]) {
    int i = 0;
    //input[strlen(input)] = 0;
    startsWith(input, input);
    printf("check:  %s\n", input);
    //creating file
    FILE *fp = fopen("words_alpha.txt", "r");
    char str[32]; /* Handles largest possible string in the list which is: dichlorodiphenyltrichloroethane*/
    //fseek(fp, 0, SEEK_SET);
    if (fp == NULL) {
        printf("Error! No file!\n");
    } else {
        //printf("Enter a number to be converted into a word-list");
        //scanf("%s", str);

        while (!feof(fp)) {
            fscanf(fp, "%s", str);
            i = strncmp(input, str, 32);
            if (i == 0) {
                printf("HIT:    %s \n", input);
                break;
            } else {
                printf("");
            }
            //if (strncmp(str, "hello", 32 ) == 0) { /*if strncmp finds the word and returns true, print */
            //  printf("%s\n", str);

        }
        //printf("%s\n", str);
        //compareNums(num);
    }
    fclose(fp);
}


void printWordsUtil(int number[], int curr_digit, char output[], int n) {

    // Base case, if current output word is prepared
    int i;
    if (curr_digit == n) {
        //printf("%s ", output);
        isEqual(output);
        return;

    }

    // Try all possible characters for current digit in number[]
    // and recur for remaining digits
    for (i = 0; i < strlen(numbered_letters[number[curr_digit]]); i++) {

        output[curr_digit] = numbered_letters[number[curr_digit]][i];
        printWordsUtil(number, curr_digit + 1, output, n);/* recursive call */


        if (number[curr_digit] == 0 || number[curr_digit] == 1)
            return;
    }
}

// A wrapper over printWordsUtil().  It creates an output array and
// calls printWordsUtil()
void printWords(int number[], int n) {
    char result[n + 1];
    result[n] = '\0';

    printWordsUtil(number, 0, result, n);


}

//Driver program
int main(void) {
    int number[] = {4, 3, 9};
    int n = sizeof(number) / sizeof(number[0]);
    printWords(number, n);
    return 0;
}

使用的文件: 字α(超过420 k字的文件)

感谢您能提供的任何指导!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-14 17:59:25

有几只虫子。

你的前缀功能没问题。你用它替换的strncmp不是。

在第一个单词匹配时停止的命中有一个break,因此没有显示后续的匹配。

在点击时,您打印的是前缀字符串,而不是单词字符串。

我已经用错误注释了你的代码并修复了它们,请原谅不必要的风格清理

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

const char numbered_letters[10][5] = { "", "", "abc", "def", "ghi", "jkl",
    "mno", "pqrs", "tuv", "wxyz"
};

bool
startsWith(const char *pre, const char *str)
{
    size_t lenpre = strlen(pre),
        lenstr = strlen(str);

    return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}

void
isEqual(char input[])
{
    int i = 0;
    int ilen = strlen(input);

    // input[strlen(input)] = 0;
// NOTE/BUG: this is extraneous
#if 0
    startsWith(input, input);
#endif
    printf("check:  '%s'\n", input);

    // creating file
    FILE *fp = fopen("words_alpha.txt", "r");
// NOTE/BUG: this needs to be one more to contain the nul terminater
#if 0
    /* Handles largest possible string in the list which is:
        dichlorodiphenyltrichloroethane */
    char str[32];
#else
    char str[33];
#endif

    // fseek(fp, 0, SEEK_SET);
    if (fp == NULL) {
        printf("Error! No file!\n");
    }
    else {
        // printf("Enter a number to be converted into a word-list");
        // scanf("%s", str);

// NOTE/BUG: although feof works here, it is considered _bad_ practice
#if 0
        while (!feof(fp)) {
            fscanf(fp, "%s", str);
#else
        while (1) {
            if (fscanf(fp, "%s", str) != 1)
                break;
#endif

// NOTE/BUG: this is broken
#if 0
            i = strncmp(input, str, 32) == 0;
#endif
// NOTE: this works and is simpler than startsWith (which also works)
#if 0
            i = strncmp(input, str, ilen) == 0;
#endif
#if 1
            i = startsWith(input, str);
#endif

            if (i) {
// NOTE/BUG: we want the actual word to be printed and not just the prefix
#if 0
                printf("HIT:    %s\n", input);
#else
                printf("HIT:    %s\n", str);
#endif
// NOTE/BUG: this break stops on the _first_ word match in the list but we
// want all of them
#if 0
                break;
#endif
            }
            else {
                //printf("");
            }
            // if (strncmp(str, "hello", 32 ) == 0) { /*if strncmp finds the word and returns true, print */
            // printf("%s\n", str);

        }
        // printf("%s\n", str);
        // compareNums(num);
    }
    fclose(fp);
}

void
printWordsUtil(int numbers[], int curr_digit, char output[], int n)
{

    // Base case, if current output word is prepared
    int i;

    if (curr_digit == n) {
        // printf("%s ", output);
        isEqual(output);
        return;
    }

// NOTE: did some cleanup to understand what was going on -- [probably] not a
// bug
    int numcur = numbers[curr_digit];
    const char *letters = numbered_letters[numcur];
    int letlen = strlen(letters);

    // Try all possible characters for current digit in number[]
    // and recur for remaining digits
    for (i = 0; i < letlen; ++i) {
        output[curr_digit] = letters[i];
        printWordsUtil(numbers, curr_digit + 1, output, n); /* recursive call */
        if ((numcur == 0) || (numcur == 1))
            break;
    }
}

// A wrapper over printWordsUtil().  It creates an output array and
// calls printWordsUtil()
void
printWords(int number[], int n)
{
    char result[n + 1];

// NOTE/BUG: this will have garbage in elements 0 to (n - 1)
// NOTE/BUG: result is not used otherwise
    result[n] = '\0';

    printWordsUtil(number, 0, result, n);

}

//Driver program
int
main(void)
{
    int number[] = { 4, 3, 9 };
    int n = sizeof(number) / sizeof(number[0]);

    printWords(number, n);
    return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53302921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档