Question

C++ See the provided specification files. Complete the implementation for each as a separate file. void...

C++

See the provided specification files. Complete the implementation for each as a separate file.

void seen(std::string);

If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects.

std::string getNextWord();

Returns the next word of the list and sets the currentItem pointer to the next Word. This may return an empty string, “”, if the pointer is NULL.

StackNode* findWord(std::string);

Returns a pointer to some Word in the list. Return NULL if not found.

Write a driver to do the following:

Create a Words object. This is a linked list of Word objects. Open an input file full of mixed words. Read each word. For each word in the input file, call the seen method for this word. The seen method takes a string argument. For example, the word ‘cat’ was just read in. Call myWords.seen(“cat”). After processing all of the words in the file, close the file. Last, print out the statistics of the file to the console. Include: For every word in the Words object, print the word and the number of occurrences. The total number of Words in the file.

Submit screenshots of your program, the input file you used, and your source code.

specification files

Word.h

#ifndef WORD_H_
#define WORD_H_
#include ;

class Word
{
private:
        std::string word;
        int occurrences;
public:
        Word(std::string w); //word = w and occurrences = 1
        std::string getWord() const; //return the stored word
        int getOccurrences() const;
        void increment();       //increment the occurrences value
};

#endif
Words.h

#ifndef WORDS_H_
#define WORDS_H_
#include ;
#include "Word.h";

class Words
{
        private:
                struct StackNode
                {
                        Word word;
                        StackNode *next;
                };

                StackNode *top;
                StackNode *currentItem;
                StackNode* findWord(std::string); //Returns a pointer to some Word in the list
                                                                                        // return NULL if not found
        public:
                Words() // Create an empty list of Word objects
                        {       top = NULL;
                                resetNextWord();
                        }
                void remove(std::string); //remove a word object, if it is in the list
                bool find(std::string) const; //if the word, as a string, is in the list, return true. else false
                void seen(std::string); //pass in a word, as a string, that has been seen.
                                                // If this word is not in the list, insert it with
                                                // the number of occurrences = 1.
                                                // If this word is in the list, increment the
                                                // occurrences for this word.
                int getOccurrences(std::string) const; //return the value of occurrences
                int getLength() const;          // returns the length of the list. NOT STORED
                std::string getNextWord();      // returns the next word of the list and sets
                                                                        // the currentItem pointer to the next Word
                void resetNextWord(); //resets the currentItem pointer to top.
                bool isEmpty() const;
};

#endif

Homework Answers

Answer #1

// Word.h
#ifndef WORD_H_
#define WORD_H_

#include <string>

class Word
{
private:
std::string word;
int occurrences;
public:
// default constructor used when creating object of StackNode
Word();
Word(std::string w); //word = w and occurrences = 1
std::string getWord() const; //return the stored word
int getOccurrences() const;
void increment(); //increment the occurrences value
};

#endif

//end of Word.h

// Word.cpp
#include "Word.h"

// default constructor
Word::Word() : word(""), occurrences(0)
{}

// parameterized constructor
Word::Word(std::string w) : word(w), occurrences(1)
{}

// return word
std::string Word:: getWord() const
{
return word;
}

// return occurrences
int Word:: getOccurrences() const
{
return occurrences;
}

// increment occurrences
void Word:: increment()
{
occurrences++;
}

//end of Word.cpp

// Words.h
#ifndef WORDS_H_
#define WORDS_H_

#include <iostream>
#include "Word.h"

class Words
{
private:
struct StackNode
{
Word word;
StackNode *next;
};

StackNode *top;
StackNode *currentItem;
StackNode* findWord(std::string); //Returns a pointer to some Word in the list
// return NULL if not found
public:
Words() // Create an empty list of Word objects
{
top = NULL;
resetNextWord();
}

void remove(std::string); //remove a word object, if it is in the list
bool find(std::string) const; //if the word, as a string, is in the list, return true. else false
void seen(std::string); //pass in a word, as a string, that has been seen.

// If this word is not in the list, insert it with
// the number of occurrences = 1.
// If this word is in the list, increment the
// occurrences for this word.
int getOccurrences(std::string) const; //return the value of occurrences
int getLength() const; // returns the length of the list. NOT STORED
std::string getNextWord(); // returns the next word of the list and sets
// the currentItem pointer to the next Word

void resetNextWord(); //resets the currentItem pointer to top.
bool isEmpty() const;
};

#endif

//end of Words.h

// Words.cpp
#include "Words.h"

// Returns a pointer to some Word in the list
// return NULL if not found
Words::StackNode* Words::findWord(std::string word)
{
// set curr to top node
Words::StackNode *curr = top;
// loop over the stack to search for the word
while(curr != NULL)
{
if(curr->word.getWord() == word) // word found, return curr
return curr;
curr = curr->next;
}

return NULL; // word not found
}

//remove a word object, if it is in the list
void Words:: remove(std::string word)
{
// set curr to top node
Words::StackNode *curr = top;
Words::StackNode *prev = NULL; // set prev to node before top

// loop over the stack
while(curr != NULL)
{
if(curr->word.getWord() == word) // word found
{
if(curr == top) // word is at top
{
// update top to node next to top
top = top->next;
}else
{
// set next of prev to next of curr
prev->next = curr->next;
}

// delete curr node and exit the loop
curr->next = NULL;
delete(curr);
curr = NULL;
break;
}

prev = curr;
curr = curr->next;
}
}

// if the word, as a string, is in the list, return true. else false
bool Words:: find(std::string word) const
{
Words::StackNode *curr = top; // set curr to top
// loop over the stack
while(curr != NULL)
{
if(curr->word.getWord() == word) // word found
return true;
curr = curr->next;
}

return false; // word not found
}

//pass in a word, as a string, that has been seen.
// If this word is not in the list, insert it with
// the number of occurrences = 1.
// If this word is in the list, increment the
// occurrences for this word.
void Words:: seen(std::string word)
{
// get the node of word using findWord
Words::StackNode *node = findWord(word);
if(node != NULL) // word present, increment occurrences
node->word.increment();
else
{
// word not present, insert the word at the top
Words::StackNode *curr = new Words::StackNode;
curr->word = Word(word);
curr->next = top;
top = curr;
}
}

//return the value of occurrences
int Words::getOccurrences(std::string word) const
{
// set curr to top
Words::StackNode *curr = top;
// loop over the stack
while(curr != NULL)
{
if(curr->word.getWord() == word) // word found
return curr->word.getOccurrences();
curr = curr->next;
}

return 0; // word not found
}

// returns the length of the list. NOT STORED
int Words::getLength() const
{
// set curr to top of stack
Words::StackNode *curr = top;
int count = 0; // set count to 0

// loop over stack, incrementing the count
while(curr != NULL)
{
count++;
curr = curr->next;
}

return count;
}

// returns the next word of the list and sets
// the currentItem pointer to the next Word
std::string Words:: getNextWord()
{
std::string word = "";
// if currentItem is not at the end of the list
if(currentItem != NULL)
{
word = currentItem->word.getWord(); // get the current word
currentItem = currentItem->next; // move currentItem to next node
}

return word;
}

//resets the currentItem pointer to top.
void Words:: resetNextWord()
{
currentItem = top;
}

// returns true if stack is empty else return false
bool Words:: isEmpty() const
{
return (top == NULL);
}

// end of Words.cpp

// main.cpp : C++ program to implement the class for counting the frequency of words in an input file (case-sensitively)
#include <iostream>
#include <fstream>
#include "Words.h"

using namespace std;

// function decaration
string removePunctuations(string word);

int main()
{
// open an input file
ifstream fin("inputfile.txt");
Words words;
string word;
// check if file opened successfully
if(fin.is_open())
{
// read till the end of file
while(!fin.eof())
{
fin>>word; // read a word
word = removePunctuations(word); // remove punctuations from word (excluding hyphen(-) and single quote('))
// if word is not empty string after removal of punctuation, insert/increment its occurrences it in stack
if(word.length() > 0)
words.seen(word);
}

fin.close(); // close the input file

// reset the counter to start of list
words.resetNextWord();
// display total number of words in file
cout<<"Total words in the file: "<<words.getLength()<<endl;
cout<<"Words: "<<endl;
// loop to display the words and their frequency
do
{
word = words.getNextWord();
if(word != "")
{
cout<<word<<" : "<<words.getOccurrences(word)<<endl;
}
}while(word != "");
}else
cout<<"Unable to open input file"<<endl;
return 0;
}

// function to remove punctuations from word except for hyphen and single quote and return the modified word
string removePunctuations(string word)
{
string mod_word = "";
for(size_t i=0;i<word.length();i++)
{
if((word[i] >= 'A' && word[i] <= 'Z') || (word[i] >='a' && word[i] <='z') || (word[i] >= '0' && word[i] <= '9' ) || word[i] == '\'' || word[i] == '-')
{
mod_word += word[i];
}
}
return mod_word;
}

//end of main.cpp

Output:

Input file: inputfile.txt

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h...
C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h (with support from common.h). There are 3 rules: 1. There are three types of students: undergraduate, MEng and PhD. 2. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2. 3. Undergraduate students require a mark of 50 to pass; graduate students need a 65. ----------------Common.h-------------------------------------------------------------------------------------------------------------------------------------------------------------- #ifndef COMMON_H #define COMMON_H /*Portable macros for declaring C functions visible to C++.*/ #ifdef __cplusplus #define...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
In main.cpp, write a templated function more which takes in two variables of the same type...
In main.cpp, write a templated function more which takes in two variables of the same type and returns whichever variable is greater than (>) the other. You can and may need to add something to the food class in order for more to be able to be called properly. //food.h #ifndef _FOOD_H #define _FOOD_H #include <iostream> #include <string> using namespace std; class Food { private: string name; int quantity; public: Food(); void setName(string newName); void setQuantity(int newQuantity); string getName(); int...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT