//p03books05
//author
//date 07/11/2001
//date 07/10/2001
//date 07/09/2001
//date 07/06/2001
//date 07/05/2001
// Project #3
// 07/10/2001 to 07/12/2001
// add member function sortBubbleByTitle (learn and use strcmp)
// add function shuffle
// at least 10 records
//
/*
do the following five times
//for (int i=1; i<=5; i++)
// {
Populate books with 10 random records // CBooks bc(10);
display with message "Original" // cout << "Original\n";
// bc.display();
sortBubbleByTitle // bc.sortBubbleByTitle();
display with message "Sorted by Title"// cout << "Sorted\n";
// bc.display();
shuffle // bc.shuffle();
display with message "After shuffling"// cout << "Shuffled\n";
// bc.display();
}
*/
// Project #2
//
// add function sortBubbleByTitle (learn and use strcmp)
// add function shuffle
// at least 10 records
//
/*
do the following five times
Populate books with 10 random records
display with message "Original"
sortBubbleByTitle
display with message "Sorted by Title"
shuffle
display with message "After shuffling"
*/
//////////////////////////////////////////////////////////////////////
//include files
//////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
//////////////////////////////////////////////////////////////////////
//constants
//////////////////////////////////////////////////////////////////////
const int TITLE_LEN = 30;
const int AUTHOR_LEN = 30;
const int MAX_BOOKS = 15;
//////////////////////////////////////////////////////////////////////
//test values
//////////////////////////////////////////////////////////////////////
const char *testTitles[] =
{
"C++ with Pizza Flavor", "Java Learn Now", "COBOL How To Forget",
"FORTRAN 77", "DB2: An Introduction", "XML In 24 Hours",
"UNIX"
};
const int MAX_TITLES = sizeof(testTitles)/sizeof(testTitles[0]);
const char *testAuthors[] =
{
"John", "Adam", "Sumeer", "Natalie", "David", "Rustin"
};
const int MAX_AUTHORS = sizeof(testAuthors)/sizeof(testAuthors[0]);
const int testYears[] = {1977, 1988, 1989, 1991, 2000, 2001};
const int MAX_YEARS = sizeof(testYears)/sizeof(testYears[0]);
void testSortByYear(void);
//////////////////////////////////////////////////////////////////////
//BookType class definition
//////////////////////////////////////////////////////////////////////
class CBook
{
private:
char title[TITLE_LEN+1];
char author[AUTHOR_LEN+1];
int year;
public:
CBook(void);
CBook(char t[], char a[], int y);
CBook(char ch);
void display(void) const;
void copy(const CBook &b2);
void swap(CBook &b2);
friend class CBooks;
};
//////////////////////////////////////////////////////////////////////
//class CBooks
//////////////////////////////////////////////////////////////////////
class CBooks
{
private:
CBook books[MAX_BOOKS];
int count;
public:
CBooks(void);
CBooks(int n);
void display(void) const;
void displayRev(void) const;
void sortBubbleByYear(void);
bool isSortedByYear(void) const;
};
//////////////////////////////////////////////////////////////////////
//main function
//////////////////////////////////////////////////////////////////////
void main(void)
{
srand(time(NULL));
//CBook b1;
//CBook b2("C++ in 10 Days", "John Smith", 2001);
//b1.display();
//b2.display();
//CBook b3('i');
//b3.display();
//b1.copy(b2);
//b1.display();
//b1.swap(b2);
//b1.display();
//b2.display();
//CBook b2[5];
testSortByYear();
}
//////////////////////////////////////////////////////////////////////
//constructor CBooks()
//////////////////////////////////////////////////////////////////////
CBooks::CBooks(void)
{
count = 0;
}
//////////////////////////////////////////////////////////////////////
//constructor CBooks(n)
//////////////////////////////////////////////////////////////////////
CBooks::CBooks(int n)
{
if (n < 0)
n = 0;
if (n > MAX_BOOKS)
n = MAX_BOOKS;
count = n;
for (int i=0; i<n; i++)
{
//get a random title
strcpy(books[i].title, testTitles[rand()%MAX_TITLES]);
//get a random author
strcpy(books[i].author, testAuthors[rand()%MAX_AUTHORS]);
//get a random year
books[i].year = testYears[rand()%MAX_YEARS];
}
}
//////////////////////////////////////////////////////////////////////
//function to display the contents of BookType variable
//////////////////////////////////////////////////////////////////////
void CBook::display(void) const
{
cout << title ;
cout << ", " << author;
cout << ", " << year << endl;
}
//////////////////////////////////////////////////////////////////////
//display()
//////////////////////////////////////////////////////////////////////
void CBooks::display(void) const
{
for (int i=0; i<count; i++)
{
cout << i << ':';
books[i].display();
}
}
//////////////////////////////////////////////////////////////////////
//displayRev()
//////////////////////////////////////////////////////////////////////
void CBooks::displayRev(void) const
{
for (int i=count-1; i>=0; i--)
{
cout << i << ':';
books[i].display();
}
}
//////////////////////////////////////////////////////////////////////
//function to initialize the contents of CBook variable
//////////////////////////////////////////////////////////////////////
CBook::CBook(char t[], char a[], int y)
{
strcpy((*this).title, t);//strcpy(title, t);
strcpy(this->author, a); //strcpy(author, a);
year = y;
}
//////////////////////////////////////////////////////////////////////
//function to initialize the contents of CBook variable
//////////////////////////////////////////////////////////////////////
CBook::CBook(void)
{
strcpy((*this).title, "-1");
strcpy(this->author, "-1");
year = -1;
}
//////////////////////////////////////////////////////////////////////
//function to input the contents of BookType variable
//////////////////////////////////////////////////////////////////////
CBook::CBook(char ch)
{
if (('i' == ch) || ('I' == ch))
{
cout << "Title: "; cin >> title;
cout << "Author: "; cin >> author;
cout << "Year: "; cin >> year;
}
else
{
strcpy((*this).title, "-1");
strcpy(this->author, "-1");
year = -1;
}
}
//////////////////////////////////////////////////////////////////////
//function to copy b2 to self/me
//////////////////////////////////////////////////////////////////////
void CBook::copy(const CBook &b2)
{
strcpy(author, b2.author);
strcpy(title, b2.title);
year = b2.year;
}
//////////////////////////////////////////////////////////////////////
//sortBubbleByYear()
//////////////////////////////////////////////////////////////////////
void CBooks::sortBubbleByYear(void)
{
bool sorted;
int i;
do
{
sorted = true;
for (i=0; i<=count-2; i++)
{
if (books[i].year > books[i+1].year)
{
// swap books[i], books[i+1]
books[i].swap(books[i+1]);
sorted = false;
}
}
}
while (!sorted);
}
//////////////////////////////////////////////////////////////////////
//test function for sortByYear
//////////////////////////////////////////////////////////////////////
void testSortByYear(void)
{
int n = rand()% (MAX_BOOKS + 1);
CBooks bc(n);
cout << "Original\n";
bc.display();
if (bc.isSortedByYear())
cout << "SORTED\n";
else
cout << "NOT SORTED\n";
bc.sortBubbleByYear();
cout << "Sorted\n";
bc.display();
if (bc.isSortedByYear())
cout << "SORTED\n";
else
cout << "NOT SORTED\n";
}
//////////////////////////////////////////////////////////////////////
//checks if sorted by year
//////////////////////////////////////////////////////////////////////
bool CBooks::isSortedByYear(void) const
{
if (count <= 1)
return true;
for (int i=0; i<=count-2; i++)
{
if (books[i].year > books[i+1].year)
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////
//swap(b)
//////////////////////////////////////////////////////////////////////
void CBook::swap(CBook &b2)
{
CBook temp;
temp.copy(*this);
this->copy(b2);
b2.copy(temp);
/*
int tYear;
char tTitle[TITLE_LEN+1];
char tAuthor[AUTHOR_LEN+1];
tYear = year;
strcpy(tTitle, title);
strcpy(tAuthor, author);
...
*/
}
|