Возникли проблемы при попытке проверить мое решение на pset5 CS50 - PullRequest
0 голосов
/ 27 марта 2020

У меня возникли проблемы с поиском решения для ошибки. В основном мой код создает таблицу ha sh, используя связанные списки и массивы (для создания словаря), а затем проверяет правописание .txt-файлов, чтобы убедиться, что слово находится в словаре. Когда я пытаюсь проверить это с помощью CS50 check50, я получаю следующий вывод:

:) 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 

Итак, как вы можете видеть, у меня проблема с большинством базовых c слов. Я не смог найти решение или исправление для своего кода.

Ниже приведен мой код:

#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;
}

1 Ответ

1 голос
/ 27 марта 2020

Запустите ./speller dictionaries/small texts/cat.txt, и вы получите ошибку сегмента. Это потому, что check пытается получить доступ к trav->next, когда table[k] НЕДЕЙСТВИТЕЛЕН здесь while (trav->next != NULL). Я позволю вам выработать (очень простое) исправление.

...