p03Books03
Home ] Up ]

 

//p03books03
//author
//date 07/09/2001
//date 07/06/2001
//date 07/05/2001

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



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

  };


/*
//BookType struct definition
struct BookType
  {
  char title[TITLE_LEN+1];
  char author[AUTHOR_LEN+1];
  int year;
  };
*/

/*
void display(BookType b);
void initialize(BookType &b, char t[], char a[], int y);
void input(BookType &b);
void copy(BookType &b1, BookType b2);
void display(BookType books[], int n);
void displayRev(BookType books[], int n);
void sortBubbleByYear(BookType books[], int n);
void populate(BookType books[], int n);   //07/06/2001
void swap(BookType &b1, BookType &b2);
*/


void main(void)
  {
  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();
  }

/*
///main function
void main(void)
  {
  BookType b1, b2;
  initialize(b1, "C++ The Hard Way", "Fowler", 2002);
  display(b1);
  copy(b2, b1);
  display(b2);
  srand(time(NULL));

  BookType books[10];
  populate(books, 5);
  display(books, 5);
  cout << "After sort\n";
  sortBubbleByYear(books, 5);
  display(books, 5);
  }

*/

/*
//function to populate array books[] with n records
void populate(BookType books[], int n)   //07/06/2001
  {
  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;
  }

/*
//function to display the contents of BookType variable
void display(BookType b)
  {
  cout << "Title:  " << b.title << endl;
  cout << "Author: " << b.author << endl;
  cout << "Year:   " << b.year << endl;
  }
*/

/*
//function to display the contents of BookType array
void display(BookType books[], int n)
  {
  for (int i=0; i<=n-1; i++)
    display(books[i]);
  }
*/

/*
//function to display the contents of BookType array
void displayRev(BookType books[], int n)
  {
  for (int i=n-1; i>=0; i--)
    display(books[i]);
  }
*/

//function to initialize the contents of CBook variable
CBook::CBook(char t[], char a[], int y)
  {
  strcpy((*this).title, t);
  strcpy(this->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 initialize the contents of BookType variable
void initialize(BookType &b, char t[], char a[], int y)
  {
  strcpy(b.title, t);
  strcpy(b.author, a);
  b.year = y;
  }

*/

//function to input the contents of BookType variable
CBook::CBook(char ch)
  {
  if ('i' == ch)
    {
    cout << "Title:  "; cin >> title;
    cout << "Author: "; cin >> author;
    cout << "Year:   "; cin >> year;
    }
  }

/*
//function to input the contents of BookType variable
void input(BookType &b)
  {
  cout << "Title:  "; cin >> b.title;
  cout << "Author: "; cin >> b.author;
  cout << "Year:   "; cin >> b.year;
  }
*/

//function to copy b2 to self/me
void CBook::copy(const CBook &b2)
  {
  strcpy(author, b2.author);
  strcpy(title, b2.title);
  year = b2.year;
  }


/*
//function to copy b2 to b1
void copy(BookType &b1, BookType b2)
  {
  strcpy(b1.author, b2.author);
  strcpy(b1.title, b2.title);
  b1.year = b2.year;
  }
*/


//function to sort array books by title
  /*
  do the following
    sorted = true
    complete a pass (set sorted = false if swap took place)
    until sorted


  do the following
    sorted = true
    for i=0 to n-2 do the following
      if books[i] > books[i+1] then
        swap books[i], books[i+1]
        sorted = false
        end if
      end for
    until sorted

  */
/*
void sortBubbleByYear(BookType books[], int n)
  {
  bool sorted;
  int i;
  
  do 
    {
    sorted = true;
    for (i=0; i<=n-2; i++)
      {
      if (books[i].year > books[i+1].year) 
        {
        // swap books[i], books[i+1]
        swap(books[i], books[i+1]);
        sorted = false;
        }
      }
    }
    //incorrect while (sorted);
    while (!sorted); //correct
  }
*/

void CBook::swap(CBook &b2)
  {
  CBook temp;
  temp.copy(*this);
  this->copy(b2);
  b2.copy(temp);
  }

/*
void swap(BookType &b1, BookType &b2)
  {
  BookType temp;
  copy(temp, b1);
  copy(b1, b2);
  copy(b2, temp);
  }
*/