Question: C++ Programming Problem Statement: This program will read books from an input file into your library data structure. A library consists of a list of

C++ Programming

Problem Statement:

This program will read books from an input file into your library data structure. A library consists of a list of books that can be in sorted or unsorted order. As books are read in from the input file, just add them to an internal array that holds books. You will provide two methods on this Library for sorting books after they are read in from the data file. One sort function should sort by the publication year of the books. The other should sort by the author of the book. One of your sorts should be written using SELECTION SORT. The other should be written with MERGESORT. You must write your own sort code. Do not use any built in C++ libraries for sorting.

I have some code down below, but it is pretty far from being right. I do know the struct Book needs to beclass Book and it should be private and not public. The Mergesort needs to be recursive (I have the code for it written below) and I need the sorts to be in seperate files such as Library.cpp / Library.h and Book.cpp / Book.h . Also, please include as descriptive comments as you can.

Requirements

You must be able to add books to your Library using an add method.

You must be able to sort your books in two manners using appropriate methods.

You must be able to display the books in the library after sorting using a print or display method.

You are not required to delete books from the list.

You must prompt the user for the name of an input file to read the books from.

Your list must be able to store at least 100 books. (Use arrays, not linked lists, - so that you can write a mergesort).

You may display your lists to the screen or to an output file. It is your choice. If you use an output file, allow the user to enter the name of the file.

Sample Input File - Here is a sample of a list of books in my input file. There are no blank lines between book data. I will use variations of this file to test your program with other data. You may assume there are no errors in the data file.

We The Living

Rand, Ayn

1936

The Sparrow

Russell, Mary Doria

1996

Foundation

Asimov, Isaac

1951

Hyperion

Simmons, Dan

1989

The Fifth Season

Jemison, N. K.

2015

Kindred

Butler, Octavia

1979

Watership Down

Adams, Richard

1972

Sample Execution (There should be spcaces inbetween the different books but chegg doesnt let me).

Welcome to the book manager. Please enter the name of your input data file: books.txt

The Library Sorted by YEAR is:

TITLE: We The Living

BY: Rand, Ayn

YEAR: 1936

TITLE: Foundation

BY: Asimov, Isaac

YEAR: 1951

TITLE: Watership Down

BY: Adams, Richard

YEAR: 1972

TITLE: Kindred

BY: Butler, Octavia

YEAR: 1979

TITLE: Hyperion

BY: Simmons, Dan

YEAR: 1989

TITLE: The Sparrow

BY: Russell, Mary Doria

YEAR: 1996

TITLE: The Fifth Season

BY: Jemison, N. K.

YEAR: 2015

The Library Sorted by AUTHOR is:

TITLE: Watership Down

BY: Adams, Richard

YEAR: 1972

TITLE: Foundation

BY: Asimov, Isaac

YEAR: 1951

TITLE: Kindred

BY: Butler, Octavia

YEAR: 1979

TITLE: The Fifth Season

BY: Jemison, N. K.

YEAR: 2015

TITLE: We The Living

BY: Rand, Ayn

YEAR: 1936

TITLE: The Sparrow

BY: Russell, Mary Doria

YEAR: 1996

TITLE: Hyperion

BY: Simmons, Dan

YEAR: 1989

End program 5

Here is the recursive Mergesort code:

//----------------------------------------------------------------------------

// MergeSort

// Sorts an array of string into ascending order using mergesort

// Parameters:

// items - contains elements to be sorted

// first - first index in range to be sorted

// last - last index in range to be sorted

// temp - scratch array for sorting

//----------------------------------------------------------------------------

void MergeSort(string items[], int first, int last, string temp[])

{

int middle; // middle index used to divide array in half

if (first < last) { // array has at least 2 items to be sorted

middle = (first + last) / 2;

MergeSort(items, first, middle, temp); // sort left half

MergeSort(items, middle+1, last, temp); // sort right half

Merge(items, first, middle, middle+1, last, temp); // merge two

halves

}

}

------------------------------------------------------

//---------------------------------------------------------------------------------

// Merge function to merge two sorted sections of an array together into asorted result

//---------------------------------------------------------------------------------

void Merge(string items[], int leftFirst, int leftLast, int rightFirst, intrightLast, string temp[])

{

int i; // traversal indexint save;

// save the index of the leftmost item to go back and copy temp elements back into main array

int comparisonResult; i = leftFirst;save = leftFirst;

while (leftFirst <= leftLast && rightFirst <= rightLast) {

comparisonResult = items[leftFirst].compare(items[rightFirst]);

if (comparisonResult < 0) // left is smaller

{

temp[i] = items[leftFirst];

leftFirst++;

}

else

{

temp[i] = items[rightFirst];

rightFirst++;

}

i++;

}

while (leftFirst <= leftLast) {

temp[i] = items[leftFirst];

leftFirst++;

i++;

}

while (rightFirst <= rightLast) { // or copy remaining elements in the right half

temp[i] = items[rightFirst];

rightFirst++;

i++;

} // now move all elements from temp back onto items

for (i = save; i <= rightLast; i++)

items[i] = temp[i];

}

Here is the code I have written so far

// C++ code #include #include #include #include #include

using namespace std;

#define MAX 1000

//Book Structure ((**should be class and public**)) struct book { string name; string author; int year; };

//Library Class class Library { private: book bookList[MAX]; int count; public: Library(){count=0;} void addBook(string Name,string Author,int Year); void sortOnYear(); void sortOnAuthor(); void display(); };

//Add book in Library void Library::addBook(string Name,string Author,int Year) { bookList[count].name=Name; bookList[count].author=Author; bookList[count].year=Year; count++; } //Print all books information present in Library void Library::display() { cout<<"Book Info: "; for(int i=0;i { cout<

//Sort all books of Library based on author names void Library::sortOnAuthor() { int position,d,c;

for (c = 0 ; c < ( count - 1 ) ; c++ ) { position = c;

for ( d = c + 1 ; d < count ; d++ ) { if ( bookList[position].author > bookList[d].author ) position = d; } if ( position != c ) { book b = bookList[c]; bookList[c] = bookList[position]; bookList[position] = b; } }

}

//Sort all books of Library based on year of publication void Library::sortOnYear() { book *temp=new book[count]; int l1,l2,u1,u2,i; int size=1,j,k; while(size { l1=0; k=0; while(l1+size { l2=l1+size; u1=l2-1; u2=(l2+size-1 for(i=l1,j=l2;i<=u1&&j<=u2;k++) if(bookList[i].year<=bookList[j].year) temp[k]=bookList[i++]; else temp[k]=bookList[j++]; for(;i<=u1;k++) temp[k]=bookList[i++]; for(;j<=u2;k++) temp[k]=bookList[j++]; l1=u2+1; } for(i=l1;k temp[k++]=bookList[i]; for(i=0;i bookList[i]=temp[i];

size*=2; } }

int main(int argc,char **argv) { string file,line,bookName,authorName; int year; cout<<"Enter the input file for reading book information:-"; cin>>file; ifstream myfile(file.c_str());

Library lib;

if (myfile.is_open()) {

while ( getline (myfile,line) ) { if(line.empty()) break; bookName=line; getline (myfile,line); authorName=line; getline (myfile,line); year=stoi(line); lib.addBook(bookName,authorName,year); } lib.display(); lib.sortOnAuthor(); cout<<" After sorting on author: "; lib.display(); lib.sortOnYear(); cout<<" After sorting on Year: "; lib.display(); } else cout << "Unable to open file"; return 0; }

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!