Question: C++ programming need help! The bold words are required functions / important details. Create a class TextAnalyzer with the following public functions and use the

C++ programming need help! The bold words are required functions / important details.

Create a class TextAnalyzer with the following public functions and use the main.cpp(see code below) to test it:

Default constructor - initializes a hash table with a size of 27 using the default hash function Another constructor - takes two parameters, an unsigned int of the hash table size and a function pointer for the hash function

Yes, you also need destructors.

Insert - inserts a string into the hash table. Takes a std::string parameter and returns void. GetCount - takes a std::string parameter and returns an unsigned int, which is the number of times the word has been seen so far. PrintHashEntries - takes no parameters and returns void. Prints all of the words and their occurrence counts in no particular order. PrintHashStats - takes no parameters and returns void. Prints statistics about the hash table as follows: the number of entries in each hash table slot, the number of used slots versus total, the maximum number of entries in a single slot, the average entries per hash table slot, the number of unique words present, and finally the total number of words. Your statistics gathering should be rather fast; it might be necessary to loop through each table slot but you shouldn't need to loop through all the words in the table. In other words, it should be O(table size), not O(word count).

This is the output of my program when run on the supplied input file using the default constructor: Hash table entries per slot: 40 74 79 69 50 37 64 33 53 21 3 9 50 48 23 36 68 5 47 143 73 16 11 60 0 10 0 25 hash table slots used out of 27 total. 143 max entries in a table slot. 41.555557 avg entries in a table slot. 1122 unique words found. 2561 total words counted.

The hash function takes a parameter of an std::string and returns an unsigned int. The default hash function looks only at the first character of the string and returns zero for any non-alphabetic character and 1 through 27 for the letters a through z.

//main.cpp : Test the functions of class TextAnalyzer

#include

#include "TextAnalyzer.h"

#include

#include

#include

using namespace std;

class FileReader

{

public:

FileReader(string filename)

{

fileStream = new ifstream(filename);

if (!fileStream->good())

{

throw invalid_argument("couldn't open file");

}

};

~FileReader()

{

delete fileStream;

};

bool GetNextWord(string &word)

{

while (true)

{

// try to read the next word from the current line

char *next = strtok_s((strtokContext == nullptr) ? currentLine : nullptr,

" \t.,:;!?\"",

&strtokContext);

if (next != nullptr)

{

word.assign(next);

return true;

}

else

{

*currentLine = '\0';

strtokContext = nullptr;

}

string nextLine;

getline(*fileStream, nextLine);

if (!fileStream->good())

{

return false;

}

strcpy_s(currentLine, nextLine.c_str());

for (unsigned int i = 0; currentLine[i] != '\0'; i++)

{

currentLine[i] = tolower(currentLine[i]);

}

}

return false;

}

private:

ifstream *fileStream = nullptr;

char currentLine[256] = {};

char *strtokContext = nullptr;

};

void TestHashTable(TextAnalyzer *analyzer)

{

pair shouldFind[] = {

{ "and" , 90 },

{ "her" , 28 },

{ "mine" , 7 },

{ "beauty", 2 },

{ "modesty", 2 },

{ "paradise", 1 },

};

for (auto entry : shouldFind)

{

unsigned int resultCount = analyzer->GetCount(entry.first);

if (entry.second != resultCount)

{

printf("ERROR! ERROR! ERROR! ERROR!\t");

}

else

{

printf("SUCCESS!\t");

}

printf("%s\texpected:%d\tactual:%d ", entry.first.c_str(), entry.second, resultCount);

}

}

int main(int argc, char** argv)

{

unique_ptr fr = make_unique("HW5input.txt");

string word;

unique_ptr analyzer;

analyzer = make_unique();

//analyzer = make_unique(256,

// [](std::string word)

//{

// static const int hashNumbers[] = { 1, 3, 5, 7, 11, 13, 17, 19, 23, 31, 41, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };

// unsigned int value = 0;

// for (size_t i = 0; i < word.length(); i++)

// {

// value += word.at(i) * hashNumbers[i % (sizeof(hashNumbers) / sizeof(*hashNumbers))];

// }

// return value;

//});

while (fr->GetNextWord(word))

{

analyzer->Insert(word);

}

analyzer->PrintHashEntries();

analyzer->PrintHashStats();

TestHashTable(analyzer.get());

printf("press enter to continue ");

getc(stdin);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!