Check50 Errros:未通过 "handles substrings properly" 测试。 Check50 也无法检查内存错误
Check50 Errros: Not passing "handles substrings properly" tests. Check50 also can't check for a memory errors
我正在研究 Pset5 Speller。我使用 base 26 的二进制数的概念制作了哈希函数。因此单个字母单词存储在索引 1-26 中,包含撇号的单词存储在索引 0 中,其余的使用前两个字母表进行哈希处理。我尝试调试代码,运行 Valgrind 仍然无法找到 Substring 测试失败的原因,任何建议
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <cs50.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 = (26 * 26) + 26 + 1 ;
// Hash table
node *table[N];
int numberofwords = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
int index = hash(word);
node *checker;
checker = table[index];
while (checker != NULL)
{
int comp = strcasecmp(word, checker->word);
if (comp == 0)
{
return true;
}
else
{
checker = checker->next;
}
}
// TODO
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int value = 0;
int length = strlen(word);
for (int i = 0; i < length; i++)
{
char c = word[i];
if (c == '\'')
{
value = 0;
return value;
}
}
if (length == 1)
{
value = tolower(word[0]) - 'a' + 1;
return value;
}
else
{
value = (26 * (tolower(word[1]) - 'a' + 1)) + (tolower(word[0]) - 'a' + 1);
return value;
}
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
//open dictionary and loop till EOF
char buffer[LENGTH + 1];
FILE *temp = fopen(dictionary, "r");
if (temp == NULL)
{
return false;
}
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
while (fscanf(temp, "%s", buffer) != EOF)
{
node *temp_node = malloc(sizeof(node));
if (temp_node == NULL)
{
return false;
}
strcpy(temp_node->word, buffer);
temp_node->next = NULL;
//hash the word
int locate = hash(buffer);
if (table[locate] == NULL)
{
table[locate] = temp_node;
}
else
{
temp_node->next = table[locate];
table[locate] = temp_node -> next;
}
numberofwords++;
}
fclose(temp);
// TODO
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return numberofwords;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *start = table[i];
node *freeer = start;
node *remember = start;
while (remember != NULL)
{
remember = remember->next;
free(freeer);
freeer = remember;
}
}
// TODO
return true;
}
这里是link错误
https://submit.cs50.io/check50/ef7901d85e821af117b76bf7a2f43f44f38e3301
这看起来像是加载中的问题:
temp_node->next = table[locate];
table[locate] = temp_node -> next;
它看起来是圆形的,它们相互指向。
尝试使用小型词典和一个包含“实际输出”窗格中的 5 个单词以及单词 cat 的文本文件进行调试。
check50因为测试失败没有检查内存错误(直到皱眉头翻过来才能检查")
我正在研究 Pset5 Speller。我使用 base 26 的二进制数的概念制作了哈希函数。因此单个字母单词存储在索引 1-26 中,包含撇号的单词存储在索引 0 中,其余的使用前两个字母表进行哈希处理。我尝试调试代码,运行 Valgrind 仍然无法找到 Substring 测试失败的原因,任何建议
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <cs50.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 = (26 * 26) + 26 + 1 ;
// Hash table
node *table[N];
int numberofwords = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
int index = hash(word);
node *checker;
checker = table[index];
while (checker != NULL)
{
int comp = strcasecmp(word, checker->word);
if (comp == 0)
{
return true;
}
else
{
checker = checker->next;
}
}
// TODO
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int value = 0;
int length = strlen(word);
for (int i = 0; i < length; i++)
{
char c = word[i];
if (c == '\'')
{
value = 0;
return value;
}
}
if (length == 1)
{
value = tolower(word[0]) - 'a' + 1;
return value;
}
else
{
value = (26 * (tolower(word[1]) - 'a' + 1)) + (tolower(word[0]) - 'a' + 1);
return value;
}
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
//open dictionary and loop till EOF
char buffer[LENGTH + 1];
FILE *temp = fopen(dictionary, "r");
if (temp == NULL)
{
return false;
}
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
while (fscanf(temp, "%s", buffer) != EOF)
{
node *temp_node = malloc(sizeof(node));
if (temp_node == NULL)
{
return false;
}
strcpy(temp_node->word, buffer);
temp_node->next = NULL;
//hash the word
int locate = hash(buffer);
if (table[locate] == NULL)
{
table[locate] = temp_node;
}
else
{
temp_node->next = table[locate];
table[locate] = temp_node -> next;
}
numberofwords++;
}
fclose(temp);
// TODO
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return numberofwords;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *start = table[i];
node *freeer = start;
node *remember = start;
while (remember != NULL)
{
remember = remember->next;
free(freeer);
freeer = remember;
}
}
// TODO
return true;
}
这里是link错误
https://submit.cs50.io/check50/ef7901d85e821af117b76bf7a2f43f44f38e3301
这看起来像是加载中的问题:
temp_node->next = table[locate];
table[locate] = temp_node -> next;
它看起来是圆形的,它们相互指向。
尝试使用小型词典和一个包含“实际输出”窗格中的 5 个单词以及单词 cat 的文本文件进行调试。
check50因为测试失败没有检查内存错误(直到皱眉头翻过来才能检查")