Can anyone write code for me?C++ in Visual Studio 2013 Implement a program that
ID: 3679198 • Letter: C
Question
Can anyone write code for me?C++ in Visual Studio 2013
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 decreasing order of occurrence, i.e. the counts need to be output in sorted order. 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
void addWord(const string &);
void printList();
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. For your constructors, use member initialization lists where possible, use default values for function parameters where appropriate. Enhance class interface if necessary.
following code:
// testing the implementation of class WordList
// Mikhail Nesterenko
// 9/04/2015
#include <iostream>
#include <string>
#include "WordList.h" // class definitions
using std::cout; using std::endl;
void testfunc(WordList); // function to test pass-by-value for WordList class
int main(){
WordList wl;
// testing regular member functions
wl.addWord("one");
wl.addWord("one"); // adding duplicate
wl.addWord("two");
wl.addWord("three");
cout << "word list is: ";
wl.printList();
WordList wl2, wl3;
wl3=wl2=wl; // testing overloaded assignment
wl3=wl3; // testing protection against self-assingment
testfunc(wl); // testing copy constructor
wl.printList(); // 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: ";
myList.printList();
} // destructor is invoked when "myList" goes out of scope
Example:
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
using namespace std;
template <class T>
struct less_second : std::binary_function<T,T,bool>
{
inline bool operator()( const T& lhs, const T& rhs )
{
return lhs.second.value < rhs.second.value;
}
};
class WordCounter
{
public:
int value;
WordCounter() : value( 0 ) {}
void operator++ (int)
{
value++;
}
};
ostream& operator<<(ostream& st, WordCounter& wc )
{
return st << wc.value;
}
bool filter(char c)
{
return isalpha( c ) == 0;
}
const string path = "C:\wordlist.txt";
int main()
{
typedef std::pair< string, WordCounter > word_mapping;
map<string, WordCounter> counter;
ifstream input;
input.open( path.c_str() );
if ( !input )
{
cout << "Error in opening file ";
return 0;
}
string tok;
while ( true )
{
input >> tok;
tok.resize( remove_if( tok.begin(), tok.end(), filter) - tok.begin() );
if ( input )
{
counter[ tok ]++;
}
else break;
}
map< string, WordCounter,less<string> >::iterator it = counter.begin();
for ( ; it != counter.end(); it++ )
{
cout << std::setw (15)
<< (*it).first
<< " "
<< (*it).second
<< endl;
}
std::vector< word_mapping > result( counter.begin(), counter.end() );
std::sort( result.begin(), result.end(), less_second<word_mapping>() );
for ( std::vector< word_mapping >::const_iterator mit = result.begin();
mit != result.end();
mit++ )
{
cout << std::setw (15)
<< (*mit).first
<< " "
<< (*mit).second.value
<< endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.