You are on page 1of 8

#include<iostream>

#include<stdlib.h>
#include<string.h>
using namespace std;
int pos;
//BOOK RECORD
struct book
{
int bookno;
char bookname[20];
char author[20];
int price;
int no_of_copies;
book *left;
book *right;
int height;
};
//STUDENT RECORD
struct student
{
int sid;
char sname[20];
char dept[20];
float fine;
struct card
{
int bookno;
int ida,imon,iyr,rda,rmon,ryr;
}b[2];
student *left,*right;
};
typedef struct book *nodeptr;
typedef struct student *nodeptr2;
//AVL TREE
class library
{
public:
void book_purchase(nodeptr &,nodeptr &);
int bsheight(nodeptr &);
nodeptr srl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr drr(nodeptr &);
int max(int,int);
void book_list(nodeptr );
nodeptr search(int ,nodeptr );
nodeptr2 search(int ,nodeptr2 );
int stock_pos(nodeptr &);
void student_det(nodeptr2 &,nodeptr2 &);
void displaystudent(nodeptr2 );
}l;
void library::book_purchase(nodeptr &x,nodeptr &p)
{
if(p==NULL)
{
p=new book;
p->bookno=x->bookno;
strcpy(p->bookname,x->bookname);
strcpy(p->author,x->author);
p->price=x->price;
p->no_of_copies=x->no_of_copies;
p->left=NULL;
p->right=NULL;
p->height=0;
}
else
{
if(x->bookno<p->bookno)
{
book_purchase(x,p->left);
if((bsheight(p->left)-bsheight(p->right))==2)
{
if(x->bookno<p->left->bookno)
p=srl(p);
else
p=drl(p);
}
}
else if(x->bookno>p->bookno)
{
book_purchase(x,p->right);
if((bsheight(p->right)-bsheight(p->left))==2)
{
if(x->bookno>p->right->bookno)
p=srr(p);
else
p=drr(p);
}
}
}
int m,n,d;
m=bsheight(p->left);
n=bsheight(p->right);
d=max(m,n);
p->height=d+1;
}// insert ends //
void library::student_det(nodeptr2 &t,nodeptr2 &p1)
{
if(p1==NULL)
{
p1=new student;
p1->sid=t->sid;
strcpy(p1->sname,t->sname);
strcpy(p1->dept,t->dept);
p1->fine=t->fine;
p1->b[0].bookno=t->b[0].bookno;
p1->b[1].bookno=t->b[1].bookno;
p1->left=NULL;
p1->right=NULL;
}
else if(t->sid<p1->sid)
student_det(t,p1->left);
else if(t->sid>p1->sid)
student_det(t,p1->right);
}// insert ends //
void library::displaystudent(nodeptr2 p)
{
if(p!=NULL)
{
displaystudent(p->left);
{
cout<<"\nstudent id:"<<p->sid;
cout<<"\nstudent name:"<<p->sname;
cout<<"\nstudent dept:"<<p->dept;
cout<<"\n--------------------------------------------";
}
displaystudent(p->right);
}
}
int library::max(int v1,int v2)
{
return((v1>v2)?v1:v2);
}
int library::bsheight(nodeptr &p)
{
int t;
if(p==NULL)
return -1;
else
{
t=p->height;
return t;
}
}
nodeptr library::srl(nodeptr &p1)
{
nodeptr p2;
p2=p1->left;
p1->left=p2->right;
p2->right=p1;
p1->height=max(bsheight(p1->left),bsheight(p1->right))+1;
p2->height=max(bsheight(p2->left),p1->height)+1;
return p2;
}
nodeptr library::srr(nodeptr &p1)
{
nodeptr p2;
p2=p1->right;
p1->right=p2->left;
p2->left=p1;
p1->height=max(bsheight(p1->left),bsheight(p1->right))+1;
p2->height=max(p1->height,bsheight(p2->right))+1;
return p2;
}
nodeptr library::drl(nodeptr &p1)
{
p1->left=srr(p1->left);
return srl(p1);
}
nodeptr library::drr(nodeptr &p1)
{
p1->right=srl(p1->right);
return srr(p1);
}
void library::book_list(nodeptr p)
{
if(p!=NULL)
{
book_list(p->left);
{
cout<<"\nbookno:";
cout<<p->bookno;
cout<<"\nbookname:"<<p->bookname;
cout<<"\nauthor name:"<<p->author;
cout<<"\nprice"<<p->price;
cout<<"\nno of copies:"<<p->no_of_copies;
cout<<"\n_______________________________________\n";
}
book_list(p->right);
}
}
//BINARY SEARCH TREE
nodeptr library::search(int no,nodeptr a)
{
if(a==NULL)
return NULL;
else
if(a->bookno==no)
{
return(a);
}
else if(no<a->bookno)
return search(no,a->left);
else if(no>a->bookno)
return search(no,a->right);
}
//FUNCTION OVERLOADING
nodeptr2 library::search(int sno,nodeptr2 a1)
{
if(a1==NULL)
return NULL;
else
if(sno==a1->sid)
{
return(a1);
}
else if(sno<a1->sid)
return search(sno,a1->left);
else if(sno>a1->sid)
return search(sno,a1->right);
}
class issue:public library
{
public:
void iss(nodeptr &,nodeptr2 &);
void ret(nodeptr &,nodeptr2 &);
void fine_calculation(nodeptr2 &,int );
void stddet(nodeptr2 &);
void bookdet(int ,nodeptr2 &);
};
void issue::iss(nodeptr &p1,nodeptr2 &q)
{
if(p1->no_of_copies>=1)
{
int d;
p1->no_of_copies-=1;
int c;
cout<<"\nenter card no:";
cin>>d;
c=d-1;
q->b[c].bookno=p1->bookno;
cout<<"\nenter issue date:";
cin>>q->b[c].ida>>q->b[c].imon>>q->b[c].iyr;
q->b[c].rda=q->b[c].ida+15;
q->b[c].rmon=q->b[c].imon;
q->b[c].ryr=q->b[c].iyr;
if(q->b[c].imon==1||q->b[c].imon==3||q->b[c].imon==5||q->b[c].imon==7||q->b[c]
.imon==8||q->b[c].imon==10)
{
if(q->b[c].rda>31)
{
q->b[c].rda-=31;
q->b[c].rmon+=1;
}
}
else if(q->b[c].imon==2)
{
if(((q->b[c].iyr%4)==0&&(q->b[c].iyr%400)!=0)||(q->b[c].iyr%100)==0)
{
if(q->b[c].rda>29)
{
q->b[c].rda-=29;
q->b[c].rmon+=1;
}
}
else
{
if(q->b[c].rda>28)
{
q->b[c].rda-=28;
q->b[c].rmon+=1;
}
}
}
else if(q->b[c].imon==12)
{
if(q->b[c].rda>30)
{
q->b[c].rda-=31;
q->b[c].rmon=1;
q->b[c].ryr+=1;
}
}
else
{
if(q->b[c].rda>30)
{
q->b[c].rda-=30;
q->b[c].rmon+=1;
}
}
cout<<"\nissue date:"<<q->b[c].ida<<"\t"<<q->b[c].imon<<"\t"<<q->b[c].iyr;
cout<<"return date:\t"<<q->b[c].rda<<"\t"<<q->b[c].rmon<<"\t"<<q->b[c].ryr;
}
else
cout<<"\nThe book is already issued!!!";
}
void issue::ret(nodeptr &p,nodeptr2 &q)
{
int c,da,mn,yr,d;
p->no_of_copies+=1;
cout<<"\nenter card no:";
cin>>d;
c=d-1;
fine_calculation(q,c);
q->b[c].bookno=0;
cout<<"\nbook returned sucessfully!!!";
}
void issue::fine_calculation(nodeptr2 &q,int c)
{
int da,mn,yr;
cout<<"enter todays date";
cin>>da>>mn>>yr;
if(q->b[c].ryr==yr&&q->b[c].rmon==mn&&q->b[c].rda>=da)
{
q->fine=0;
cout<<"\nfine="<<q->fine;
}
else if((q->b[c].rmon==mn)&&(da>q->b[c].rda))
{
q->fine=((da-(q->b[c].rda))*1);
cout<<"\nfine="<<q->fine;
}
else if((q->b[c].rmon!=mn))
{
q->fine=((((mn-(q->b[c].rmon))*30)-(q->b[c].rda))+da)*1;
cout<<"\nfine="<<q->fine;
}
}
void issue::stddet(nodeptr2 &p)
{
cout<<"\nThe books owned by:"<<p->sname;
cout<<"\nISSUED BOOK NUMBERS:\t"<<p->b[0].bookno;
cout<<"\t";
cout<<p->b[1].bookno;
}
//INORDER TREE TRAVERSAL
void issue::bookdet(int n,nodeptr2 &p)
{
if(p!=NULL)
{
bookdet(n,p->left);
{
if(p->b[0].bookno==n)
cout<<"\n"<<p->sid;
else if(p->b[1].bookno==n)
cout<<"\n"<<p->sid;
}
bookdet(n,p->right);
}
}
int main()
{
nodeptr root,temp,q1,h;
library l;
student s;
nodeptr2 r,t,q3,f;
issue i;
int n,c,ch;
char bname[20],aname[20];
root=NULL;
r=NULL;
while(1)
{
int ch;
cout<<"\nenter choice.......";
cout<<"\n1.Book purchase\n2.Book list\n3.Insert student\n4.Display student detai
ls\n5.Issue\n6.Return\n7.Student details\n8.book details\n";
cin>>ch;
switch(ch)
{
case 1:
do
{
temp=new book;
cout<<"\nenter bookno:";
cin>>temp->bookno;
cout<<"\nenter book name:";
cin>>bname;
strcpy(temp->bookname,bname);
cout<<"\nenter author:";
cin>>aname;
strcpy(temp->author,aname);
cout<<"\nenter price:";
cin>>temp->price;
cout<<"\nenter no of copies:";
cin>>temp->no_of_copies;
temp->left=NULL;
temp->right=NULL;
l.book_purchase(temp,root);
cout<<"\ndo u want to continue(press 1):";
cin>>ch;
}while(ch==1);
break;
case 2:
l.book_list(root);
break;
case 3:
{
do
{
char stname[20],deptn[20];
t=new student;
cout<<"\nenter sid:";
cin>>t->sid;
cout<<"\nenter student name:";
cin>>stname;
strcpy(t->sname,stname);
cout<<"\nenter dept:";
cin>>deptn;
strcpy(t->dept,deptn);
t->fine=0;
t->b[0].bookno=0;
t->b[1].bookno=0;
t->left=NULL;
t->right=NULL;
l.student_det(t,r);
cout<<"\ndo u want to continue(press 1):";
cin>>c;
}while(c==1);
break;
}
case 4:
l.displaystudent(r);
break;
case 5:
int no,sno;
cout<<"\nenter bookno:";
cin>>no;
cout<<"\nenter student id:";
cin>>sno;
h=l.search(no,root);
f=l.search(sno,r);
if(h!=NULL&&f!=NULL)
i.iss(h,f);
break;
case 6:
int no1,sno1;
cout<<"\nenter bookno to be returned:";
cin>>no1;
cout<<"\nenter student id:";
cin>>sno1;
q1=l.search(no1,root);
q3=l.search(sno1,r);
i.ret(q1,q3);
break;
case 7:
cout<<"\nenter the student id:";
cin>>sno;
q3=l.search(sno,r);
i.stddet(q3);
break;
cas8:
cout<<"\nenter book no:";
cin>>no;
i.bookdet(no,r);
break;
default:
cout<<"\nenter valid option:";
}
}
return(0);
}

You might also like