我正在用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);
}当使用由check50提供的CS50函数进行检查时,我得到:
> :( 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主要的问题是:我如何才能不区分大小写,来比较两个字符串呢?
发布于 2020-05-04 12:07:30
这个if (strcasecmp(word, tmp->word) == 0)并不是单词大小写有意义的唯一地方。那hash呢?它还需要考虑字母大小写,例如'a‘和'A’将散列成不同的值。
https://stackoverflow.com/questions/61591006
复制相似问题