Code:
//class to store info of books and basic method
#include<string>
#include<sstream>
#include<cstdlib>
using namespace std;
const long MAX_C =100000;
const int MAX_Q =10000;
class BMSBook
{
public:
BMSBook()
{
isbn="";
title="";
press="";
instruction="";
type="";
quantity=0;
next=NULL;
}
BMSBook(string _isbn,string _title,string _press,string _instruction,string _type,int _quantity,BMSBook *_next)
{
isbn=_isbn;
title=_title;
press=_press;
instruction=_instruction;
type=_type;
quantity=_quantity;
next=_next;
}
BMSBook(BMSBook *_book)
{
isbn=_book->isbn;
title=_book->title;
press=_book->press;
instruction=_book->instruction;
type=_book->type;
quantity=_book->quantity;
next=_book->next;
}
protected:
string isbn;
string title;
string press;
string instruction;
string type;
int quantity;
BMSBook *next;
friend class BMSBooks;
};
class BMSBooks
{
public:
BMSBooks()
{
thebook=NULL;
current;
count=0;
}
~BMSBooks()
{
while(thebook!=NULL)
{
current=thebook;
thebook=current->next;
delete current;
}
}
bool getdata(string &_isbn,string &_title,string &_press,string &_instruction,string &_type,int &_quantity)
{
if(current!=NULL)
{
_isbn=current->isbn;
_title=current->title;
_press=current->press;
_instruction=current->instruction;
_type=current->type;
_quantity=current->quantity;
return true;
}
return false;
}
bool gettitle(string &_title)
{
if(current!=NULL)
{
_title=current->title;
return true;
}
return false;
}
void moveforward(int index)
{
while(index&&(current->next!=NULL))
{
current=current->next;
index--;
}
}
bool match(string title,string press,string instruction,string type,int quantity)
{
if(isempty())return false;
if(title!="")
{
if(title!=current->title)return false;
}
if(press!="")
{
if(press!=current->press)return false;
}
if(instruction!="")
{
if(instruction!=current->instruction)return false;
}
if(type!="")
{
if(type!=current->type)return false;
}
if(quantity!=0)
{
if(quantity!=current->quantity)return false;
}
return true;
}
bool moveonestep()
{
if(current->next!=NULL)
{
current=current->next;
return true;
}
return false;
}
long searchbyisbn(int &cmp,const string &isbn)
{
BMSBook *temp;
temp=thebook;
movetotop();
long tempindex=1;
long top=count;
long bottom=1;
moveforward((top+bottom)/2-1);
cmp=strcmp(isbn.c_str(),current->isbn.c_str());
do
{
if(!cmp)
{
return (top+bottom)/2;
}
else if(cmp>0)
{
tempindex=(top+bottom)/2;
temp=current;
bottom=tempindex+1;
moveforward((top+bottom)/2-tempindex);
}
else if(cmp<0)
{
current=temp;
top=(top+bottom)/2;
moveforward((top+bottom)/2-tempindex);
}
cmp=strcmp(isbn.c_str(),current->isbn.c_str());
}while(top>bottom);
return -1;
}
short add(string isbn,string title,string press,string instruction,string type,int quantity)
{
if((count>=MAX_C)||(quantity>=MAX_Q))return 2;
if(checkisbn(isbn))
{
BMSBook *temp;
int cmp;
if(count)
{
searchbyisbn(cmp,isbn);
if(!cmp)
{
if((current->quantity+quantity)>MAX_Q)return 2;
current->quantity+=quantity;
return 0;
}
else if(cmp>0)
{
temp=current->next;
current->next=new BMSBook(isbn, title, press, instruction, type,quantity,temp);
if(current->next==NULL)return 1;
count++;
return 0;
}
else if(cmp<0)
{
temp=new BMSBook(current->isbn,current->title,current->press,current->instruction,current->type,current->quantity,current->next);
if(temp==NULL)return 1;
current->isbn=isbn;
current->title=title;
current->press=press;
current->type=type;
current->instruction=instruction;
current->quantity=quantity;
current->next=temp;
count++;
return 0;
}
}
else
{
thebook=new BMSBook(isbn,title,press,instruction,type,quantity,NULL);
if(thebook==NULL)return 1;
count=1;
return 0;
}
}
return -1;
}
bool remove()
{
if(count)
{
BMSBook *temp;
if(current==thebook)
{
thebook=current->next;
delete current;
}
else if(current->next==NULL)
{
delete current;
}
else
{
temp=current->next;
current->isbn=temp->isbn;
current->title=temp->title;
current->press=temp->press;
current->instruction=temp->instruction;
current->type=temp->type;
current->quantity=temp->quantity;
current->next=temp->next;
delete temp;
}
count--;
return true;
}
return false;
}
bool checkisbn(const string &isbn)
{
int sum=0,num=0,num1=0;
if(isbn.size()!=13)return false;
for(int loops=0;loops<12;loops++)
{
if(isdigit(isbn[loops]))
{
istringstream temp(isbn.substr(loops,1));
temp>>num;
sum+=(num*(++num1));
}
}
num=sum%11;
if(num>=10)
{
if((isbn.substr(12,1)!="x")&&(isbn.substr(12,1)!="X"))return false;
}
else
{
if(!isdigit(isbn[12]))return false;
istringstream temp(isbn.substr(12,1));
temp>>sum;
if(num!=sum)return false;
}
return true;
}
bool update(string isbn,string title,string press,string instruction,string type,int quantity)
{
if(count&&(current!=NULL))
{
current->isbn=isbn;
current->title=title;
current->press=press;
current->type=type;
current->instruction=instruction;
current->quantity=quantity;
return true;
}
return false;
}
bool isempty()
{
if(count)return false;
return true;
}
void movetotop()
{
current=thebook;
}
void getcount(long &num)
{
num=count;
}
private:
long count;
BMSBook *thebook;
BMSBook *current;
};