首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在pset5 CS50上检查我的解决方案时遇到问题

尝试在pset5 CS50上检查我的解决方案时遇到问题
EN

Stack Overflow用户
提问于 2020-03-27 17:42:39
回答 1查看 1.2K关注 0票数 0

我在想出一个错误的解决方案时遇到了一些麻烦。基本上,我的代码使用链表和数组构建一个哈希表(目的是构建字典),然后对.txt文件进行拼写检查,以确保单词在字典中。当我尝试用CS50 check50检查它时,我得到了以下输出:

代码语言:javascript
复制
:) speller compiles
:( handles most basic words properly
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
:) program is free of memory errors 

所以你可以看到,我对最基本的单词有一个问题。我找不到我的代码的解决方案或修复。

下面列出了我的代码:

代码语言:javascript
复制
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.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 = 27;

//Number of words on dictionary
int COUNTER = -1;

//Check if dictionary is loaded
bool LOAD = false;

// Hash table
node *table[N] = {NULL};

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    unsigned int k = hash(word);

    node *trav = table[k];

    while (trav->next != NULL)
    {
        if ((strcasecmp(word, trav->word)) == 0)
        {
            return true;
        }
        else
        {
            trav = trav->next;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // assign a number to the first char of buffer from 0-25
    if ((isalpha(word[0]) > 0))
    {
        return tolower(word[0]) - 'a';
    }
    else
    {
        return 26;
    }
}


// Loads dictionary into memory, returning true if successful else false
char c;
bool load(const char *dictionary)
{

    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        LOAD = false;
        return false;
    }

    char wordy[LENGTH + 1];

    do
    {
        c = fscanf(file, "%s", wordy);
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            LOAD = false;
            return false;
        }
        strcpy(n->word, wordy);
        n->next = NULL;
        unsigned int i = hash(wordy);
        if (table[i] == NULL)
        {
            table[i] = n;
        }
        else
        {
            node *p = table[i];

            while (true)
            {
                if (p->next == NULL)
                {
                    p->next = n;
                    break;
                }
                p = p->next;
            }
        }

        COUNTER++;

    }
    while (c != EOF);

    fclose(file);
    LOAD = true;
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if (LOAD)
    {
        return COUNTER;
    }
    else
    {
        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];

        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
    }
    LOAD = false;
    return true;
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-27 21:52:08

运行./speller dictionaries/small texts/cat.txt,你会得到一个seg错误。这是因为当table[k]while (trav->next != NULL)中为NULL时,check会尝试访问trav->next。我会让你算出(非常简单的)修正。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60883447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档