Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming Problem Statement: This program will read books from an input fi

ID: 3822653 • Letter: C

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 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;
}

Explanation / Answer

Here is the code for the question. Output shown for sample input. Please dont forget to rate the answer if it helped. Thank you very much.

#include <iostream>
#include<fstream>


using namespace std;

#define MAX 1000

class book
{
private:

string name;
string author;
int year;

public:
book()
{
name="none";
author="none";
year=2017;
}

book(string namein,string authorin,int yearin):name(namein),author(authorin),year(yearin)
{}

string get_name()
{
return name;
}
string get_author()
{
return author;
}
int get_year()
{
return year;
}
void set_name(string namein)
{
name = namein;
}
void set_author(string authorin)
{
author=authorin;
}
void set_year(int yearin)
{
year=yearin;
}
};


//library Class
class library
{
private:
book book_list[MAX];
int count;
public:
library(){count=0;}
void add_book(string name,string author,int year);
void sort_on_year();
void sort_on_author();
void display();
int get_count();
friend void merge_sort(book list[],int start,int end);
friend void merge(book list[],int start,int end);
};


void merge(book list[],int first,int last1,int second,int last2,book temp[])
{
int i=first, j=second, k=0;
while(i<=last1 && j<=last2)
{
if(list[i].get_year() <= list[j].get_year())
{
temp[k]=list[i];
k++;
i++;
}
else
{
temp[k]=list[j];
k++;
j++;
}
}

while(i<=last1 )
{

temp[k]=list[i];
k++;
i++;

}
while( j<=last2)
{

temp[k]=list[j];
k++;
j++;

}
for(i=0;i<k;i++)
{
list[first+i]=temp[i];
}

}

void merge_sort(book list[], int first, int last,book 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;

merge_sort(list, first, middle,temp); // sort left half

merge_sort(list, middle+1, last,temp); // sort right half

merge(list, first, middle, middle+1, last,temp); // merge two halves

}

}


//Add book in library
void library::add_book(string name,string author,int year)
{
book_list[count].set_name(name);
book_list[count].set_author(author);
book_list[count].set_year(year);
count++;

}
//Print all books information present in Library
void library::display()
{

for(int i=0;i<count;i++)
{
cout<<"TITLE: "<<book_list[i].get_name()<<endl;
cout<<"BY: "<<book_list[i].get_author()<<endl;
cout<<"YEAR: "<<book_list[i].get_year()<<endl<<endl;
}

}


//Sort all books of Library based on author names using selection sort
void library::sort_on_author()
{
int position,d,c;

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

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

}


//Sort all books of Library based on year of publication using mergesort
void library::sort_on_year()
{
book *temp=new book[count];
merge_sort(book_list,0,count-1,temp);
delete []temp;
}

int library::get_count()
{
return count;
}
int main(int argc,char **argv)
{
string filename,line,book_name,author;
int year;
library lib;


cout<<"Enter the input file for reading book information:-";
cin>>filename;

ifstream file(filename.c_str());


if (file.is_open())
{

while ( getline(file,book_name) )
{
getline(file,author);
getline(file,line);
year=atoi(line.c_str());
lib.add_book(book_name,author,year);
}

file.close();

cout<<"The file contains "<<lib.get_count() <<" books . "<<endl;
cout<<"The original order of books read from file is as follows:"<<endl<<endl;
lib.display();

cout<<"The list of books sorted on author name using selection sort: "<<endl<<endl;
lib.sort_on_author();
lib.display();


cout<<"The list of books sorted on year using mergesort: "<<endl<<endl;
lib.sort_on_year();
lib.display();
}

else
cout << "Unable to open file"<<endl;
return 0;
}

sample input file books.txt

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 output

The file contains 7 books .
The original order of books read from file is as follows:

TITLE: We The Living
BY: Rand, Ayn
YEAR: 1936

TITLE: The Sparrow
BY: Russell, Mary Doria
YEAR: 1996

TITLE: Foundation
BY: Asimov, Isaac
YEAR: 1951

TITLE: Hyperion
BY: Simmons, Dan
YEAR: 1989

TITLE: The Fifth Season
BY: Jemison, N. K.
YEAR: 2015

TITLE: Kindred
BY: Butler, Octavia
YEAR: 1979

TITLE: Watership Down
BY: Adams, Richard
YEAR: 1972

The list of books sorted on author name using selection sort:

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

The list of books sorted on year using mergesort:

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote