cs50 Speller 不处理大多数单词并且不区分大小写
cs50 Speller does not handle most words and case is not insensitive
我已尽我所能解决此问题。我已经尝试调整 strcompcase() 函数以反映它的整数 returns,但我可能不知道正确的方法,我尝试在散列之前将所有文本转换为小写并仅使用 strcmp () .这不仅不能解决问题,而且我还有内存问题。
我假设这是我的检查功能的问题,但也可能不是。我真的可以听取真正知道自己在做什么的人的建议,而我绝对不会。
提前致谢。
// Implements a dictionary's functionality
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// 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 = 200000;
// Hash table
node *table[N];
unsigned int dict_size = 0;
// Returns true if word is in dictionary else false
bool check(const char *word) {
// getting hash value of word
int index = hash(word);
// declare travel point to navigate table
node *trav = table[index];
while (trav != NULL) {
// comparison to find if word is present in table
if (strcasecmp (trav->word, word) == 0) {
return true;
} else {
trav = trav->next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word) {
// hash function found https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
unsigned int index = 0;
for (int i = 0, n=strlen(word); i < n; i++) {
index = (index << 2) ^ word[i];
}
return index % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary) {
// declaring buffer array
char dict_word [LENGTH + 1];
// opening dictionary file
FILE *dict = fopen(dictionary, "r");
// double checking if file opened correctly
if (dict == NULL) {
printf("Dictionary did not open.\n");
return false;
}
// looping through until end of file
while (fscanf (dict, "%s\n", dict_word) != EOF) {
// hashing word to check
int index = hash(dict_word);
node *new_node = malloc (sizeof(node));
// making sure malloc worked
if (new_node == NULL) return false;
// checks if index is empty
if (table [index] == NULL) {
// points index to new node
table [index] = new_node;
// points next field to NULL
new_node->next = NULL;
// if not empty
} else {
// insert new node at front of list
new_node->next = table [index];
// makes new node head of list
table [index] = new_node;
}
// insert word into new node
strcpy(new_node->word, dict_word);
dict_size ++;
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void) {
return dict_size;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void) {
for (int i = 0; i < N; i++) {
// creates new travel pointer
node *cursor = table [i];
while (cursor != NULL) {
// creates temporary pointer, synch with cursor
node *tmp = cursor;
cursor = cursor->next;
// free what tmp is pointing at
free(tmp);
}
}
return true;
}
我在执行此操作时遇到了同样的问题...您在将单词转换为小写之前对其进行了哈希处理。这是您的检查功能:
// Returns true if word is in dictionary else false
bool check(const char *word) {
//Just converting the word to lowercase
char* lower_word = strcmp(word)
// getting hash value of word
int index = hash(lower_word);
// declare travel point to navigate table
node *trav = table[index];
while (trav != NULL) {
// comparison to find if word is present in table
if (strcasecmp (trav->word, word) == 0) {
return true;
} else {
trav = trav->next;
}
}
return false;
}
我遇到的问题是我将作品转换为小写,所以ASCII值会用小写字母计算你的单词
我希望这有效!
我已尽我所能解决此问题。我已经尝试调整 strcompcase() 函数以反映它的整数 returns,但我可能不知道正确的方法,我尝试在散列之前将所有文本转换为小写并仅使用 strcmp () .这不仅不能解决问题,而且我还有内存问题。
我假设这是我的检查功能的问题,但也可能不是。我真的可以听取真正知道自己在做什么的人的建议,而我绝对不会。
提前致谢。
// Implements a dictionary's functionality
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// 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 = 200000;
// Hash table
node *table[N];
unsigned int dict_size = 0;
// Returns true if word is in dictionary else false
bool check(const char *word) {
// getting hash value of word
int index = hash(word);
// declare travel point to navigate table
node *trav = table[index];
while (trav != NULL) {
// comparison to find if word is present in table
if (strcasecmp (trav->word, word) == 0) {
return true;
} else {
trav = trav->next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word) {
// hash function found https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
unsigned int index = 0;
for (int i = 0, n=strlen(word); i < n; i++) {
index = (index << 2) ^ word[i];
}
return index % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary) {
// declaring buffer array
char dict_word [LENGTH + 1];
// opening dictionary file
FILE *dict = fopen(dictionary, "r");
// double checking if file opened correctly
if (dict == NULL) {
printf("Dictionary did not open.\n");
return false;
}
// looping through until end of file
while (fscanf (dict, "%s\n", dict_word) != EOF) {
// hashing word to check
int index = hash(dict_word);
node *new_node = malloc (sizeof(node));
// making sure malloc worked
if (new_node == NULL) return false;
// checks if index is empty
if (table [index] == NULL) {
// points index to new node
table [index] = new_node;
// points next field to NULL
new_node->next = NULL;
// if not empty
} else {
// insert new node at front of list
new_node->next = table [index];
// makes new node head of list
table [index] = new_node;
}
// insert word into new node
strcpy(new_node->word, dict_word);
dict_size ++;
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void) {
return dict_size;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void) {
for (int i = 0; i < N; i++) {
// creates new travel pointer
node *cursor = table [i];
while (cursor != NULL) {
// creates temporary pointer, synch with cursor
node *tmp = cursor;
cursor = cursor->next;
// free what tmp is pointing at
free(tmp);
}
}
return true;
}
我在执行此操作时遇到了同样的问题...您在将单词转换为小写之前对其进行了哈希处理。这是您的检查功能:
// Returns true if word is in dictionary else false
bool check(const char *word) {
//Just converting the word to lowercase
char* lower_word = strcmp(word)
// getting hash value of word
int index = hash(lower_word);
// declare travel point to navigate table
node *trav = table[index];
while (trav != NULL) {
// comparison to find if word is present in table
if (strcasecmp (trav->word, word) == 0) {
return true;
} else {
trav = trav->next;
}
}
return false;
}
我遇到的问题是我将作品转换为小写,所以ASCII值会用小写字母计算你的单词 我希望这有效!