This centers on a program that accepts as input two lists of e-books (derived from Project Gutenberg)
This assignment centers on a program that accepts as input two lists of e-books (derived from Project Gutenberg) and merges the two into one, eliminating any duplicate entries during the process. Each e-book is is described by three fields, an ID, the author info, and the title. In input and output, these appear together on a line separated by tab (“t”) characters.
For example, given the input
etext19640 Twain, Mark, 1835-1910 Adventures of Huckleberry Finn
etext13 Carroll, Lewis, 1832-1898 The Hunting of the Snark
etext9038 Twain, Mark, 1835-1910 The Adventures of Tom Sawyer
and
etext9038 Twain, Mark, 1835-1910 The Adventures of Tom Sawyer
etext20595 Twain, Mark, 1835-1910 The Awful German Language
etext23717 Carroll, Lewis, 1832-1898 Jabberwocky
the merge will be
etext13 Carroll, Lewis, 1832-1898 The Hunting of the Snark
etext19640 Twain, Mark, 1835-1910 Adventures of Huckleberry Finn
etext20595 Twain, Mark, 1835-1910 The Awful German Language
etext23717 Carroll, Lewis, 1832-1898 Jabberwocky
etext9038 Twain, Mark, 1835-1910 The Adventures of Tom Sawyer
The output is sorted by the project Gutenberg ID (the “etext…” field).
You are being supplied with a nearly complete working version of the mergeLists application.
Your task is to complete the implementation of the ReadingList class in accordance with the principles for robust and reusable ADTs as described in the lecture notes. You must use a dynamically allocated array as the basis for storing books within the reading list.
You will only be submitting your modified versions of readingList.h and readingList.cpp. You should not change any other source code files that contribute to this project
========================
readingList.cpp:
========================
/*
* readingList.cpp
*/
#include "readingList.h"
#include <algorithm>
using namespace std;
/**
* A list of books, identified by Gutenberg ID.
*/
/**
* Create a new empty reading list, capable of holding up to
* maxCapacity books.
*
* @param maxCapacity maximum number of books this reading list
* can hold.
*/
ReadingList::ReadingList(int maxCapacity)
{
theCapacity = maxCapacity;
theSize = 0;
books = new Book[theCapacity];
}
/**
* How many books currently in this reading list?
*
* @return number of books in the reading list.
*/
int ReadingList::size() const
{
return theSize;
}
/**
* How many books can this reading list hold?
*
* @return max capacity of this reading list.
*/
int ReadingList::capacity() const
{
return theCapacity;
}
/**
* Attempt to alter the capacity of this reading list so that
* it can hold at least newCapcity books. Existing books
* in the reading list are retained.
*
* @param newCapacity maximum number of books we would like to be able
* to store in this reading list.
* @post capacity() >= newCapacity
*/
void ReadingList::reserve(int newCapacity)
{
if (newCapacity >= theSize)
{
// Allocate a new array for the books.
Book *newBooks = new Book[newCapacity];
// Copy the books over to the new array.
for (int i = 0; i < theSize; ++i)
{
newBooks[i] = books[i];
}
// Update member variables
theCapacity = newCapacity;
delete[] books;
books = newBooks;
}
}
/**
* Check to see if a book is contained in the reading list.
*
* @param bookID the ID string of a potential book
* @returns true iff a book with that ID is in the reading list.
*/
bool ReadingList::contains(std::string bookID) const
{
for (int i = 0; i < theSize; ++i)
{
if (books[i].getID() == bookID)
return true;
}
return false;
}
/**
* Return the index (position) of the book with a given ID
* in the reading list.
*
* @param bookID the ID string of a potential book
* @returns The position of a book with that ID in the reading list, or
* -1 if the ID is unknown.
*
* @post find(id) < 0 || get(find(id)).getID() == id
*/
int ReadingList::find(std::string bookID) const
{
for (int i = 0; i < theSize; ++i)
{
if (books[i].getID() == bookID)
return i;
}
return -1;
}
/**
* Return a book based upon ID ordering.
*
* @param i index of a desired book.
* @return the requested book, or Book() if i is illegal.
*
* @pre 0 <= i && i < size()
* @post if i0 <= i1, get(i0).getID() <= get(i1).getID()
*/
Book ReadingList::get(int i) const
{
if (i >= 0 && i < theSize)
return books[i];
else
return Book();
}
/**
* Read a reading list from the input stream, terminating at
* end of stream or at a book with ID "**END**".
*
* @param in the stream to read from.
* @param readingList the reading list variable to hold the input.
* @return the stream from which the data was read.
*/
std::istream &operator>>(std::istream &in, ReadingList &readingList)
{
readingList = ReadingList();
Book b;
while (in >> b)
{
if (b.getID() != "**END**")
{
if (readingList.capacity() <= readingList.size())
{
readingList.reserve(2 * readingList.size() + 1);
}
readingList.add(b);
}
else
{
break;
}
}
return in;
}
/**
* Write a reading list to an output stream.
*
* @param out the stream to print to.
* @param readingList the reading list to be printed.
* @return the stream to which the data was written.
*
*/
std::ostream &operator<<(std::ostream &out, const ReadingList &readingList)
{
for (int i = 0; i < readingList.size(); ++i)
{
out << readingList.get(i) << endl;
}
return out;
}
========================
readingList.h:
========================
/*
* readingList.h
*/
#ifndef READINGLIST_H_
#define READINGLIST_H_
#include <string>
#include <iostream>
#include "book.h"
/**
* A list of books, identified by Gutenberg ID.
*/
class ReadingList {
public:
/**
* Create a new empty reading list, capable of holding up to
* maxCapacity books.
*
* @param maxCapacity maximum number of books this reading list
* can hold.
*/
ReadingList (int maxCapacity = 100);
/**
* How many books currently in this reading list?
*
* @return number of books in the reading list.
*/
int size() const;
/**
* How many books can this reading list hold?
*
* @return max capacity of this reading list.
*/
int capacity() const;
/**
* Attempt to alter the capacity of this reading list so that
* it can hold at least newCapcity books. Existing books
* in the reading list are retained.
*
* @param newCapacity maximum number of books we would like to be able
* to store in this reading list.
* @post capacity() >= newCapacity
*/
void reserve (int newCapacity);
/**
* Add a book to the reading list. If a book with the same ID exists, it is
* replaced by b. Books are kept in order by their IDs.
*
* If there is no room to add another book, calls reserve(size()+1)
* before adding.
*
* @param b A book to add
*
* @pre size() < capacity() || contains(b.getID())
* @post contains(b.getID()) && b == get(b.getID())
*/
void add (Book b);
/**
* Remove a book from the reading list. If a book with the same ID exists, it is
* removed from the list. Otherwise the list is left unchanged.
*
* @param bid ID of a book to remove
*
* @post !contains(bid)
*/
void remove (std::string bid);
/**
* Check to see if a book is contained in the reading list.
*
* @param bookID the ID string of a potential book
* @returns true iff a book with that ID is in the reading list.
*/
bool contains(std::string bookID) const;
/**
* Return the index (position) of the book with a given ID
* in the reading list.
*
* @param bookID the ID string of a potential book
* @returns The position of a book with that ID in the reading list, or
* -1 if the ID is unknown.
*
* @post find(id) < 0 || get(find(id)).getID() == id
*/
int find(std::string bookID) const;
/**
* Return a book at a position (based upon ID ordering).
*
* @param i index of a desired book.
* @return the requested book, or Book() if i is illegal.
*
* @pre 0 <= i && i < size()
* @post if i0 < i1, get(i0).getID() < get(i1).getID()
*/
Book get(int i) const;
private:
//** Do NOT change the next three lines
Book* books;
int theCapacity;
int theSize;
// For use by instructor only
public:
bool sanityCheck();
};
/**
* Read a reading list from the input stream, terminating at
* end of stream or at a book with ID "**END**".
*
* @param in the stream to read from.
* @param readingList the reading list variable to hold the input.
* @return the stream from which the data was read.
*/
std::istream& operator>> (std::istream& in, ReadingList& readingList);
/**
* Write a reading list to an output stream.
*
* @param out the stream to print to.
* @param readingList the reading list to be printed.
* @return the stream to which the data was written.
*
*/
std::ostream& operator<< (std::ostream& out, const ReadingList& readingList);
#endif
========================
book.cpp:
========================
#include "book.h"
using namespace std;
/**
* Create a book.
*
* @param id the Gutenberg ID for this book
* @param authorInfo the author of the book
* @param title the title of the book
*/
Book::Book (std::string theId, std::string authorInfo, std::string theTitle)
: title(theTitle), id(theId), authorName(authorInfo)
{
}
bool Book::operator< (const Book& b) const
{
return id < b.id;
}
bool Book::operator== (const Book& b) const
{
return (id == b.id);
}
std::ostream& operator<< (std::ostream& out, const Book& b)
{
out << b.getID() << "t"
<< b.getAuthor() << "t"
<< b.getTitle();
return out;
}
std::istream& operator>> (std::istream& in, Book& b)
{
string line;
getline (in, line);
if (!in.good())
return in;
int tab1 = line.find ("t");
int tab2 = line.find ("t", tab1+1);
string id = line.substr(0, tab1);
string author = line.substr (tab1+1, tab2-tab1-1);
string title = line.substr(tab2+1);
b.setID (id);
b.setAuthor (author);
b.setTitle (title);
return in;
}
========================
book.h:
========================
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>
#include "counted.h"
/**
* An ebook.
*/
class Book {
private:
std::string title;
std::string id;
std::string authorName;
Counted _counted;
public:
/**
* Create a default book.
*
* @post getID() == "" && getTitle() == "" && getAuthor() == ""
*/
Book() {}
/**
* Create a book.
*
* @param theId the Gutenberg ID for this book
* @param authorInfo the author of the book
* @param theTitle the title of the book
*/
Book (std::string theId, std::string authorInfo, std::string theTitle);
/**
* The Gutenberg project ID for this book
*/
std::string getID() const {return id;}
void setID (std::string ID) {id = ID;}
/**
* Title of this book
*/
std::string getTitle() const {return title;}
void setTitle (std::string theTitle) {title = theTitle;}
/**
* Name of the book's author(s)
*/
std::string getAuthor() const {return authorName;}
void setAuthor (std::string name) {authorName = name;}
/**
* Ordered by ID
*/
bool operator< (const Book&) const;
bool operator== (const Book&) const;
};
// Print a book as id, tab, author, tab, title
std::ostream& operator<< (std::ostream& out, const Book& b);
// Read a book. Input format is id, then author, then title,
// all on one line, separated by tab characters.
std::istream& operator>> (std::istream& in, Book& b);
#endif
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.
