Source

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 9

/*

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

void sortCost(book arrayBooks[], int numBooks);


void sortPages(book arrayBooks[], int numBooks);
void sortTitle(book arrayBooks[], int numBooks);
void avgtotal(book arrayBooks[], int numBooks, double &totalcost, double
&avgcost);
int main()
{
book someBooks[50];
char answer;
int bookCount, choice;
double total, average;
do
{
cout << "\nHere are your menu options: " << endl;
cout << "\n1. Fill in information for many ebooks. " << endl;
cout << "2. Output the information stored ." << endl;
cout << "3. Sort the books ascending by author. " << endl;
cout << "4. Sort the books descending by cost. " << endl;
cout << "5. Sort the books descending by number of pages. " << endl;
cout << "6. Sort the books ascending by title. " << endl;
cout << "7. Find the average and total costs of the books. " << endl;
cout << "\nEnter the number menu choice which you'd like to perform. ";
cin >> choice;
switch (choice)
{
case 1: bookCount = bookProp(someBooks);
break;
case 2: outputMultBook(bookCount, someBooks);
break;
case 3: sortAuthor(someBooks, bookCount);
cout << "\nYour sorted books: ";
outputMultBook(bookCount, someBooks);
break;
case 4: sortCost(someBooks, bookCount);
cout << "\nYour sorted books: ";
outputMultBook(bookCount, someBooks);
break;
case 5: sortPages(someBooks, bookCount);
cout << "\nYour sorted books: ";
outputMultBook(bookCount, someBooks);
break;
case 6: sortTitle(someBooks, bookCount);
cout << "\nYour sorted books: ";
outputMultBook(bookCount, someBooks);
break;
case 7: avgtotal(someBooks, bookCount, total, average);

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

// array from pass to NumBooks-1


for (place = pass + 1; place < numBooks; place++)
{
if (arrayBooks[place].page > arrayBooks[maxIndex].page)
maxIndex = place;
}
// swap
temp[pass] = arrayBooks[maxIndex];
arrayBooks[maxIndex] = arrayBooks[pass];
arrayBooks[pass] = temp[pass];
}
}
/*
Void function to find the average cost per book and total cost of all the books
Input: parameters: array of data type book, number of books
Output: Passes total cost and average cost by reference
Processing: uses for loop to add up the total, and use that total to find the average
*/
void avgtotal(book arrayBooks[], int numBooks, double &totalcost, double
&avgcost)
{
totalcost = 0;
for (int i = 0; i < numBooks; i++)
{
totalcost += arrayBooks[i].cost;
}
avgcost = (totalcost / (static_cast<double>(numBooks)));
}
/*
Void function that sorts the books based on title ascending using a bubble sort
Input: Parameters - an array of data type book, number of books
Output: returns a sorted array of books ascending by title
Processing: Compares adjacent titles and if one is "greater" than the one after it, it
swaps them. When it swaps, it goes back to the start to compare from the beginning
*/
void sortTitle(book arrayBooks[], int numBooks)

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

You might also like