In: Computer Science
code in. c++
void seen );
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.
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.
complete
#ifndef WORD_H_ #define WORD_H_ #include <string>; 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
complete
#ifndef WORDS_H_ #define WORDS_H_ #include <string>; #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
Below is completed code. You need to use your own input file i have provided "input.txt" just for test for code.
Let me know if you have any problem or doubt. Thank you.
===================================================================
Word.h
========================
#ifndef WORD_H_
#define WORD_H_
#include <string>;
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
========================
Word.cpp
========================
#include "Word.h"
Word::Word(std::string w)
{
word = w;
occurrences = 1;
}
std::string Word::getWord() const
{
return word;
}
int Word::getOccurrences() const
{
return occurrences;
}
void Word::increment()
{
occurrences++;
}
========================
Words.h
========================
#ifndef WORDS_H_
#define WORDS_H_
#include <string>;
#include "Word.h";
class Words
{
private:
struct StackNode
{
Word word;
StackNode* next;
// constructor to add
new nodes
StackNode(std::string w)
: word(w), next(NULL){}
};
StackNode* top;
StackNode* currentItem;
StackNode* findWord(std::string word)
{
// create iterator to
loop over list
StackNode* wordNode =
top;
while (wordNode !=
NULL)
{
// compare string to check for given word
if (wordNode->word.getWord().compare(word) == 0)
{
// return current node
return wordNode;
}
}
return NULL;
}
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
========================
Words.cpp
========================
#include "Words.h"
void Words::remove(std::string word)
{
// find node with given word
StackNode* wordNode = findWord(word);
// nothing to do if word is not in list
if (wordNode != NULL)
{
// remove the word
object from list
}
}
bool Words::find(std::string word) const
{
// if number of occurrence is 0 then word is not
in list
// if word is in list it must have occurrence
more than 0
return getOccurrences(word) > 0;
}
void Words::seen(std::string word)
{
// find node with given word
StackNode* wordNode = top;
// create pointer to add node in list
StackNode* previous = top;
// check for empty list
if (isEmpty()) {
// add first node in
list
top = currentItem = new
StackNode(word);
return;
}
// create a loop to find the word
while (wordNode != NULL)
{
// compare string to
check if word already exist in list
if
(wordNode->word.getWord().compare(word) == 0)
{
// increase occurrence for already existing word
wordNode->word.increment();
// return to avoid adding same word at end of list
return;
}
// move to next
node
previous =
wordNode;
wordNode =
wordNode->next;
}
// if node with given word is not found then add
new word at end of list
previous->next = new StackNode(word);
}
int Words::getOccurrences(std::string word) const
{
// find node with given word and return
occurrences
StackNode* wordNode = top;
// create a loop to find the word
while (wordNode != NULL)
{
// compare string to
check for word
if
(wordNode->word.getWord().compare(word) == 0)
{
return wordNode->word.getOccurrences();
}
// move to next
node
wordNode =
wordNode->next;
}
// if word is not in list then occurrence is
0
return 0;
}
int Words::getLength() const
{
// create an iterator to count number of
words
int num_words = 0;
StackNode* itr = top;
while (itr != NULL)
{
// count current word
and move to next node
num_words++;
itr =
itr->next;
}
return num_words;
}
std::string Words::getNextWord()
{
// if next word is NULL return empty
string
if (currentItem == NULL)
{
return "";
}
else
{
std::string next =
currentItem->word.getWord();
currentItem =
currentItem->next;
return next;
}
}
void Words::resetNextWord()
{
currentItem = top;
}
bool Words::isEmpty() const
{
// list is empty if top element is NULL
return top == NULL;
}
========================
Main.cpp
========================
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Words.h"
int main() {
// ask user to input file name
std::cout << "Enter input file name: ";
std::string filename;
std::cin >> filename;
// create a Words list
Words words;
// open input file
std::ifstream file(filename);
// check for valid file name
if (!file.is_open())
{
std::cerr << "Can not open
input file: " << filename << std::endl;
return 1;
}
// create a loop to read data from file
std::string word;
while (!file.eof()) {
// read each word from file
file >> word;
// call seen function for each
word
words.seen(word);
}
// print statistics
std::cout << std::left << std::setw(20)
<< "Word" << "Occurrences" << std::endl;
std::cout <<
"===============================\n";
int total_words = 0;
int len = words.getLength();
for (int i = 0; i < len; i++) {
std::string w =
words.getNextWord();
int occurence =
words.getOccurrences(w);
std::cout << std::setw(25)
<< w << occurence << std::endl;
total_words += occurence;
}
std::cout <<
"===============================\n";
std::cout << std::setw(25) << "Total
Words: " << total_words << std::endl;
return 0;
}
========================
input.txt
========================
dog
cat
rat
fish
elephant
cat
fish
cow