CS50 PSET 5拼写器工作不正常(以及valgrind问题)
我是CS新手,在拼写器pset上确实需要一些帮助。我已经有了一个基本的大纲,似乎并没有错,但我仍然有问题。检查50失败,原因是:CS50 PSET 5拼写器工作不正常(以及valgrind问题),c,cs50,C,Cs50,我是CS新手,在拼写器pset上确实需要一些帮助。我已经有了一个基本的大纲,似乎并没有错,但我仍然有问题。检查50失败,原因是: 它不能正确处理最基本的单词 拼写检查不区分大小写 它不能正确处理子字符串 并且有内存错误 如果我在其中运行一个测试文件,计数器只显示字典中有2个单词,因此它几乎会将文档中的每个单词都显示为拼写错误(这让我觉得加载时有错误,但我不知道在哪里) 还有一个valgrind错误。它显示以下内容: 堆摘要:在出口处使用:14个块中有1300字节。 堆使用总量:15个allo
- 它不能正确处理最基本的单词
- 拼写检查不区分大小写
- 它不能正确处理子字符串
- 并且有内存错误
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.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 = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int numloc = hash(word);
node *cursor = table[numloc];
while (cursor != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned long hash = 5381;
int c;
while ((c = *word++))
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
int counter = 0;
bool load(const char *dictionary)
{
FILE *dictionary = fopen(dictionary, "r");
if (dictionary == NULL)
{
return false;
}
char tempword[LENGTH + 1];
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
while (fscanf(dict, "%s", tempword) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n->word, tempword);
int A = hash(tempword);
if (table[A] == NULL)
{
table[A] = n;
n->next = NULL;
}
else
{
n->next = table[A];
table[A] = n;
}
counter++;
}
fclose(dictionary);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (counter == 0)
{
unload();
return 1;
}
else
{
return counter;
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
node *tmp = table[i];
while (cursor != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
return true;
}
return false;
}
#包括
#包括
#包括
#包括
#包括
#包括“dictionary.h”
//表示哈希表中的节点
类型定义结构节点
{
字符字[长度+1];
结构节点*下一步;
}
节点;
//哈希表中的桶数
常数无符号整数N=26;
//哈希表
节点*表[N];
//如果单词在字典中,则返回true;否则返回false
布尔检查(常量字符*单词)
{
int numloc=散列(字);
节点*光标=表[numloc];
while(光标!=NULL)
{
if(strcasecmp(光标->字,字)==0)
{
返回true;
}
光标=光标->下一步;
}
返回false;
}
//将单词散列为数字
无符号整数散列(常量字符*字)
{
无符号长散列=5381;
INTC;
而((c=*word++)
{
哈希=((哈希字,临时字);
int A=散列(临时字);
如果(表[A]==NULL)
{
表[A]=n;
n->next=NULL;
}
其他的
{
n->next=表[A];
表[A]=n;
}
计数器++;
}
fclose(字典);
返回true;
}
//如果已加载,则返回字典中的字数;如果尚未加载,则返回0
无符号整数大小(void)
{
如果(计数器==0)
{
卸载();
返回1;
}
其他的
{
返回计数器;
}
}
//从内存中卸载字典,如果成功则返回true,否则返回false
bool卸载(无效)
{
对于(int i=0;i下一步;
免费(tmp);
tmp=光标;
}
返回true;
}
返回false;
}
字典.h的内容是什么??实际上,发布的代码无法编译
下面是对编译器的一次运行的结果,其中混合了一些关于如何解决问题的建议
compile语句:
gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o"
来自编译器的输出消息:
untitled1.c:20:7: error: variably modified ‘table’ at file scope
20 | node *table[N];
| ^~~~~
this is not C++.. Therefore suggest replacing:
const unsigned int N = 26;
与
因此表[]将被正确声明
untitled1.c: In function ‘check’:
untitled1.c:25:18: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
25 | int numloc = hash(word);
| ^~~~
此时,编译器不知道函数的原型:hash()
。建议将hash()
函数移到包含此语句的函数之前
untitled1.c: At top level:
untitled1.c:39:14: error: conflicting types for ‘hash’
39 | unsigned int hash(const char *word)
| ^~~~
untitled1.c:25:18: note: previous implicit declaration of ‘hash’ was here
25 | int numloc = hash(word);
| ^~~~
untitled1.c: In function ‘hash’:
untitled1.c:45:37: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
45 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
| ^
字典
已声明为字符串。因此请在此处使用其他变量名
untitled1.c:52:23: note: previous definition of ‘dictionary’ was here
52 | bool load(const char *dictionary)
| ~~~~~~~~~~~~^~~~~~~~~~
untitled1.c:54:30: warning: passing argument 1 of ‘fopen’ from incompatible pointer type [-Wincompatible-pointer-types]
54 | FILE *dictionary = fopen(dictionary, "r");
| ^~~~~~~~~~
| |
| FILE * {aka struct _IO_FILE *}
In file included from untitled1.c:4:
/usr/include/stdio.h:246:14: note: expected ‘const char * restrict’ but argument is of type ‘FILE *’ {aka ‘struct _IO_FILE *’}
246 | extern FILE *fopen (const char *__restrict __filename,
| ^~~~~
untitled1.c:60:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
60 | for (int i = 0; i < N; i++)
|
^
dict
必须是FILE*
类型,但从未通过调用fopen()
untitled1.c:64:19:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次
untitled1.c:72:17:警告:从“unsigned int”转换为“int”可能会更改结果的符号[-Wsign conversion]
72 | int A=散列(tempword);
| ^~~~
untitled1.c:在函数“size”中:
untitled1.c:100:16:警告:从“int”转换为“unsigned int”可能会更改结果的符号[-Wsign conversion]
100 |返回计数器;
| ^~~~~~~
untitled1.c:在函数“unload”中:
untitled1.c:107:23:警告:比较不同符号的整数表达式:“int”和“unsigned int”[-Wsign compare]
107 | for(int i=0;i
字典.h的内容是什么??实际上,发布的代码无法编译
下面是对编译器的一次运行的结果,其中混合了一些关于如何解决问题的建议
compile语句:
gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o"
来自编译器的输出消息:
untitled1.c:20:7: error: variably modified ‘table’ at file scope
20 | node *table[N];
| ^~~~~
this is not C++.. Therefore suggest replacing:
const unsigned int N = 26;
与
因此表[]将被正确声明
untitled1.c: In function ‘check’:
untitled1.c:25:18: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
25 | int numloc = hash(word);
| ^~~~
此时,编译器不知道函数的原型:hash()
。建议将hash()
函数移到包含此语句的函数之前
untitled1.c: At top level:
untitled1.c:39:14: error: conflicting types for ‘hash’
39 | unsigned int hash(const char *word)
| ^~~~
untitled1.c:25:18: note: previous implicit declaration of ‘hash’ was here
25 | int numloc = hash(word);
| ^~~~
untitled1.c: In function ‘hash’:
untitled1.c:45:37: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
45 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
| ^
字典
已声明为字符串。因此请在此处使用其他变量名
untitled1.c:52:23: note: previous definition of ‘dictionary’ was here
52 | bool load(const char *dictionary)
| ~~~~~~~~~~~~^~~~~~~~~~
untitled1.c:54:30: warning: passing argument 1 of ‘fopen’ from incompatible pointer type [-Wincompatible-pointer-types]
54 | FILE *dictionary = fopen(dictionary, "r");
| ^~~~~~~~~~
| |
| FILE * {aka struct _IO_FILE *}
In file included from untitled1.c:4:
/usr/include/stdio.h:246:14: note: expected ‘const char * restrict’ but argument is of type ‘FILE *’ {aka ‘struct _IO_FILE *’}
246 | extern FILE *fopen (const char *__restrict __filename,
| ^~~~~
untitled1.c:60:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
60 | for (int i = 0; i < N; i++)
|
^
dict
必须是FILE*
类型,但从未通过调用fopen()
untitled1.c:64:19:注意:每个未声明的标识符对于它出现在其中的每个函数只报告一次
untitled1.c:72:17:警告:从“unsigned int”转换为“int”可能会更改结果的符号[-Wsign conversion]
72 | int A=散列(tempword);
| ^~~~
untitled1.c:在函数“size”中:
untitled1.c:100:16:警告:从“int”转换为“unsigned int”可能会更改结果的符号[-Wsign conversion]
100 |返回计数器;
| ^~~~~~~
untitled1.c:在函数“unload”中:
untitled1.c:107:23:警告:比较不同符号的整数表达式:“int”和“unsigned int”[-Wsign compare]
107 | for(int i=0;i
内存泄漏的原因是因为t