CS50 pset5拼写怪异的bug

时间:2020-09-11 08:38:44

标签: c cs50

当我第一次运行完整程序时,我已经完成了cs50 pset5拼写程序的编写(或者,我认为)。它开始打印出一些单词,所以我就像:“宾果!我完成了!”我非常高兴,因为我已经为此工作了数周。但是后来,当它遍历单词时,我注意到当它们在词典中时,会打印出一些单词,例如单词“ and”。所以我认为我的代码中有一个小错误。但是当到达列表末尾时,它并没有执行“单词拼写错误:”部分以及其他部分。它甚至没有以$~开头的新行。我不得不关闭整个终端,因为那样就无法在那做任何事情:

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;
    
}

非常感谢, 丢失代码。

1 个答案:

答案 0 :(得分:1)

  • hash不区分大小写。文本中任何带有大写字母的单词都将进入索引1。
  • unloadwhile(table[i] != NULL)处有一个无限循环,因为table[i]永不改变

解决这两个问题应该会取得进展,尽管可能不会完全成功。

NB ctrl-c 应该结束陷入循环而无需关闭终端的程序。