我在CS50课程的第5题:拼写。为了解决这个问题,我们基本上需要将字典(已经由工作人员提供)加载到我们需要创建的哈希表中。然后,我们必须使用哈希表检查文本(也是给定的)中是否存在错误。(哈希表中不包含的错误->单词)。最后,程序应该释放所有分配给加载字典的内存,并显示“屏幕上拼写错误的单词(错误)”。在此之后,为了检查我们的程序中是否有错误,教师包括了他们对问题的答案(不是他们的程序,而是他们的程序在文本中发现的拼写错误的单词)。就我个人而言,我写了一个程序,给出了与工作人员相同的答案。但是,当我尝试使用check50检查它时(教员创建它是为了自动检查我们的程序是否编写得很好),我得到了以下内容:
期望输出:拼写错误的单词:字典中的0个单词:文本中的1个单词:1实际输出:无法加载min_length/dict。预期输出:拼写错误的单词拼写错误:字典中的0个单词:文本中的1个单词:1实际输出:无法加载max_length/dict。预期输出:拼写错误的单词foo的拼写错误:字典中的一个单词:文本中的一个单词:一个实际的输出:无法加载撇号/没有/dict。预期输出:拼错单词拼写错误:字典中的0个单词:文本中的1个单词:8个实际输出:无法加载大小写/字典。
哦耶!我忘了告诉您,讲师已经编写了大部分的程序:我们只需要实现以下功能:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
// TODO: Choose number of buckets in hash table
const unsigned int N = LENGTH * 'Z';
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// TODO
//Get the hashcode for the word
int hashcode = hash(word);
//Create a pointer which is going through the linked-list
node *trav = NULL;
//pointing to the first element of the linked-list
trav = table[hashcode];
//Go through the linked-list
while(trav != NULL)
{
//if found the corresponding word in the hashtable
if(strcasecmp(trav -> word, word) == 0)
{
return true;
}
else
{
//pointing to the next node
trav = trav -> next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
//creating an integer to store a value
//insert here
int value = 0;
//adding the value of all the characters in the word
int n = 0;
while (word[n] != '\0')
{
value = value + toupper(word[n]) - 'A';
n++;
}
if (value < 0)
{
value = (value) * -1;
}
return value;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
//Creating a string storing a word
char word[LENGTH + 1];
int check = 0;
//Open dictionary file
FILE *ptrDico = fopen(dictionary, "r");
if (ptrDico == NULL)
{
printf("The pointer to the dictionary result in NULL.\n");
return 1;
}
//Read strings from file one at a time
while(fscanf(ptrDico, "%s", word) != EOF)
{
//Create a new node for each word
node* newWord = NULL;
newWord = malloc(sizeof(node));
//Put the word into the node
if (newWord == NULL)
{
free(newWord);
return 1;
}
strcpy(newWord -> word, word);
//Hash word to obtain a hash value
int hashcode = hash(word);
//Insert node into hash table at that location
newWord -> next = table[hashcode];
table[hashcode] = newWord;
check++;
}
//check if loaded successfully
if (check > 1)
{
fclose(ptrDico);
return true;
}
fclose(ptrDico);
return false;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
unsigned int count = 0;
//Creating a pointer that is going to go through the linked-list
node *trav = NULL;
//Going through the array of linked-list
for (int list = 0; list < N; list++)
{
//Going through the linked-lists
trav = table[list];
while(trav != NULL)
{
trav = trav -> next;
count++;
}
}
return count;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO
//Creating a tmp pointer and a free pointer
node *tmp = NULL;
node *freeNode = NULL;
//Going through the linked-lists to free them one at a time
for (int list = 0; list < N; list++)
{
//Pointing the free pointer to the first element of the linked-list
tmp = table[list];
freeNode = table[list];
while(tmp != NULL)
{
tmp = tmp -> next;
free(freeNode);
freeNode = tmp;
}
}
return true;
}我真的不明白这里发生了什么(正如你猜的那样,我是一个真正的初学者),所以即使是最微小的贡献也会受到极大的赞赏。
发布于 2022-08-19 20:07:20
仔细和批判性地阅读check50结果。考试失败的字典里有多少个单词?这个数字是否大于1(如在if (check > 1)中)?
https://stackoverflow.com/questions/73420096
复制相似问题