CS50拼写器(PSET 5)未加载字典
我正在处理CS50拼写器问题,遇到一个问题,运行程序时返回一个错误“无法卸载字典/大型” 我看过其他人的解决方案,但我一辈子都无法确定我的计划中出了什么问题。我认为它在has功能中,但你见过它在其他人的工作程序中有功能吗 任何帮助都将不胜感激CS50拼写器(PSET 5)未加载字典,c,dictionary,cs50,C,Dictionary,Cs50,我正在处理CS50拼写器问题,遇到一个问题,运行程序时返回一个错误“无法卸载字典/大型” 我看过其他人的解决方案,但我一辈子都无法确定我的计划中出了什么问题。我认为它在has功能中,但你见过它在其他人的工作程序中有功能吗 任何帮助都将不胜感激 // Implements a dictionary's functionality #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #inclu
// Implements a dictionary's functionality
#include <stdbool.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 words in dictionary
int word_count = 0;
// Number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
unsigned int n = hash(word);
node *cursor = table[n];
while (cursor != NULL)
{
if (strcasecmp(word, cursor -> word) == 0)
{
return true;
}
cursor = cursor -> next;
}
return false;
}
// Hashes word to a number
// Function credit to delipity(staff) on CS50 reddit page
unsigned int hash(const char *word)
{
unsigned int hash_value = 0;
for (int i = 0, n = strlen(word); i < n; i++)
{
hash_value = (hash_value << 2) ^ word[i];
}
return hash_value % N; //N is size of hashtable
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open dictionary and check for memory issue
// Open dictionary file and create word array
FILE *dict = fopen(dictionary, "r");
char word[LENGTH + 1];
// Check for memory issue with dict
if(dict == NULL)
{
printf("Dictionary is null\n");
unload();
return false;
}
// Read string 1 word at a time
while (fscanf(dict, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n -> word, word);
word_count++;
// Index word using hash function
int dict_index = hash(word);
// Insert into hash table if already empty
if (table[dict_index] == NULL)
{
n -> next = NULL;
}
// Insert work as new node if not empyty
else
{
n -> next = table[dict_index];
}
table[dict_index] = n;
}
// Close dictionary file
fclose(dict);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
return 0;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
node *tmp = cursor;
while (cursor != NULL)
{
cursor = cursor -> next;
free(tmp);
tmp = cursor;
}
}
return false;
}
//实现字典的功能
#包括
#包括
#包括
#包括
#包括
#包括“dictionary.h”
//表示哈希表中的节点
类型定义结构节点
{
字符字[长度+1];
结构节点*下一步;
}
节点;
//词典中的字数
int word_count=0;
//哈希表中的桶数
常数无符号整数N=26;
//哈希表
节点*表[N];
//如果单词在字典中,则返回true;否则返回false
布尔检查(常量字符*单词)
{
无符号整数n=哈希(字);
节点*光标=表[n];
while(光标!=NULL)
{
if(strcasecmp(字、光标->字)==0)
{
返回true;
}
光标=光标->下一步;
}
返回false;
}
//将单词散列为数字
//CS50 reddit页面上Delipite(员工)的功能积分
无符号整数散列(常量字符*字)
{
无符号整数散列值=0;
for(int i=0,n=strlen(word);inext=NULL;
}
//如果不是临时性的,则将工作作为新节点插入
其他的
{
n->next=表[dict_index];
}
表[dict_index]=n;
}
//关闭字典文件
fclose(dict);
//表示成功
返回true;
}
//如果已加载,则返回字典中的字数;如果尚未加载,则返回0
无符号整数大小(void)
{
返回字数;
返回0;
}
//从内存中卸载字典,如果成功则返回true,否则返回false
bool卸载(无效)
{
对于(int i=0;i下一步;
免费(tmp);
tmp=光标;
}
}
返回false;
}
bool卸载(无效)
{
对于(int i=0;i下一步;
免费(临时);
}
}
返回true;
}
对卸载函数尝试此操作这是返回错误语句。非常感谢。要获得帮助,你需要提供更多的背景知识,说明你正在努力实现什么以及如何实现。具体来说,您需要包含或引用错误源代码。在这种情况下(正如GitHub上的一个偶然提示),问题很可能是
unload
函数总是返回false
,使用它的代码将其解释为错误。
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
while (cursor)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}