Python 3.x PSET5拼写器大量拼写错误的单词
我一直在运行该解决方案,但遇到大量拼写错误的单词。 拼写错误的单词:15904,而员工拼写错误的单词:955 除此之外,字数是准确的,运行时是正常的。 我怀疑问题可能来自检查/加载功能,但我不确定是什么原因造成的。 其他一些代码在check函数中实现了“to lowercase”,但我认为(strcasecmp)可以完成字符串之间的比较工作,而不考虑大小写 检查功能Python 3.x PSET5拼写器大量拼写错误的单词,python-3.x,cs50,Python 3.x,Cs50,我一直在运行该解决方案,但遇到大量拼写错误的单词。 拼写错误的单词:15904,而员工拼写错误的单词:955 除此之外,字数是准确的,运行时是正常的。 我怀疑问题可能来自检查/加载功能,但我不确定是什么原因造成的。 其他一些代码在check函数中实现了“to lowercase”,但我认为(strcasecmp)可以完成字符串之间的比较工作,而不考虑大小写 检查功能 bool check(const char *word) { int hashInt = hash(word);
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;
}
无符号整数散列(const char*word)
{
无符号整数散列=0;
for(int i=0,n=strlen(word);i
字典
假定为包含小写字母列表的文件
言语
及
check的实现必须不区分大小写
这个int i=strcasecmp(游标->单词,单词);
看起来可以满足要求,只要这个int hashInt=hash(单词);
也不区分大小写。唉,它不是;hash(“A”)和hash(“A”)将返回不同的值。哈希函数是否考虑大小写?不,我认为没有。添加了哈希函数。我相信它只是筛选单词并对其进行哈希运算