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
// 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:
Get Answers For Free
Most questions answered within 1 hours.