Code:
//class to store info of books and basic method
#include<string>
#include<sstream>
#include<stdlib>
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;
}
void getdata(string &_isbn,string &_title,string &_press,string &_instruction,string &_type,int &_quantity)
{
_isbn=isbn;
_title=title;
_press=press;
_instruction=instruction;
_type=type;
_quantity=quantity;
}
protected:
string isbn;
string title;
string press;
string instruction;
string type;
int quantity;
BMSBook *next;
friend class BMSBooks;
};
class BMSBooks
{
public:
BMSBooks()
{
thebook=NULL;
count=0;
}
~BMSBooks()
{
while(thebook!=NULL)
{
BMSBook *temp;
temp=thebook;
thebook=temp->next;
delete temp;
}
}
void moveforward(BMSBook *find,int index)
{
while(index&&(find->next!=NULL))
{
find=find->next;
index--;
}
}
void searchbyisbn(BMSBook *find,int &cmp,const string &isbn)
{
BMSBook *temp;
temp=thebook;
find=thebook;
long tempindex=1;
long top=count;
long bottom=1;
moveforward(find,(top+bottom)/2-1);
cmp=strcmp(isbn.c_str(),find->isbn.c_str());
do
{
if(!cmp)
{
return;
}
else if(cmp>0)
{
tempindex=(top+bottom)/2;
temp=find;
bottom=tempindex+1;
moveforward(find,(top+bottom)/2-tempindex);
}
else if(cmp<0)
{
find=temp;
top=(top+bottom)/2;
moveforward(find,(top+bottom)/2-tempindex);
}
cmp=strcmp(isbn.c_str(),find->isbn.c_str());
}while(top>bottom);
}
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 *find;
BMSBook *temp;
int cmp;
if(count)
{
searchbyisbn(find,cmp,isbn);
if(!cmp)
{
if((find->quantity+quantity)>MAX_Q)return 2;
find->quantity+=quantity;
return 0;
}
else if(cmp>0)
{
temp=find->next;
find->next=new BMSBook(isbn, title, press, instruction, type,quantity,temp);
if(find->next==NULL)return 1;
count++;
return 0;
}
else if(cmp<0)
{
temp=new BMSBook(find->isbn,find->title,find->press,find->instruction,find->type,find->quantity,find->next);
if(temp==NULL)return 1;
find->isbn=isbn;
find->title=title;
find->press=press;
find->type=type;
find->instruction=instruction;
find->quantity=quantity;
find->next=temp;
count++;
return 0;
}
else
{return -2;}
}
else
{
thebook=new BMSBook(isbn,title,press,instruction,type,quantity,NULL);
if(thebook==NULL)return 1;
count=1;
return 0;
}
}
return -1;
}
bool remove(string isbn,int quantity)
{
if(count)
{
BMSBook *find;
BMSBook *temp;
int cmp;
searchbyisbn(find,cmp,isbn);
if(!cmp)
{
if(find->quantity>quantity)
{
find->quantity-=quantity;
}
else
{
temp=find->next;
find->isbn=temp->isbn;
find->title=temp->title;
find->press=temp->press;
find->instruction=temp->instruction;
find->type=temp->type;
find->quantity=temp->quantity;
find->next=temp->next;
delete temp;
count--;
}
}
}
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")return false;
}
else
{
if(!isdigit(isbn[12]))return false;
istringstream temp(isbn.substr(12,1));
temp>>sum;
if(num!=sum)return false;
}
return true;
}
void movetotop(BMSBook *current)
{
current=thebook;
}
void getcount(long &num)
{
num=count;
}
private:
long count;
BMSBook *thebook;
};