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

Question: Your program should open a file, read information from the file, and s

ID: 3888854 • Letter: Q

Question

Question: Your program should open a file, read information from the file, and store that information in your linked list implementation. It should also print this information once the program has finished running.

The file will contain information on n books (0 n 1000). Your program should read each book from the file, store the information in the associated class objects (book in Book, date in Date, etc.), create a new Node to store that information, and then add the new Node to a linked list. Once your program has finished reading the information it should print out the contents of the linked list formatted as shown in the example at the end of this file.

The following classes must be defined by your program:

class Date : Store information pertaining to a Date

class Book : Store information pertaining to a Book

class Node : Used to construct link list and hold Book objects

class LinkedList : Perform various operations (insert, print, etc.) on the linked list.

The following public members must be defined by the Date class:

unsigned int day;

unsigned int month;

unsigned int year;

The following public functions must be defined by the Date class:

Date(void); : constructor taking no arguments

~Date(void); : deconstructor taking no arguments

The following public members must be defined by the Book class:

string title;

string author;

Date published;

string publisher;

float price;

string isbn;

unsigned int pages;

unsigned int copies;

The following public functions must be defined by the Book class:

Book(void); : constructor taking no arguments

~Book(void); : deconstructor taking no arguments

The following private members must be defined by the Node class:

Book *book; : reference to Book structure

Node *next : reference to next Node in linked list

The following public members must be defined by the Node class:

Node(void); : constructor taking no arguments

Node(Book*); : constructor taking reference to predefined Book as argument

Node(Book*,Node*); : constructor taking references to a predefined Book and Node

~Node(void); : deconstructor taking no arguments

The following private members must defined by the LinkedList class:

Node *head; : reference to first Node in list

Node *tail; : reference to last Node in list

The following public functions must be defined by the LinkedList class:

LinkedList(void); : constructor taking no arguments

LinkedList(Book*) : constructor taking reference to predefined Book

~LinkedList(void) : deconstructor taking no arguments

void insert_front(Book*) : insert Book reference into front of list

void insert_rear(Book*) : insert Book reference into end of list

void print_list(void) : print the contents of the list

** Do not change names

The file will be structured as follows:

Title (string)

Author (string)

Published (unsigned int unsigned int unsigned int)

Publisher (string)

Price (float)

ISBN (string)

Pages (unsigned int)

Copies(unsigned int)

** All information stored in the file will not exceed the bounds of its associated type e.g. the number of pages will not exceed the bounds of an unsigned integer.

You may also assume each line ends with a newline (‘ ’) character. Be careful of the differences between the way Windows and Linux systems terminate the line of a text file. Also, do not hard code your program to read 1000 books from the text file; it should only allocate enough space to hold the exact number of books that are stored in a given file.

Example:

$ cat input.txt

Magician: Apprentice

Raymond E. Feist

12 1 1993

Spectra (January 1, 1994)

5.02

0553564943

512

1

Magician: Master

Raymond E. Feist

12 1 1993

Spectra (January 1, 1994)

7.99

0553564935

499

1

$ g++ book_inventory_linked_list.cpp –o submission

$ ./submission

Title: "Magician: Master"

Author: Raymond E. Feist

Published: 12/1/1993

Publisher: Spectra (January 1, 1994)

Price: $7.99

ISBN: 0553564935

Pages: 499

Copies: 1

Title: "Magician: Apprentice"

Author: Raymond E. Feist

Published: 12/1/1993

Publisher: Spectra (January 1, 1994)

Price: $5.02

ISBN: 0553564943

Pages: 512

Copies: 1

Explanation / Answer

Given below is the program for the question along with output. Please do rate the answer if it was helpful. Thank you.

#include <iostream>
#include <fstream>

using namespace std;

class Date
{
public:
unsigned int day;
unsigned int month;
unsigned int year;
Date(void){ day = 1; month = 1; year = 2000;}
~Date(void){}
  
};

class Book
{
public:
string title;
string author;
Date published;
string publisher;
float price;
string isbn;
unsigned int pages;
unsigned int copies;
Book(void)
{
pages = 0;
price = 0;
copies = 0;
}
~Book(void){}
};

class Node
{

  
public:
Book *book;
Node *next;
  
Node(void){book = NULL; next = NULL;}
Node(Book* b)
{
book = b;
next = NULL;
}
  
Node(Book* b, Node* nxt)
{
book = b;
next = nxt;
}
  
~Node(){}
  
};

class LinkedList
{
private:
Node* head;
Node* tail;
public:
LinkedList(void)
{
head = NULL;
tail = NULL;
}
  
LinkedList(Book* b)
{
head = tail = new Node(b);
}
  
~LinkedList()
{
//deallocate all nodes
Node* n = head;
Node* temp;
while(n != NULL)
{
temp = n->next;
delete n;
n = temp;
}
}
  
void insert_front(Book* b)
{
Node* n = new Node(b);
if(head == NULL) //empty?
head = tail = n;
else
{
//add to front
n->next = head;
head = n;
}
}
  
void insert_rear(Book* b)
{
Node* n = new Node(b);
if(head == NULL) //empty?
head = tail = n;
else
{
//add to end
tail->next = n;
tail = n;
}
}
  
void print_list(void)
{
Node* n = head;
while(n != NULL)
{
cout << "Title: " << n->book->title << endl;
cout << "Author: " << n->book->author << endl;
cout << "Published: " << n->book->published.month << "/" << n->book->published.day << "/" << n->book->published.year << endl;
cout << "Publisher: " << n->book->publisher << endl;
cout << "Price: $" << n->book->price << endl;
cout << "ISBN: $" << n->book->isbn << endl;
cout << "Pages: " << n->book->pages << endl;
cout << "Copies: " << n->book->copies << endl;
cout << endl;
n = n->next;
}
}
  
};

int main()
{
string filename = "/Users/raji/Documents/Chegg/c++/Test2/Test2/input.txt";

ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "Could not open input file " << filename << endl;
return 1;
}
  
string title;
LinkedList list;
while(true)
{
getline(infile, title);
if(infile.eof()) //check if eof?
break;
  
Book *b = new Book();
b->title = title;
getline(infile, b->author);
infile >> b->published.month >> b->published.day >> b->published.year;
  
infile.ignore();//ignore newline before using getline()
getline(infile, b->publisher);
infile >> b->price;
  
infile.ignore();//ignore newline before using getline()
getline(infile , b->isbn);
infile >> b->pages;
infile >> b->copies;
infile.ignore(); //ignore newline so that next iteration the books title is read propelry using getline()
  
list.insert_front(b);
}
  
infile.close();
list.print_list();
}

input file: input.txt

Magician: Apprentice
Raymond E. Feist
12 1 1993
Spectra (January 1, 1994)
5.02
0553564943
512
1
Magician: Master
Raymond E. Feist
12 1 1993
Spectra (January 1, 1994)
7.99
0553564935
499
1

output

Title: Magician: Master
Author: Raymond E. Feist
Published: 12/1/1993
Publisher: Spectra (January 1, 1994)
Price: $7.99
ISBN: $0553564935
Pages: 499
Copies: 1

Title: Magician: Apprentice
Author: Raymond E. Feist
Published: 12/1/1993
Publisher: Spectra (January 1, 1994)
Price: $5.02
ISBN: $0553564943
Pages: 512
Copies: 1

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