PSET5拼写错误单词数量巨大

时间:2020-02-25 03:50:46

标签: python-3.x cs50

我一直在运行该解决方案,但收到大量拼写错误的单词。 误导词:15904,而员工误导词:955

除此之外,单词计数准确,运行时还可以。 我怀疑问题可能来自检查/加载功能,但我不确定是什么原因引起的。 其他一些代码在check函数中实现了“小写”,但是我认为(strcasecmp)可以完成字符串之间的比较工作,而无需区分大小写。

检查功能

bool check(const char *word)
{

    int hashInt = hash(word);

    if (table[hashInt] == NULL)
    {
        return 1;
    }

    node *cursor = table[hashInt];

    while (cursor != NULL)
    {

        int i = strcasecmp(cursor -> word, word);

        if (i == 0)
        {
            return 0;
            break;
        }

        cursor = cursor -> next;
    }

    return false;
}

加载功能

bool load(const char *dictionary)
{

    FILE *file = fopen(dictionary, "r");

    if (file == NULL)
    {
        printf("error opening file");
        return 1;
    }

    char word [LENGTH + 1];

    while (fscanf(file, "%s\n", word) != EOF)
    {

        int hashInt = hash(word);

        node *n = malloc(sizeof(node));

        if (n == NULL)
        {
            unload();
            return 1;
        }



        if (table[hashInt] == NULL)
        {
            table[hashInt] = n;
        }
        else
        {
            n -> next = table[hashInt];
            table[hashInt] = n;
        }

        strcpy(n -> word, word);
        wordLoaded++;
    }

    fclose(file);

    return true;
}

哈希函数

unsigned int hash(const char *word)
{
    unsigned int hash = 0;

    for (int i = 0, n = strlen(word); i < n; i++)
        hash = (hash << 2) ^ word[i];

    return hash % N;

    return 0;
}

1 个答案:

答案 0 :(得分:0)

根据规格:

dictionary被假定为包含小写字母列表的文件 字

您的check实现必须不区分大小写。

只要此int i = strcasecmp(cursor -> word, word);也不区分大小写,则该int hashInt = hash(word);看起来就可以满足的要求。 las,不是。 hash(“ A”)和hash(“ a”)将返回不同的值。