【问题标题】:My program of speller.c is not compiling pset5我的 speller.c 程序没有编译 pset5
【发布时间】:2022-01-25 09:27:00
【问题描述】:

当我运行我的程序时,我得到了这 2 个错误,但不知道为什么

错误

dictionary.c:87:9: error: use of undeclared identifier 'words_counter'
        words_counter++;
        ^
dictionary.c:97:12: error: use of undeclared identifier 'words_counter'
    return words_counter;
           ^
2 errors generated.
make: *** [Makefile:3: speller] Error 1

代码

// Implements a dictionary's functionality

#include <stdbool.h>

#include "dictionary.h"
#include <stdlib.h>
#include <strings.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.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 = 1500;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int index = hash(word);
    node *cursor = table[index];
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor -> word) ==0 )
        {
            return true;
        }
        //Move cursor to next node
        cursor = cursor -> next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    // Ref of hash function: https://cs50.stackexchange.com/questions/38753/cs50-good-hash-function-for-pset5-speller/38755
    int hash = 600;
    while (*word != '\0')
    {
        hash = ((hash << 4 ) + (int) (*word)) % N;
        word++;
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    // Read strings from file
    // define word
    char word [LENGTH + 1];
    int index;
    while(fscanf(file, "%s", word) != EOF)
    {
        // Creating new node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copies word to the node
        strcpy (n->word, word);
        // Obtaining index to hash word
        index = hash(word);
        n -> next = table[index];
        table[index] = n;
        words_counter++;
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    //Free nodes in each linked list
    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 true;
}

我定义了 words_counter 并编译了它,但似乎我没有通过 check50 2 个错误: 代码

// Implements a dictionary's functionality

#include <stdbool.h>

#include "dictionary.h"
#include <stdlib.h>
#include <strings.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.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 = 1500;
unsigned words_counter;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int index = hash(word);
    node *cursor = table[index];
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor -> word) == 0 )
        {
            return true;
        }
        //Move cursor to next node
        cursor = cursor -> next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    // Ref of hash function: https://cs50.stackexchange.com/questions/38753/cs50-good-hash-function-for-pset5-speller/38755
    int hash = 600;
    while (*word != '\0')
    {
        hash = ((hash << 4 ) + (int) (*word)) % N;
        word++;
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    // Read strings from file
    // define word
    char word [LENGTH + 1];
    int index;
    while(fscanf(file, "%s", word) != EOF)
    {
        // Creating new node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copies word to the node
        strcpy (n->word, word);
        // Obtaining index to hash word
        index = hash(word);
        n -> next = table[index];
        table[index] = n;
        words_counter++;
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    //Free nodes in each linked list
    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 true;
}

错误:

:) 字典.c 存在

:) 拼写编译

:(正确处理最基本的单词

预期为“MISSPELLED WOR...”,而不是“MISSPELLED WOR...”

:) 处理最小长度(1 个字符)的单词

:) 处理最大长度(45 个字符)的单词

:) 正确处理带撇号的单词

:(拼写检查不区分大小写

预期为“MISSPELLED WOR...”,而不是“MISSPELLED WOR...”

:) 正确处理子字符串

:) 程序没有内存错误

我不知道如何处理这 2 个

【问题讨论】:

  • erm.... 因为words_counter 实际上是未定义的?
  • 这个错误似乎很容易解释。 C 中的所有变量都必须在使用前定义。你能指出你认为words_counter 变量是在哪里定义的吗?
  • 什么是words_counter?看起来它应该是一个unsigned int,但你没有在代码中的任何地方定义它,所以我们不知道,编译器也不知道。

标签: c dictionary compiler-errors cs50


【解决方案1】:

您的words_counter 在您的代码中用作全局变量,因此请在与其他全局对象相同的位置定义它:

const unsigned int N = 1500;
unsigned words_counter;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 2014-06-19
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2011-12-13
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多