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...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
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...
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT