CS50“ Speller”:词典文件有问题

时间:2020-08-19 16:54:53

标签: c cs50

我正在处理CS59第5周的“ Speller”任务。目前,我正在尝试在桌面上使用测试词典文件编写卸载功能。但是由于某些原因,第91行(while(!feof(dictionary)))返回错误:

“要求成员'_flag'不在结构或联合中”

我正在使用新计算机。我以前的计算机没有返回此错误。有人可以解释一下这是怎么回事,我该如何解决?预先感谢。

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

unsigned int HASH_MAX = 50;
unsigned int LENGTH = 20;

unsigned int hash (const char *word);
bool load(char *dictionary);
bool check (char *word);
bool unload (void);
void print (void);

typedef struct _node
{
    char *word[20];
    struct _node *next;
} node;

node *HASH_TABLE[50];

int main(int argc, char *argv[])
{
    FILE *dictionary = fopen("C:/Users/aaron/Desktop/Dictionary.txt", "r");

    if(!dictionary)
    {
        printf("FILE NOT FOUND\n");
        return 1;
    }

    if (load(dictionary))
    {
        // print "LIST (number): {(name, address), ...}\n
        print();
    }

    int lois = hash("Lois");
    printf("\n%s\n", HASH_TABLE[lois]->word);

    if (check("StEwIe"))
    {
        printf("STEWIE found\n");
    }
    else if (!check("StEwIe"))
    {
        printf("STEWIE not found\n");
    }

    if (check("Tron"))
    {
            printf("TRON found\n");
    }
    else if (!check("Tron"))
    {
            printf("TRON not found\n");
    }

    if (unload())
    {
        print();
    }
}

unsigned int hash(const char *word) // hash code function (FIND A BETTER ALGORITHM)
{
    char word_conv[LENGTH + 1]; // store converted word for uniform key
    unsigned int code = 0; // hash code

    for (int i = 0; i < strlen(word); i++) // set all letters in the word to lower case
    {
        word_conv[i] = tolower(word[i]);
    }

    for (int j = 0; j < strlen(word_conv); j++) // for all letters in converted word, add ascii value to code and multiply by 3
    {
        code += word_conv[j];
        code *= 3;
    }

    code = code % HASH_MAX; // set code to remainder of current code divided by maximum hash table size

    return code;
}

bool load (char *dictionary)
{
    char word[LENGTH+1];

    while(!feof(dictionary))
    {
        fscanf(dictionary, "%s", word);

        node *new_n = malloc(sizeof(node));
        strcpy(new_n->word, word);
        new_n->next = NULL;

        unsigned int code = hash(word);

        if (HASH_TABLE[code] == NULL)
        {
            HASH_TABLE[code] = new_n;
        }
        else if (HASH_TABLE[code] != NULL)
        {
            node *trav = HASH_TABLE[code];

            while (trav->next != NULL)
            {
                trav = trav->next;
            }

            if (trav->next == NULL)
            {
                trav->next = new_n;
            }
        }
    }

    return true;
}

bool check (char *word)
{
    unsigned int code = hash(word);

    node *check = malloc(sizeof(node));
    check = HASH_TABLE[code];

    while (check != NULL)
    {
        if (strcasecmp(check->word, word) == 0)
            return true;
    }

    if (check == NULL)
        return false;
}

bool unload (void)
{
    for (int i = 0; i < HASH_MAX; i++)
    {
        while (strcmp(HASH_TABLE[i]->word, "FREE" != 0)
        {
            node *curr = HASH_TABLE[i];
            node *prev = NULL;

            while (curr != NULL)
            {
                prev = curr;
                curr = curr->next;
            }

            strcpy(prev->word, "FREE");
        }
    }

    return true; // freed successfully
}

void print (void)
{
    for (int i = 0; i < HASH_MAX; i++)
    {
        node *check = HASH_TABLE[i];

        printf("LIST %02d: {", i);

        while (check != NULL)
        {
            printf("%s, ", check->word);
            check = check->next;
        }

        printf("}\n");
    }
}

0 个答案:

没有答案