CS50 pset5 拼写器奇怪的错误
CS50 pset5 speller weird bug
当我第一次 运行 我的完整程序时,我已经完成了 cs50 pset5 拼写器(或者我是这么想的)。它开始打印出一些文字,所以我说:“宾果游戏!我完成了!”我真的很高兴,因为我已经为此工作了几个星期。但是后来,当它检查单词时,我注意到一些单词,比如 'and' 是在字典里打印出来的。所以我认为我的代码中有一个小错误。但是当它到达列表的末尾时,它没有做 'Words misspelled:' 部分和其他部分。它甚至没有换行以 $~
开头。我不得不关闭整个终端,因为我无法在那里做任何其他事情,因为它是这样的:
OUT
THE
END
这些话是最后的话,然后只有一个空行。然后我打开了一个新终端,用工作人员的答案检查了我的答案,错了太多!我试图在我的代码中找到一些错误,但我找不到任何错误。如果你能找到一些并告诉我,我真的很感激。这是我的代码:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
#include <strings.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 = 19683;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
unsigned int lol = hash(word);
// TODO
int i;
node *cursor =table[lol];;
while(cursor != NULL)
{
if(strcasecmp(word, cursor -> word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int count = 0;
// TODO
for(int i = 96; i < 122; i++)
{
for(int j = 96; j < 122; j++)
{
for(int d = 96; d < 122; d++)
{
count++;
char firstletter = i;
char secondletter = j;
char thirdletter = d;
if(word[0] == firstletter&& word[1] == secondletter && word[2] == thirdletter)
{
return count;
}
}
}
}
return 1;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open dictionary and check for memory issue
// Open dictionary file and create word array
FILE *dict = fopen(dictionary, "r");
char word[LENGTH + 1];
// Check for memory issue with dict
if(dict == NULL)
{
printf("Dictionary is null\n");
unload();
return false;
}
// Read string 1 word at a time
while (fscanf(dict, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n -> word, word);
// Index word using hash function
int dict_index = hash(word);
// Insert into hash table if already empty
if (table[dict_index] == NULL)
{
n -> next = NULL;
}
// Insert work as new node if not empyty
else
{
n -> next = table[dict_index];
}
table[dict_index] = n;
}
// Close dictionary file
fclose(dict);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
int count = 0;
// TODO
for(int i = 0; i < N; i++)
{
while(table[i] != NULL)
{
node* cursor = table[i];
count++;
cursor = cursor -> next;
}
}
return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
for(int i = 0; i < N; i++)
{
node *cursor = table[i];
while(cursor)
{
node *temp = cursor;
cursor = cursor -> next;
if(temp != NULL)
{
return true;
}
free(temp);
}
}
return 1;
}
非常感谢,
迷失在代码中。
hash
不区分大小写。文本中任何带有大写字母的单词都会进入索引 1。
unload
这里有一个无限循环 while(table[i] != NULL)
因为 table[i]
永远不会改变
解决这两个问题应该会取得进展,但可能不会完全成功。
NB ctrl-c 应该 结束卡在循环中的程序而不需要关闭终端。
当我第一次 运行 我的完整程序时,我已经完成了 cs50 pset5 拼写器(或者我是这么想的)。它开始打印出一些文字,所以我说:“宾果游戏!我完成了!”我真的很高兴,因为我已经为此工作了几个星期。但是后来,当它检查单词时,我注意到一些单词,比如 'and' 是在字典里打印出来的。所以我认为我的代码中有一个小错误。但是当它到达列表的末尾时,它没有做 'Words misspelled:' 部分和其他部分。它甚至没有换行以 $~
开头。我不得不关闭整个终端,因为我无法在那里做任何其他事情,因为它是这样的:
OUT
THE
END
这些话是最后的话,然后只有一个空行。然后我打开了一个新终端,用工作人员的答案检查了我的答案,错了太多!我试图在我的代码中找到一些错误,但我找不到任何错误。如果你能找到一些并告诉我,我真的很感激。这是我的代码:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
#include <strings.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 = 19683;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
unsigned int lol = hash(word);
// TODO
int i;
node *cursor =table[lol];;
while(cursor != NULL)
{
if(strcasecmp(word, cursor -> word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int count = 0;
// TODO
for(int i = 96; i < 122; i++)
{
for(int j = 96; j < 122; j++)
{
for(int d = 96; d < 122; d++)
{
count++;
char firstletter = i;
char secondletter = j;
char thirdletter = d;
if(word[0] == firstletter&& word[1] == secondletter && word[2] == thirdletter)
{
return count;
}
}
}
}
return 1;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open dictionary and check for memory issue
// Open dictionary file and create word array
FILE *dict = fopen(dictionary, "r");
char word[LENGTH + 1];
// Check for memory issue with dict
if(dict == NULL)
{
printf("Dictionary is null\n");
unload();
return false;
}
// Read string 1 word at a time
while (fscanf(dict, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n -> word, word);
// Index word using hash function
int dict_index = hash(word);
// Insert into hash table if already empty
if (table[dict_index] == NULL)
{
n -> next = NULL;
}
// Insert work as new node if not empyty
else
{
n -> next = table[dict_index];
}
table[dict_index] = n;
}
// Close dictionary file
fclose(dict);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
int count = 0;
// TODO
for(int i = 0; i < N; i++)
{
while(table[i] != NULL)
{
node* cursor = table[i];
count++;
cursor = cursor -> next;
}
}
return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
for(int i = 0; i < N; i++)
{
node *cursor = table[i];
while(cursor)
{
node *temp = cursor;
cursor = cursor -> next;
if(temp != NULL)
{
return true;
}
free(temp);
}
}
return 1;
}
非常感谢, 迷失在代码中。
hash
不区分大小写。文本中任何带有大写字母的单词都会进入索引 1。unload
这里有一个无限循环while(table[i] != NULL)
因为table[i]
永远不会改变
解决这两个问题应该会取得进展,但可能不会完全成功。
NB ctrl-c 应该 结束卡在循环中的程序而不需要关闭终端。