
Patrick B. answered 07/27/21
Math and computer tutor/teacher
//***************BOOK.h
#ifndef _BOOK
#define _BOOK
#define MAX_NUM_BOOKS (1000)
#define BOOK_CSV_BUFF_LENGTH (1024)
typedef
class Book
{
protected:
char * bookTitle;
char * bookAuthor;
int bookYear;
float bookPrice;
public:
Book();
Book(char * title);
Book(char * title, char * author, int yr, float price);
Book(const Book&);
Book(Book*);
~Book();
void Kill();
void SetBookTitle(char * title);
void SetBookAuthor(char * author);
void SetBookYear(int yr) { bookYear =yr; }
void SetBookPrice(float price) { bookPrice =price; }
int GetBookTitleStrLength();
int GetBookAuthorStrLength();
void GetBookTitle(char*);
void GetBookAuthor(char*);
int GetBookYear() { return(bookYear); }
float GetBookPrice() { return(bookPrice); }
void operator=(const Book&);
bool operator==(Book);
bool operator!=(Book);
void Parse(char*bookBuffCSV);
char* SerializeToCSV(char * bookBuffCSV);
void Input();
void Output(char * strTitleMsg=0, bool flagDisplayHorizontally=false);
} *TBook;
#define BOOK_SIZE (sizeof(Book))
typedef struct _TLibrary
{
int count;
TBook books;
} * TLibrary;
#define LIBRARY_SIZE (sizeof(struct _TLibrary))
#define LIBRARY_FILE ("E:\\library.dat") //PLEASE CHANGE THIS
#define LIBRARY_UPDATE_FATAL_ERROR (-4)
void Library_Init(TLibrary);
int Library_Read(TLibrary);
int Library_Write(TLibrary);
void Library_Dump(TLibrary,char*strTitleMsg=0);
int Library_InsertAddNew(TLibrary,TBook);
int Library_RemoveDelete(TLibrary,int iIndexPos);
int Library_IndexerGetAtIndex(TLibrary, int iIndexPos, TBook);
int Library_IndexerSetAtIndex(TLibrary, int iIndexPos, TBook);
typedef int (*TBookCompareCallbackFuncPtr) (TBook,TBook); //compares two books
typedef int (*_TBookCompareCallbackFuncPtr) (const void*,const void*); //passed to qsort
typedef int (*TBookFilterCallbackFuncPtr)(TBook,void*params); //verfies book meets criteria for filtering
#endif
//**********************************************************************BOOK.CPP
#ifndef _BOOK
#define _BOOK
#define MAX_NUM_BOOKS (1000)
#define BOOK_CSV_BUFF_LENGTH (1024)
typedef
class Book
{
protected:
char * bookTitle;
char * bookAuthor;
int bookYear;
float bookPrice;
public:
Book();
Book(char * title);
Book(char * title, char * author, int yr, float price);
Book(const Book&);
Book(Book*);
~Book();
void Kill();
void SetBookTitle(char * title);
void SetBookAuthor(char * author);
void SetBookYear(int yr) { bookYear =yr; }
void SetBookPrice(float price) { bookPrice =price; }
int GetBookTitleStrLength();
int GetBookAuthorStrLength();
void GetBookTitle(char*);
void GetBookAuthor(char*);
int GetBookYear() { return(bookYear); }
float GetBookPrice() { return(bookPrice); }
void operator=(const Book&);
bool operator==(Book);
bool operator!=(Book);
void Parse(char*bookBuffCSV);
char* SerializeToCSV(char * bookBuffCSV);
void Input();
void Output(char * strTitleMsg=0, bool flagDisplayHorizontally=false);
} *TBook;
#define BOOK_SIZE (sizeof(Book))
typedef struct _TLibrary
{
int count;
TBook books;
} * TLibrary;
#define LIBRARY_SIZE (sizeof(struct _TLibrary))
#define LIBRARY_FILE ("E:\\library.dat") //PLEASE CHANGE THIS
#define LIBRARY_UPDATE_FATAL_ERROR (-4)
void Library_Init(TLibrary);
int Library_Read(TLibrary);
int Library_Write(TLibrary);
void Library_Dump(TLibrary,char*strTitleMsg=0);
int Library_InsertAddNew(TLibrary,TBook);
int Library_RemoveDelete(TLibrary,int iIndexPos);
int Library_IndexerGetAtIndex(TLibrary, int iIndexPos, TBook);
int Library_IndexerSetAtIndex(TLibrary, int iIndexPos, TBook);
typedef int (*TBookCompareCallbackFuncPtr) (TBook,TBook); //compares two books
typedef int (*_TBookCompareCallbackFuncPtr) (const void*,const void*); //passed to qsort
typedef int (*TBookFilterCallbackFuncPtr)(TBook,void*params); //verfies book meets criteria for filtering
#endif
//************************Main.cpp
using namespace std;
#include <iostream>
#ifndef _BOOK
#include "book.h"
#endif
int Menu()
{
int x;
cout <<"***************************"<<endl;
cout <<" (1) INSERT //ADD NEW "<<endl;
cout <<" (2) REMOVE//DELETE "<<endl;
cout <<" (3) DISPLAY BOOKS "<<endl;
cout <<" (0) EXIT//QUIT "<<endl;
cout <<"****************************"<<endl;
cout <<" INPUT SELECTION :>";
cin >>x;
return(x);
}
Go()
{
struct _TLibrary library;
Library_Init(&library);
Library_Read(&library);
int xMenu=-1;
while (xMenu!=0)
{
xMenu =Menu();
if (xMenu==0) { break; }
switch (xMenu)
{
case 1:
{
Book newBook;
newBook.Input();
Library_InsertAddNew(&library,&newBook);
break;
}
case 2:
{
//inputs the 1-based record #
int iIndexPos=-1;
while (iIndexPos<1 || iIndexPos>library.count)
{
cout << "record # :>";
cin >>iIndexPos;
}
int iReturn =Library_RemoveDelete(&library,--iIndexPos);
if (iReturn==LIBRARY_UPDATE_FATAL_ERROR)
{
cout <<"Fatal error occurs while updating the library.... Terminating!!!" <<endl;
return(-1);
}
break;
}
case 3:
{
Library_Dump(&library);
break;
}
}//switch
} //while
return 0;
}
int main(int argc, char** argv)
{
Go();
}
//**************************LIBRARY.DAT
5
Algorithms, Sedgewick:Robert&Wayne:Ken,1988, 54.32
Numerical Analysis,Faires:J.Douglas&Burden:Richard,1992, 65.43
The Empire Strikes Bak,Glut,1982, 9.87
Applied Business Statistics,DiRusso:Lawrence,1955, 35.42
Storm on the Sea,Thompson:Mike,2000, 65.32