該程式對與給定字典(CS50:Pset5 拼寫器)配對的文本運行拼寫檢查。字典檔案為txt檔案形式,以哈希表的形式加載到記憶體中。
check 函式采用從文本檔案中讀取的單詞作為引數。該詞被散列并與給定的單個散串列中存在的任何元素進行比較。如果一個詞存在,它回傳真,否則回傳假。
Hash 是對任何給定的單詞進行哈希處理,而 Load 函式是將字典中的單詞加載到哈希表中單詞的相應哈希索引中。Load Function 將指向字典的指標作為引數。
大小函式通過指向現有的哈希表來測量字典的大小。
Unload 函式重復哈希表的每個可能索引,同時呼叫 ClearNodes,它檢查連接到哈希表的鏈表中的內容。
我嘗試將文本和字典的大小調整為較小的大小,并且我還嘗試在呼叫單個函式之前和之后放置斷點(因為以某種方式將斷點放置在main() loads the dictionaryand之后Finished checking all the words in a given text,然后在除錯器中手動按下step-over直到main()完成,設法使程式正常終止)。對于較小尺寸的字典和文本,我沒有發現任何導致函式不回傳值的相似之處。請記住,卸載函式假定此時始終回傳 true,并且尚未完成。
最小可重復示例
非常感謝幫助。
// Implements a dictionary's functionality
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <strings.h>
#include <string.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 = 17576;
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//Create Word Hash Value
int WordHashIndex = hash(word);
node *Temp = table[WordHashIndex];
//Jump to Hash Index
while(Temp != NULL)
{
if(strcasecmp(Temp -> word, word) != 0)
{
Temp = Temp -> next;
}
else
{
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
if(word[1] == '\0')
{
return (toupper(word[0]) - 65);
}
else if(word[2] == '\0')
{
return ((toupper(word[0]) - 65) ((((toupper(word[1]) - 65) * 26) > -1) ? ((toupper(word[1]) - 65) * 26) : 0));
}
else
{
return ((toupper(word[0]) - 65) ((((toupper(word[1]) - 65) * 26) > -1) ? ((toupper(word[1]) - 65) * 26) : 0) ((((toupper(word[2]) - 65) * 676) > -1) ? ((toupper(word[2]) - 65) * 676) : 0));
}
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO
int HashedIndex = 0;
char TempWord[LENGTH 1];
int CharLengthCounter = 0;
FILE *LoadedDictionary = fopen(dictionary, "r");
if(LoadedDictionary == NULL)
{
return false;
}
while(fscanf(LoadedDictionary, "%s", TempWord) != EOF)
{
HashedIndex = hash(TempWord);
node *TempNode = NULL;
//Jump To HashedIndex
if(table[HashedIndex] != NULL)
{
TempNode = malloc(sizeof(node));
TempNode -> next = table[HashedIndex] -> next;
table[HashedIndex] -> next = TempNode;
strcpy(TempNode -> word, TempWord);
}
else
{
TempNode = malloc(sizeof(node));
table[HashedIndex] = TempNode;
strcpy(table[HashedIndex] -> word, TempWord);
}
}
if(LoadedDictionary != NULL)
{
free(LoadedDictionary);
return true;
}
else
{
return false;
}
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO
node *Temp;
int DictSize = 0;
for(int Counter = 0; Counter < N; Counter )
{
Temp = table[Counter];
if(Temp != NULL)
{
DictSize ;
while(Temp -> next != NULL)
{
Temp = Temp -> next;
DictSize ;
}
}
else
{
continue;
}
}
if(DictSize != 0)
{
return DictSize;
}
else
{
return 0;
}
}
//Check Node Data and Length Inside Each Table
bool ClearNodes(node *CurrentIndex)
{
if(CurrentIndex != NULL)
{
if(CurrentIndex -> next != NULL)
{
if(ClearNodes(CurrentIndex -> next))
{
free(CurrentIndex);
return true;
}
}
else
{
free(CurrentIndex);
return true;
}
}
return false;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO
int TableElementsCheck = 0;
for(int Counter = 0; Counter < N; Counter )
{
ClearNodes(table[Counter]);
}
return true;
}
uj5u.com熱心網友回復:
您的問題在函式中至少有以下錯誤load:
以下塊是錯誤的:
else
{
TempNode = malloc(sizeof(node));
table[HashedIndex] = TempNode;
strcpy(table[HashedIndex] -> word, TempWord);
}
您忘記設定TempNode->next為NULL.
此外,該行
free(LoadedDictionary);
是錯的。您應該呼叫fclose而不是在函式回傳的freea 上。FILE *fopen
修復這些錯誤后,輸出顯示正確:
MISSPELLED WORDS
A
is
not
a
此輸出是正確的,因為這些詞在您提供的詞典中不存在。單詞cat和caterpillar并沒有列印錯誤,因為它們在字典中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/404573.html
標籤:
上一篇:我怎樣才能把這本字典轉換成資料框
下一篇:在Typescript中,如何將Array<T>轉換為Map<K,V>并推斷K和V如果T是元組[K,V]同時具有編譯時保護(如果不是)
