在这里我需要任何帮助。我在CS50课程中解决了问题5,并且代码成功编译,当我手动测试它时,它没有问题。但是我尝试用$ check50 cs50/problems/2020/x/speller
进行检查,并给了this result。
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
char oneword[LENGTH + 1];
int counter = 0 ;
// Number of buckets in hash table
const int HASHTABLE_SIZE = 65536;
// Hash table
node *table[HASHTABLE_SIZE];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
char lowerWord[LENGTH + 1];
for (int i = 0; i <LENGTH; i++)
{
lowerWord[i] = tolower(word[i]);
}
int x = hash (lowerWord);
for (node *tmp = table[x] ; tmp != NULL; tmp = tmp->next)
{
if (strcasecmp(tmp->word, word)==0)
{
return true;
}
}
return false;
}
// Hashes word to a number
//https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9nlkn/
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 % HASHTABLE_SIZE;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// TODO
FILE *dict = NULL;
dict = fopen("dictionaries/large", "r");
int x = 0;
table[x] = malloc(sizeof(node));
if (dict != NULL)
{
while (true)
{
if (feof(dict))
{
break;
}
fscanf (dict, "%s", oneword);
node *h = malloc(sizeof(node));
if (h == NULL)
{
return 1;
}
strcpy(h->word, oneword);
h->next = NULL;
x = hash (h->word);
h->next = table[x];
table[x] = h;
counter++;
}
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return counter;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
node *cursor= NULL;
for (int i = 0; i > HASHTABLE_SIZE; i++)
{
for (cursor = table[i] ; cursor != NULL; cursor = cursor->next)
{
node *tmp = cursor;
cursor = cursor->next;
free (tmp);
}
free (cursor);
}
return true;
}
答案 0 :(得分:0)
仔细查看check50报告中的“单词词典”结果。它没有使用大型词典。该程序被硬编码查找“字典/大”,并且它不会检测到fopen是否成功。可以推测CS50服务器上不存在“字典/大字典”,并且该程序立即由于分段错误而失败。