//p03books01
//author
//date 07/05/2001
//include files
#include <iostream.h>
#include <string.h>
//constants
const int TITLE_LEN = 20;
const int AUTHOR_LEN = 20;
//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 sortBubble(BookType books[], int n);
//main function
void main(void)
{
BookType b1, b2;
initialize(b1, "C++ The Hard Way", "Fowler", 2002);
display(b1);
copy(b2, b1);
display(b2);
BookType books[10];
initialize(books[0], "title0", "author0", 0);
initialize(books[1], "title1", "author1", 1);
initialize(books[2], "title2", "author2", 2);
initialize(books[3], "title3", "author3", 3);
display(books, 4);
displayRev(books, 4);
}
//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]);
/*
{
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Year: " << books[i].year << endl;
}
*/
}
//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 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
void input(BookType &b)
{
cout << "Title: "; cin >> b.title;
cout << "Author: "; cin >> b.author;
cout << "Year: "; cin >> b.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
void sortBubble(BookType books[], int n)
/*
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
*/
|