Question
C++ Implement a program that reads in a text file, counts how many times each word occurs in the file and outputs the words (and
C++
Implement a program that reads in a text file, counts how many times each word occurs in the file and outputs the words (and counts) in the increasing order of occurrence, i.e. the counts need to be output in sorted order rare words first. Word is any sequence of alphanumeric characters. Whitespace and punctuation marks are to be discarded. That is, the punctuation marks should not be counted either as a part of the word or as a separate word. You are free to make your program case sensitive (Hello and hello are counted as separate words) or case insensitive. File name is supplied on command line. You are to use the following classes.
class WordOccurrence { public: WordOccurrence(const string& word="", int num=0); bool matchWord(const string &); // returns true if word matches stored void increment(); // increments number of occurrences string getWord() const; int getNum() const; private: string word_; int num_; }; class WordList{ public: // add copy constructor, destructor, overloaded assignment // implement comparison as friend friend bool equal(const WordList&, const WordList&); void addWord(const string &); void print(); private: WordOccurrence *wordArray_; // a dynamically allocated array of WordOccurrences // may or may not be sorted int size_; };
Using vectors is not allowed. You may use standard sorting algorithms or implement insertion sort form scratch. For the second class (WordList), implement a copy constructor (implementing deep copy), destructor and an overloaded assignment (either classical or copy-and-swap). Make sure your class works correctly with the following code(testWordList.cpp). For your constructors, use member initialization lists where possible, use default values for function parameters where appropriate. Enhance class interface if necessary.
Milestone. Code that inputs a file and prints a list of words (possibly repeating).
testWordList.cpp :
#include#include #include "WordList.hpp" // class definitions using std::cout; using std::endl; void testfunc(WordList); // function to test pass-by-value for WordList class int main(){ WordList w; // testing regular member functions w.addWord("one"); w.addWord("one"); // adding duplicate w.addWord("two"); w.addWord("three"); cout << "word list is: "; w.print(); WordList w2, w3; w3=w2=w; // testing stacked overloaded assignment w3=w3; // testing protection against self-assingment if(equal(w2,w3)) cout << "w2 is equal to w3" << endl; else cout << "w2 is not equal to w3" << endl; testfunc(w); // testing copy constructor w.print(); // if destructor is implemented correctly // this should print properly after testfunc completes } // tests pass-by-value for object of class varArray void testfunc(WordList myList){ // copy constructor is invoked on "myList" cout << "word list in testfunc: " << endl; myList.print(); } // destructor is invoked when "myList" goes out of scope
===============================================================
Hint: you can use ctype functions to check if a character is alphanumeric. Here is an example use.
ctypedemo.cpp :
#include#include using std::cout; using std::cin; using std::endl; int main(){ char c; // The character to manipulate do{ cout << "Enter the Character(n to stop): "; cin >> c; cout << "Function" << " Result" << endl << "-------------------" << endl // the functions return non-zero value if true << "isalnum" << " " << isalnum(c) << endl << "isalpha" << " " << isalpha(c) << endl << "iscntrl" << " " << iscntrl(c) << endl << "isdigit" << " " << isdigit(c) << endl << "isgraph" << " " << isgraph(c) << endl << "islower" << " " << islower(c) << endl << "isprint" << " " << isprint(c) << endl << "ispunct" << " " << ispunct(c) << endl << "isspace" << " " << isspace(c) << endl << "isupper" << " " << isupper(c) << endl // the functions return integers, we use // type casting (char) to have it printed // in character representation << "tolower" << " " << char(tolower(c)) << endl << "toupper" << " " << char(toupper(c)) << endl; }while(c!='n'); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started