Source
Source
Source
Apurva Bhogale
CMPSC 121
Project 5
Section 4
Problem: To create a program that stores information about ebooks in an array of
the data type of a structure with properties, title, author, publisher, copyright year,
pages and cost
Input: The properties for each ebook in the array and what sort function or display
the user wants
Output: Menu to choose which function to perform, Display array every time the
user sorts it
Processing: 9 functions - one to input properties for one variable, one to input
properties of multiple ebooks, one to output an array of ebooks, 4 sort functions by
author, cost, number of pages, and title, and one to find the average and total cost
of all the books. In main, there is an array declared of the structures data type.
There is a menu to choose which functions to call using a switch case which runs as
many times as the user wants it to using a do while.
Test Data: Put in test data with all properties of the book "The Great Gatsby" and
"Harry Potter and the Sorcerer's Stone" and "Slaughterhouse Five". And all sorts
happened correctly, and all output functions happened correctly. The spacing and
formatting of the output is correct as well, and the menu performs as its suppose to
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct book
{
string title;
string author;
string publisher;
int copyrightYr;
int page;
double cost;
};
book inputBook();
int bookProp(book groupBook[50]);
void outputBook(book B);
void outputMultBook(int numBook, book arrayOfBooks[]);
void sortAuthor(book arrayBooks[], int numBooks);
cout << "\nThe total cost of all the books is " << total << setprecision(2)
<< fixed << " dollars and your average cost per book is "<< average <<
setprecision(2) << fixed << " dollars.";
break;
default: cout << "The choice you entered is invalid.";
break;
}
cout << "\nDo you want to see the menu again? Enter y for yes and n for no. ";
cin >> answer;
} while (answer == 'y' || answer == 'Y');
return 0;
}
/*
Function to enter data for one single variable of book data type
Input: input title, author, publisher, copyright year, page number and cost of book
Output: returns a variable of data type book
Processing: cout
*/
book inputBook()
{
book A;
char dummy;
cin.get(dummy);
cout << "Enter the title of the book. ";
getline(cin, A.title);
cout << "Enter the author of the book. ";
getline(cin, A.author);
cout << "Enter the publisher of the book. ";
getline(cin, A.publisher);
cout << "Enter the copyright year of the book. ";
cin >> A.copyrightYr;
cout << "Enter the number of pages in the book. ";
cin >> A.page;
cout << "Enter the cost of the book. ";
cin >> A.cost;
cin.get(dummy);
return A;
}
/*
Function to fill array of 50 spaces max of data type book. Returns number of times
user has entered info, array of data type book is parameter
Input: Everything that inputbook function asks to input, but for as many times as
the user wants
Output: Asks user to enter more information and outputs that all allotted info has
been put in if loop has run 50 times, returns number of times that the user has
entered info for a book
Processing: Uses a do while loop to enter info into an aray of data type book, and
calls inputBook function in the do while. Runs again if user wants to and a counter
counts how many times the user enters information.
*/
int bookProp(book groupBook[50])
{
char ans;
int counter = 0;
do
{
cout << "\nEnter the information for book " << counter + 1 << "." << endl;
groupBook[counter] = inputBook();
counter++;
if (counter == 50)
{
cout << "\nYou have entered the maximum allowed data in this group of
books. ";
ans = 'n';
}
if (counter < 50)
{
cout << "\nWould you like to enter information for another book?";
cout << " Enter y for yes and n for no. ";
cin >> ans;
}
} while (ans == 'y' || ans == 'Y');
return counter;
}
/*
Void function to output the attributes of a single book, single variable of data type
book is parameter
Input: Parameter: Single variable of data type book
Output: Properties of a variable of data type book
Processing: cout
*/
void outputBook(book B)
{
cout << B.title << " is written by " << B.author << " and copyrighted in the year
" << B.copyrightYr << ". " << B.publisher << " published the book. It costs " <<
B.cost << " dollars and is " << B.page << " pages long.";
}
/*
Void function to output the attributes of an array of books
Input: Parameters are number of books, and an array of data type book
Output: Number book and properties of said book
Processing: For loop to cycle through an array of books and calls outputBook
function
*/
void outputMultBook(int numBook, book arrayOfBooks[])
{
for (int i = 0; i < numBook; i++)
{
cout << endl << "Book " << i + 1 << ":" << endl;
outputBook(arrayOfBooks[i]);
}
}
/*
void function to do an insertion sort based on author ascending
input: parameters: array of type book and number of books
Output: Returns sorted array of books based on author ascending
Processing: Runs for the contents of the whole array using a for loop. Compares
consecutive elements, and if the name of an author is out of alphabetical order,
bool "sorted" is false, and a marker is used in a while loop that runs as long as it is
not sorted and a swap is made. Then the comparison starts again at the start of the
marker's spot. Stops when the comparison yields less than 0 which means that it is
in alphabetical order
*/
void sortAuthor(book arrayBooks[], int numBooks)
{
int lastBook = numBooks - 2;
int marker;
// temp place marker
book temp[50];
// temp array to store
bool sorted = true;
// tells when array has been sorted
// Insertion sort
for (int i = 0; i <= lastBook; i++)
{
if (arrayBooks[i].author > arrayBooks[i + 1].author)
{
sorted = false;
marker = i;
while (!sorted) // loop to place element in preceding list
{
temp[i] = arrayBooks[marker];
arrayBooks[marker] = arrayBooks[marker + 1];
arrayBooks[marker + 1] = temp[i];
if (marker == 0)
//at beginning of array
sorted = true;
//Check to see if in order
else if (arrayBooks[marker].author < arrayBooks[marker - 1].author)
sorted = true;
else
marker = marker - 1;
//reset marker
}
}
}
}
/*
Void function to perform a selection sort in descending order based on cost
Input: Parameters: array of data type book, number of books
Output: returns sorted array of books descending based on cost
Processing: The largest cost in the array is determined and swapped with the first
element of the array and that continues sending the second largest cost to the
second index of the array and so on and so forth
*/
void sortCost(book arrayBooks[], int numBooks)
{
int pass;
int place;
int maxIndex;
book temp[50];
for (pass = 0; pass<numBooks - 1; pass++)
{
maxIndex = pass;
//find index (subscript) of the smallest component in the
// array from pass to numBooks-1
for (place = pass + 1; place < numBooks; place++)
{
if (arrayBooks[place].cost > arrayBooks[maxIndex].cost)
maxIndex = place;
}
// swap
temp[pass] = arrayBooks[maxIndex];
arrayBooks[maxIndex] = arrayBooks[pass];
arrayBooks[pass] = temp[pass];
}
}
/*
Void function to perform a selection sort based on number of pages in descending
order
Input: Parameters - array of data type books, number of books
Output: Returns sorted array of books based on pages descending
Processing: The largest number of pages of the array is determined and swapped
with the first element of the array and that continues sending the second largest
number of pages to the second index of the array and so on and so forth
*/
void sortPages(book arrayBooks[], int numBooks)
{
int pass;
int place;
int maxIndex;
book temp[50];
for (pass = 0; pass<numBooks - 1; pass++)
{
maxIndex = pass;
//find index (subscript) of the smallest component in the
{
int i;
int lastBook = numBooks - 2;
int first = 0, pass = 0;
book temp[50];
bool sorted = false;
// Tells when sort is complete
while (!sorted)
{
//stops loop
sorted = true;
for (i = first; i <= lastBook; i++)
{
//swap when title is "bigger" aka not in alphabetical order
if (arrayBooks[i].title > arrayBooks[i + 1].title)
{
temp[i] = arrayBooks[i];
arrayBooks[i] = arrayBooks[i + 1];
arrayBooks[i + 1] = temp[i];
sorted = false;
}
}
lastBook = lastBook - 1;
pass++;
}
}