Programming Homework Help

CIS 22B De Anza College Fundamentals of Database Systems Project

 

Assignment:

You will extend your lab 1 to work with the whole book information, not just the title. Your program will use the same input file, but this time you will define a structure to receive the title, author and genre. You can use the same method that you learned to strip the book title to get the other parts. Instead of just a vector of strings (title), you will now need a vector of structures. Each element in the vector will have the book’s title, author and genre. Your program needs to be able to sort the vector by title, author or genre.

Detailed specifications:

  • Define a structure with fields to store each part of the book information: title, author and genre. The structure can be defined in the global scope as it is not a variable. A structure is a type.
  • Declare a variable, whose type is the structure, to receive the book info, and also a vector of structures that will store all the books. Remember that variables should not be global.
  • Read the each line of the file the same way you did before, making it a string stream, but now you will need to call getline on the stringstream repeatedly to get each part following the title. After populating the structure variable with the book info, push it into the vector.
  • Design an interface to let the user of your program choose which field they want to see the books sorted by (title, author or genre). Therefore you will need to define three different compare functions to pass to sort. Here are examples of how you would call sort with different compare functions:

sort (books.begin(), books.end(), compareByTitle);

sort (books.begin(), books.end(), compareByAuthor);

sort (books.begin(), books.end(), compareByGenre);

sort will use the function that you provide to sort the data.

This is how function compareByTitle should look like, assuming Book is the name of your structure:

bool compareByTitle(Book book1, Book book2) { return book1.title < book2.title) }

Display the books sorted by the field that the user chooses. You should display ALL fields no matter how the data is sorted.

I expect that you split your program in several functions, instead of having one big main function. Define separate functions to:

  • Read the data from the file and populate the vector. Notice that vectors are not arrays and need to be passed by reference.
  • Have an interface with the user asking which fields should be used for sorting and call sort with the appropriate compare function.
  • Print the book info. Vectors should be passed by reference to avoid unnecessary copying. Pass it as a const reference since it does not need to be changed inside this function.

Documentation:

All labs should have a comment at the beginning of each source code (.cpp and .h files) that looks like this:

/***************************************************************************
Write the description of program here (this is not the lab instructions!)
Date:
Lab Group #
Partners names:
****************************************************************************/

Each function should have a comment above that looks like this:

/***************************************************************************
Describe what the function does
Parameters: explain
Return value: explain or write none
****************************************************************************/

Input:

Use the same file you used on lab 1 to test your lab 2.

Output:

This is how the output should look like if the user chooses to sort by. Here is an example of an output if the user chooses to sort by genre:

Replay, Ken Grimwood, fantasy

One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction

Ashes, Kenzo Kitakana, fiction

The Pillars of the Earth, Ken Follett, historical fiction

Fall of Giants, Ken Follet, historical fiction

Mindset: The New Psychology of Success, Carol Dweck, psychology

The Dark Forest, Liu Cixin, science fiction

Starting out with c++, Tony Gaddis, technical

Fundamentals of Database Systems, Elmarsi & Navathe, technical

The C++ Programming Language, Bjarne Stroustrup, technical

Take a screenshot of the output your program generates and upload it with your source code.