我的 speller.c 程序没有编译 pset5
My program of speller.c is not compiling pset5
当我 运行 我的程序出现这 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 != '[=12=]')
{
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 != '[=13=]')
{
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;
}
错误:
:) dictionary.c 存在
:) 拼写编译
:(正确处理最基本的单词
应为“错字...”,而不是“错字...”
:) 处理最小长度(1 个字符)的单词
:) 处理最大长度(45 个字符)的单词
:) 正确处理带撇号的单词
:(拼写检查不区分大小写
应为“错字...”,而不是“错字...”
:) 正确处理子字符串
:) 程序没有内存错误
我不确定如何处理这两个
您的 words_counter
在您的代码中用作全局变量,因此将其定义在与其他全局对象相同的位置:
const unsigned int N = 1500;
unsigned words_counter;
当我 运行 我的程序出现这 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 != '[=12=]')
{
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 != '[=13=]')
{
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;
}
错误:
:) dictionary.c 存在
:) 拼写编译
:(正确处理最基本的单词
应为“错字...”,而不是“错字...”
:) 处理最小长度(1 个字符)的单词
:) 处理最大长度(45 个字符)的单词
:) 正确处理带撇号的单词
:(拼写检查不区分大小写
应为“错字...”,而不是“错字...”
:) 正确处理子字符串
:) 程序没有内存错误
我不确定如何处理这两个
您的 words_counter
在您的代码中用作全局变量,因此将其定义在与其他全局对象相同的位置:
const unsigned int N = 1500;
unsigned words_counter;