Question: (C++) Implementinhg the bold words required functions / important details to Create a class TextAnalyzer with the following public functions and use the main.cpp(see code
(C++)Implementinhg the bold words required functions / important details to 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
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.
//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
{ "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
string word;
unique_ptr
analyzer = make_unique
//analyzer = make_unique
// [](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);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
