как решить ошибку в CS50 pset5 speller и написать функцию ha sh? - PullRequest
0 голосов
/ 03 мая 2020

У меня возникли проблемы с проблемой Speller в pset5. программа компилируется нормально, но check50 показывает мне эту ошибку:

:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly
expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
:( program is free of memory errors
 valgrind tests failed; rerun with --log for more information.

вот мой код:

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "dictionary.h"
#include <strings.h>
#include <stdlib.h>
#include <ctype.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 = 27;

// Hash table
node *table[N];
char buffer[LENGTH + 1] ;
int count = 0 ;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
 node *checker = malloc(sizeof(node))  ;
 int position = hash(word) ;
 checker = table[position] ; 
 while(checker!= NULL)
 {
    if( strcasecmp(checker->word, word) == 0)
    {
        return true ;
    }
    checker = checker->next ; 
 }

   return false;
 }

 // Hashes word to a number
//hash function made using sum of ascii values of characters of a word
unsigned int hash(const char *word)
{
 int index = 0 ;
 for(int i = 0 ; word[i] != '\0' ; i++)
 {
    index += tolower(word[i]) ;
 }
 return index % N ;
 }

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
 FILE *file = fopen(dictionary, "r") ;
 if(file == NULL)
 {
    fprintf(stderr, "file doesnt open \n") ; 
    return false ;
 }
 while(fscanf(file, "%s", buffer) != EOF)
 {
    node *new1 = malloc(sizeof(node)) ;
    if(new1 == NULL)
    {
        return false ;
    }
    strcpy(new1->word, buffer) ;
    int hashed = hash(buffer) ;
    if( table[hashed] == NULL)
    {
        table[hashed] = new1 ;
        new1->next = NULL ;
    }
    else
    {
        new1->next = table[hashed] ;
        table[hashed] = new1->next ;
    }
    count++ ;
}
fclose(file) ;
return true;

}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{

  return count;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
 for(int i = 0 ; i < N ; i++)
  {
   node *cursor = NULL ;
   cursor = table[i] ;
   while(cursor)
   {
     node *tmp = cursor ;
     cursor = cursor->next ;
     free(tmp) ;

   }
    table[i] = NULL ;

   }
    return true;
   }

при выполнении сравнения моего решения с решением персонала, которое я обнаружил что количество слов в словаре и тексте оказывается одинаковым, но количество слов с ошибками отличается, количество их в моем намного выше.

, пожалуйста, помогите мне решить эту проблему

...