Question: Please help with code regarding hash tables. Here are the source code files. I need to write all of the functions specified in wordCount.cpp and

Please help with code regarding hash tables.

Here are the source code files. I need to write all of the functions specified in wordCount.cpp and create test cases and place them in tddFuncs.cpp. Here is the base code I have.

// WordCount.cpp #include "WordCount.h" using namespace std; // Default constructor WordCount::WordCount() {} // Simple hash function. Do not modify. size_t WordCount::hash(std::string word) const { size_t accumulator = 0; for (size_t i = 0; i < word.size(); i++) { accumulator += word.at(i); } return accumulator % CAPACITY; } int WordCount::getTotalWords() const { // STUB. return -1; } int WordCount::getNumUniqueWords() const { // STUB return -1; } int WordCount::getWordCount(std::string word) const { // STUB return -1; } int WordCount::incrWordCount(std::string word) { // STUB return -1; } bool WordCount::isWordChar(char c) { // STUB return false; } std::string WordCount::stripWord(std::string word) { // STUB return ""; }
// WordCount.h #ifndef WORDCOUNT_H #define WORDCOUNT_H #include  #include  #include  #include  class WordCount { public: WordCount(); int getTotalWords() const; // Return total number of word occurences (for all words) in the // hash table. int getNumUniqueWords() const; // Return total number of unique words in the hash table. int getWordCount(std::string word) const; // Return the number of occurences for a specific word. // You may not assume the parameter is a valid word and must // strip / convert to upper case before looking in the hash table. // If the word does not exist in the table, return 0 int incrWordCount(std::string word); // Update the hash table by incrementing the number of occurences for // the word. You may not assume the parameter word is a valid word. // Words must be hashed and stored containing all upper-case // characters. If the word not in the table, then add it. If the word // was added, this function returns the current number of occurences of // the word after it was added. static bool isWordChar(char c); // May be useful as a helper function in your code. // Returns true if c is a valid word character defined as either // a lower-case or upper-case alpha char. static std::string stripWord(std::string word); // Words cannot contain any digits or special characters EXCEPT for // hyphens (-) and apostrophes (') that occur in the middle of a // valid word (the first and last characters of a word must be an alpha // character). // For example, "can't" and "good-hearted" are considered valid words. // "12mOnkEYs-$" will be stripped to "mOnkEYs" private: const static size_t CAPACITY = 100; // capacity for the hash table array std::vector > table[CAPACITY]; // hash table array of vectors. Each index in the array will contain // a vector. Each element in the vector contains a  // pair where the string value represents a unique word and the size_t // value represents the number of occurences for that word. size_t hash(std::string word) const; // Hash function that will return an index for the hash table. }; #endif // WORDCOUNT_H

//tddFuncs.cpp

#include "tddFuncs.h" #include  #include  using std::cout; using std::endl; std::string convertLineNumber(int lineNumber) { std::string retVal=""; if (lineNumber > 0) retVal += " line: " + std::to_string(lineNumber); return retVal; } void assertEquals(const char * const expected, const char * const actual, std::string message, int lineNumber) { std::string line = convertLineNumber(lineNumber); if ( !strcmp(expected,actual) ) { // if they are equal cout << "PASSED: " << message << line << endl;; } else { cout << " FAILED: " << message << line << endl << " Expected: " << expected << " Actual: " << actual << endl; } } void assertEquals(const char * const expected, std::string actual, std::string message, int lineNumber) { assertEquals(std::string(expected),actual,message,lineNumber); } void assertTrue(bool expression, std::string message, int lineNumber) { std::string line = convertLineNumber(lineNumber); if (expression) { cout << "PASSED: " << message << line << endl;; } else { cout << " FAILED: " << message << line << endl << " Expected true, but was false " << endl; } }

//tddFuncs.h

#ifndef TDDFUNCS_H #define TDDFUNCS_H #include  #include  std::string convertLineNumber(int lineNumber); // template requires == and << template void assertEquals(T expected, T actual, std::string message, int lineNumber) { std::string line=convertLineNumber(lineNumber); if (expected==actual) { std::cout << "PASSED: " << message << line << std::endl;; } else { std::cout << " FAILED: " << message << line << std::endl << " Expected: " << expected << " Actual: " << actual << std::endl; } } // specialized because char * doesn't work properly on == void assertEquals(const char * const expected, const char * const actual, std::string message, int lineNumber=-1); // specialized for the same reason, and because expected is often a string literal void assertEquals(const char * const expected, std::string actual, std::string message, int lineNumber=-1); #define ASSERT_EQUALS(expected,actual) \ assertEquals(expected,actual,#actual " at " __FILE__ , __LINE__ ) void assertTrue(bool expression, std::string message="", int lineNumber=-1); #define ASSERT_TRUE(expression) \ assertTrue(expression,#expression " at " __FILE__, __LINE__) #endif // TDDFUNCS_H

Please help me !! I am most concerned with how to write the test cases though.

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!