Speller 对某些词有效,对其他词无效。不知道问题出在哪里
Speller works for some words not others. Dont know where the problem is
我是CS50第4题,拼写,hashtable版
代码可以正确检测字典和文本中的单词数。但是它拼错了单词。
// Loads dictionary
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
node *new_node = malloc(sizeof(node));
int j = hash(word);
// check if out of memory
if (new_node == NULL)
{
unload();
return false;
}
else
{
strcpy(new_node->word, word);
new_node->next = hashtable[j];
hashtable[j] = new_node;
}
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded
{
unsigned int count = 0;
for(int i = 0; i < 26; i++)
{
node *cursor = hashtable[i];
while (cursor != NULL)
{
cursor = cursor->next;
count++;
}
}
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int i = hash(word);
node *chk = hashtable[i];
while (chk != NULL)
{
if (strcmp(chk->word, word))
{
return true;
}
chk = chk->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
for (int i = 0; i < N; i++)
{
node *cursor = hashtable[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}
如果选择大词典,拼写错误的单词始终为 0(无论文本是什么),如果选择小词典(cat.txt),它会按预期显示。任何定制词典,将一些正确的单词显示为拼写错误的单词。
来自 man strcmp
[已添加重点]:
RETURN VALUE
The strcmp() and strncmp() 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.
您可以通过 debug50 获取一个定制的小词典,看看在这一行会发生什么:
if (strcmp(chk->word, word))
因为 strcmp
returns 一个 int
,所以像 int
一样测试它,而不是 bool
。不要忘记:检查器应该区分大小写 in!
我是CS50第4题,拼写,hashtable版
代码可以正确检测字典和文本中的单词数。但是它拼错了单词。
// Loads dictionary
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
node *new_node = malloc(sizeof(node));
int j = hash(word);
// check if out of memory
if (new_node == NULL)
{
unload();
return false;
}
else
{
strcpy(new_node->word, word);
new_node->next = hashtable[j];
hashtable[j] = new_node;
}
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded
{
unsigned int count = 0;
for(int i = 0; i < 26; i++)
{
node *cursor = hashtable[i];
while (cursor != NULL)
{
cursor = cursor->next;
count++;
}
}
return count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int i = hash(word);
node *chk = hashtable[i];
while (chk != NULL)
{
if (strcmp(chk->word, word))
{
return true;
}
chk = chk->next;
}
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
for (int i = 0; i < N; i++)
{
node *cursor = hashtable[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}
如果选择大词典,拼写错误的单词始终为 0(无论文本是什么),如果选择小词典(cat.txt),它会按预期显示。任何定制词典,将一些正确的单词显示为拼写错误的单词。
来自 man strcmp
[已添加重点]:
RETURN VALUE
The strcmp() and strncmp() 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.
您可以通过 debug50 获取一个定制的小词典,看看在这一行会发生什么:
if (strcmp(chk->word, word))
因为 strcmp
returns 一个 int
,所以像 int
一样测试它,而不是 bool
。不要忘记:检查器应该区分大小写 in!