cs50 - cs50 拼写器不处理大多数单词并且大小写不敏感
问题描述
我已经尝试了所有我知道如何解决这个问题的方法。我尝试调整 strcompcase() 函数以反映它返回的整数,但我可能不知道正确的方法,并且我尝试在散列之前将所有文本转换为小写并仅使用 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 值用小写字母计算你的单词我希望这能工作!
推荐阅读
- python - 将 csv 数据转换为逗号的单独列表
- html - vs代码中的实时服务器
- c++ - 在 MPI 上下文中使用 boost-odeint 而不使用 mpi_state 会导致自适应步进器的不同时间步长
- java - Java:泛型,具有多个边界的通配符
- python - 如何计算 groupby 列的百分比并按降序排序?
- terraform - 无法销毁 eip 关联资源:ErrorCode: IncorrectEipStatus
- swift - 如何设置本地通知内容与打开时主屏幕显示的内容相同?[迅速]
- javascript - 无法从循环外部引用变量
- reactjs - 带有 StrictMode 的 ReactJS 吞下了一些日志消息
- .net - 使用 Mediatr 管道进行单元测试