load()函数的问题(CS50问题集5:拼写器)

发布于 2025-01-27 03:14:45 字数 2974 浏览 4 评论 0原文

我正在研究CS50的问题集5(拼写),但我不明白LOAD()函数在哪里出错。当我尝试编译时,我会在第36和79行中遇到错误。但是我不明白如何修复它。我是CS的新手,所以很抱歉这是明显的。 我认为我的问题与strcpy和strcmp函数有关,但据我了解,我正在使用CS50手动页面中指示的功能。但是,显然我缺少一些东西,否则它将有效。 先感谢您!!

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char *word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
unsigned long word_count;
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // Find hash location for current word
    int h = hash(word);
    // Create cursor to keep track of location on linked list at hash location
    node *cursor = table[h] -> next;
    while (cursor != NULL)
    {
        // If word is found
        if (strcmp(cursor -> word, word) == 0)
        {
            return true;
        }
        // If word is not at current cursor location
        else
        {
            cursor = cursor -> next;
        }
    }
    // If word is not found in linked list at hash location
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
   return toupper(word[0]) - 'A';
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // Initialise number of words loaded from dictionary
    word_count = 0;
    // Open dictionary file from command line
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        return false;
    }
    // Read words from dictionary one by one
    char buffer[LENGTH + 1];
    while (fscanf(dict, "%s", buffer) != EOF)
    {
        // Create new node for current word
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copy current word to created node
        strcpy(n -> word, buffer);

        // Assign node to the hash table
        int h = hash(buffer);
        // Node does not already exist at hash location
        if (table[h] -> next == NULL)
        {
            n -> next = NULL;
            table[h] -> next = n;
        }
        // Node already exists at hash location
        else
        {
            n -> next = table[h] -> next;
            table[h] -> next = n;
        }
        // Update counter for words loaded from dictionary
        word_count++;
    }
    fclose(dict);
    // Dictionary successfully loaded
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    return false;
}

I'm working on CS50's Problem Set 5 (Speller) and I don't understand where I am going wrong with the load() function. I am getting errors in lines 36 and 79 when I try to compile. But I don't understand how to fix it. I am new to CS so I am sorry if this is something obvious.
I think my issue has something to do with the strcpy and strcmp functions, but as far as I understand I am using the functions as instructed in the CS50 manual pages. However, clearly I am missing something otherwise it would be working.
Thank you in advance!!

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char *word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
unsigned long word_count;
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // Find hash location for current word
    int h = hash(word);
    // Create cursor to keep track of location on linked list at hash location
    node *cursor = table[h] -> next;
    while (cursor != NULL)
    {
        // If word is found
        if (strcmp(cursor -> word, word) == 0)
        {
            return true;
        }
        // If word is not at current cursor location
        else
        {
            cursor = cursor -> next;
        }
    }
    // If word is not found in linked list at hash location
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
   return toupper(word[0]) - 'A';
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // Initialise number of words loaded from dictionary
    word_count = 0;
    // Open dictionary file from command line
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        return false;
    }
    // Read words from dictionary one by one
    char buffer[LENGTH + 1];
    while (fscanf(dict, "%s", buffer) != EOF)
    {
        // Create new node for current word
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copy current word to created node
        strcpy(n -> word, buffer);

        // Assign node to the hash table
        int h = hash(buffer);
        // Node does not already exist at hash location
        if (table[h] -> next == NULL)
        {
            n -> next = NULL;
            table[h] -> next = n;
        }
        // Node already exists at hash location
        else
        {
            n -> next = table[h] -> next;
            table[h] -> next = n;
        }
        // Update counter for words loaded from dictionary
        word_count++;
    }
    fclose(dict);
    // Dictionary successfully loaded
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-02-03 03:14:45

节点定义与发行版代码不同。 Word应该是一个char阵列,而不是字符指针数组。 node-&gt; word是在字符串函数中使用的错误类型。

The node definition is not the same as the distro code; word should be a char array not an array of char pointers. node->word is the wrong type for use in the string functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文