【发布时间】:2020-05-04 11:20:09
【问题描述】:
我正在学习 C 语言的 cs50 在线课程,我有一个项目是编写一个函数,将字典(单词数据库)加载到内存中,有效地将其散列到散列表中,然后进行比较给定文本中的单词要检查拼写,必须在不区分大小写的情况下进行比较。
我通过这个概述完成了我遇到的主要问题是降低了我的大小写敏感性,因此我不确定我在下面的 strcasecmp 函数中出了什么问题。我知道内存泄漏,但稍后会解决它们。
下面是C语言代码:
//for the universal hash function
#define BASE 256
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 676;
// Hash table
node *table[N];
int word_count = 0;
// Returns true if word is in dictionary else false
//Require a search funtion
bool check(const char *word)
{
int hashIndex = hash(word);
for (node *tmp = table[hashIndex]; tmp != NULL; tmp = tmp->next)
{
if (strcasecmp(word, tmp->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
// the dividing hash function is one I cited from the yale.edu page http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)HashTables.html having worked with.
unsigned int hash(const char *word)
{
unsigned long m = 11;
unsigned long h;
unsigned const char *us;
//ensure element value is >= 0
us = (unsigned const char *) word;
h = 0;
while(*us != '\0')
{
h = (h * BASE + *us) % m;
us++;
}
return (h % N);
}
当使用 CS50 提供的 check50 功能检查时,我得到:
> :( spell-checking is case-insensitive expected "MISSPELLED WOR...",
> not "MISSPELLED WOR..." Log running ./speller case/dict case/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 1\nWORDS IN TEXT: 8\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8 Actual Output: MISSPELLED WORDS
>
> foO fOo Foo fOO FoO FOo FOO
>
> WORDS MISSPELLED: 7 WORDS IN DICTIONARY: 1 WORDS IN TEXT:
> 8
>
> :( handles most basic words properly expected "MISSPELLED WOR...", not
> "MISSPELLED WOR..." Log running ./speller basic/dict basic/text...
> checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS
> IN DICTIONARY: 8\nWORDS IN TEXT: 9\n"...
>
> Expected Output: MISSPELLED WORDS
>
>
> WORDS MISSPELLED: 0 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9 Actual Output: MISSPELLED WORDS
>
> The
>
> WORDS MISSPELLED: 1 WORDS IN DICTIONARY: 8 WORDS IN TEXT:
> 9
主要问题是:如何在不区分大小写的情况下比较两个字符串?
【问题讨论】:
-
unload函数只释放每个列表中的第一个条目。