Question: In this assignment, you will be writing a very simple text-processing program. You are asked to write a program that accomplishes the following: A text

In this assignment, you will be writing a very simple text-processing program. You are asked to write a program that accomplishes the following:

A text is to be read by the program, word by word

All words occurring in the text are to be stored as a Word Item; a WordItem is a type of object that stores the word proper and its frequency in a given text

All WordItems from a text are stored in a List so that at any time, the items are listed in alphabetical order of their word component

All WordItems are printed out and the word with the highest frequency count is reported

For this program, you will reuse the List.h header file from the previous lab and lectures. You will generate your own header file WordItem.h with a class WordItem which will maintain two datamembers, a string word and an integer count. It will be ok to place the implementations of he WordItem member functions into WordItem.h since they will have small bodies. Thus, no WordItem.cpp file will be necessary. In addition to our usual library, you will need to also include . Lastly, you will generate your own Hw2Main.cpp file, in which you implement the solution to this assignment as listed in the bullet points above.

This is the List.h

#ifndef LIST_H #define LIST_H

#include using namespace std; template class List { private: struct Node { T data; Node *prev; Node *next;

Node( const T & d = T{ }, Node * p = nullptr,Node * n = nullptr ) : data{ d }, prev{ p }, next{ n }{} };

public: class const_iterator { public: const_iterator( ) : current { nullptr } { }

const T & operator* ( ) const { return retrieve( ); }

const_iterator & operator++ ( ) { current = current->next; return *this; }

const_iterator operator++ ( int ) { const_iterator old = *this; ++( *this ); return old; } const_iterator & operator-- ( ) { current = current->prev; return *this; }

const_iterator operator-- ( int ) { const_iterator old = *this; --( *this ); return old; }

bool operator== ( const const_iterator & rhs ) const { return current == rhs.current; } bool operator!= ( const const_iterator & rhs ) const { return !( *this == rhs ); }

protected: Node *current;

T & retrieve( ) const { return current->data; }

const_iterator( Node *p ) : current{ p } { }

friend class List; }; class iterator : public const_iterator { public: iterator( ) { }

T & operator* ( ) { return const_iterator::retrieve( ); } const T & operator* ( ) const { return const_iterator::operator*( ); }

iterator & operator++ ( ) { this->current = this->current->next; return *this; }

iterator operator++ ( int ) { iterator old = *this; ++( *this ); return old; } const_iterator & operator-- ( ) { this->current = this->current->prev; return *this; }

const_iterator operator-- ( int ) { const_iterator old = *this; --( *this ); return old; }

protected: iterator( Node *p ) : const_iterator{ p } { }

friend class List; }; public: List( ) { init( ); }

~List( ) { clear( ); delete head; delete tail; }

List( const List & rhs ) { init( ); const_iterator itr = rhs.begin(); for (; itr != rhs.end(); ++itr) push_back( *itr ); }

List & operator= ( const List & rhs ) { List copy = rhs; std::swap( *this, copy ); return *this; } List( List && rhs ) : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail } { rhs.theSize = 0; rhs.head = nullptr; rhs.tail = nullptr; }

List & operator= ( List && rhs ) { std::swap( theSize, rhs.theSize ); std::swap( head, rhs.head ); std::swap( tail, rhs.tail );

return *this; }

iterator begin( ) { return iterator ( head->next ); } const_iterator begin( ) const { return const_iterator ( head->next ); } iterator end( ) { return iterator ( tail ); } const_iterator end( ) const { return const_iterator ( tail ); } // Erase item at itr. iterator erase( iterator itr ) { Node *p = itr.current; iterator retVal{ p->next }; p->prev->next = p->next; p->next->prev = p->prev; delete p; --theSize;

return retVal; }

// Insert x before itr. iterator insert( iterator itr, const T & x ) { Node *p = itr.current; ++theSize; return iterator ( p->prev = p->prev->next = new Node{ x, p->prev, p } ); } int size( ) const { return theSize; } bool empty( ) const { return size( ) == 0; }

void clear( ) { while( !empty( ) ) pop_front( ); } T & front( ) { return *begin( ); } const T & front( ) const { return *begin( ); } T & back( ) { return *--end( ); } const T & back( ) const { return *--end( ); } void push_front( const T & x ) { insert( begin( ), x ); } void push_back( const T & x ) { insert( end( ), x ); } void pop_front( ) { erase( begin( ) ); } void pop_back( ) { erase( --end( ) ); } private: int theSize; Node *head; Node *tail; void init( ) { theSize = 0; head = new Node; tail = new Node; head->next = tail; tail->prev = head; } }; #endif

This is some of my work Hw2Main.cpp

#include #include #include "WordItem.h" #include

using namespace std;

template void print_list(list); template void print_lines(list); void alpha_insert(string, list&); string strip_punct(string);

int main() { list wdList; string next; ifstream inp; inp.open("file.txt"); inp >> next; next = strip_punct(next); while (!inp.fail()) { alpha_insert(next, wdList); // DEFINE BELOW inp >> next; next = strip_punct(next); // DEFINE BELOW } inp.close(); // Print out each word and its number of occurrence // in "file.txt"; word:count pair per alpha_insert // Iterate over the wdList and determine the word // among all WordItems that has maximal count; // : export this most frquent word and its count; return 0; }

template void print_list(list lst) { cout << endl; typename list::iterator itr; for (itr = lst.begin(); itr != lst.end(); ++itr) cout << *itr << " "; cout << endl; }

template void print_lines(list lst) { cout << endl; typename list::iterator itr; for (itr = lst.begin(); itr != lst.end(); ++itr) cout << *itr << " " << endl; cout << endl; }

string strip_punct(string x) { for (int i = x.size()-1; i >= 0; i--) { if (ispunct(x[i])) { x.erase(i--,1); i++; } else return x; } return x; }

void alpha_insert(string x, list& wdlst) { WordItem wordit; if (wdlst.empty()) { wordit = WordItem(x); wdlst.insert(wdlst.begin(), wordit); return; } Find proper place and insert... return; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!