Cs50 拼写器:无法识别任何不正确的单词
Cs50 speller: not recognising any incorrect words
我目前正在开发 CS50 Speller 功能。我设法编译了我的代码并完成了完整程序的原型,但是它不起作用(它不识别任何拼写错误的单词)。我一次查看一个函数并打印出它们的输出以查看内部发生的情况。
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
char word[LENGTH + 1];
int counter = 0;
FILE *dicptr = fopen(dictionary, "r");
if (dicptr == NULL)
{
printf("Could not open file\n");
return 1;
}
while (fscanf(dicptr, "%s", word) != EOF)
{
printf("%s", word);
node *n = malloc(sizeof(node));
if (n == NULL)
{
unload();
printf("Memory Error\n");
return false;
}
strcpy(n->word, word);
int h = hash(n->word);
n->next = table[h];
table[h] = n;
amount++;
}
fclose(dicptr);
return true;
}
据我所知,这工作正常。这让我想知道问题是否出在我的检查功能上,如下所示:
bool check(const char *word)
{
int n = strlen(word);
char copy[n + 1];
copy[n] = '[=11=]';
for(int i = 0; i < n; i++)
{
copy[i] = tolower(word[i]);
printf("%c", copy[i]);
}
printf("\n");
node *cursor = table[hash(copy)];
while(cursor != NULL)
{
if(strcasecmp(cursor->word, word))
{
return true;
}
cursor = cursor->next;
}
return false;
}
如果有眼尖的人能看出问题所在,我将不胜感激,因为我很困惑。第一个函数用于将字典中的单词加载到哈希 table\linked 列表中。第二个函数应该检查 txt 文件的单词,看它们是否与链接列表中的任何术语匹配。如果不是,那么它们应该被视为不正确。
这个if(strcasecmp(cursor->word, word))
是个问题。来自 man strcasecmp:
Return Value
The strcasecmp() and strncasecmp() functions return an
integer less than, equal to, or greater than zero if s1 (or the first
n bytes thereof) is found, respectively, to be less than, to match, or
be greater than s2.
如果单词 匹配 ,它 returns 0,计算结果为 false。
我目前正在开发 CS50 Speller 功能。我设法编译了我的代码并完成了完整程序的原型,但是它不起作用(它不识别任何拼写错误的单词)。我一次查看一个函数并打印出它们的输出以查看内部发生的情况。
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
char word[LENGTH + 1];
int counter = 0;
FILE *dicptr = fopen(dictionary, "r");
if (dicptr == NULL)
{
printf("Could not open file\n");
return 1;
}
while (fscanf(dicptr, "%s", word) != EOF)
{
printf("%s", word);
node *n = malloc(sizeof(node));
if (n == NULL)
{
unload();
printf("Memory Error\n");
return false;
}
strcpy(n->word, word);
int h = hash(n->word);
n->next = table[h];
table[h] = n;
amount++;
}
fclose(dicptr);
return true;
}
据我所知,这工作正常。这让我想知道问题是否出在我的检查功能上,如下所示:
bool check(const char *word)
{
int n = strlen(word);
char copy[n + 1];
copy[n] = '[=11=]';
for(int i = 0; i < n; i++)
{
copy[i] = tolower(word[i]);
printf("%c", copy[i]);
}
printf("\n");
node *cursor = table[hash(copy)];
while(cursor != NULL)
{
if(strcasecmp(cursor->word, word))
{
return true;
}
cursor = cursor->next;
}
return false;
}
如果有眼尖的人能看出问题所在,我将不胜感激,因为我很困惑。第一个函数用于将字典中的单词加载到哈希 table\linked 列表中。第二个函数应该检查 txt 文件的单词,看它们是否与链接列表中的任何术语匹配。如果不是,那么它们应该被视为不正确。
这个if(strcasecmp(cursor->word, word))
是个问题。来自 man strcasecmp:
Return Value
The strcasecmp() and strncasecmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
如果单词 匹配 ,它 returns 0,计算结果为 false。