#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define N 10
struct Book
{
int BID;
char name[50];
char writer[50];
char publisher[50];
int num;
int left;
struct Book *prev;
struct Book *next;
};
struct Student
{
int SID;
char name[50];
char Class[50];
int borrow;
int bookID[N];
struct Student *prev;
struct Student *next;
};
struct Record
{
int RID;
int bookID;
int studentID;
time_t lendTime;
time_t returnTime;
struct Record *prev;
struct Record *next;
};
typedef struct Book BOOK;
typedef struct Student STUDENT;
typedef struct Record RECORD;
BOOK *book_head;
STUDENT *student_head;
RECORD *record_head;
int book_id=1;
int student_id=1;
int record_id=1;
void InitDataEntries()
{
book_head=(BOOK *)malloc(sizeof(BOOK));
book_head->prev=book_head;
book_head->next=book_head;
student_head=(STUDENT *)malloc(sizeof(STUDENT));
student_head->next=student_head;
student_head->prev=student_head;
record_head=(RECORD *)malloc(sizeof(RECORD));
record_head->next=record_head;
record_head->prev=record_head;
}
BOOK *AddBook()
{
BOOK *newBook;
BOOK *pB=book_head->next;
newBook=(BOOK *)malloc(sizeof(BOOK));
printf("请输入书名:");
scanf("%s",newBook->name);
printf("请输入作者:");
scanf("%s",newBook->writer);
printf("请输入出版商:");
scanf("%s",newBook->publisher);
printf("请输入书的数量:");
scanf("%d",&newBook->num);
while(pB!=book_head)
{
if(((strcmp(pB->name,newBook->name))==0) && ((strcmp(pB->writer,newBook->writer))==0)
&& ((strcmp(pB->publisher,newBook->publisher))==0))
{
pB->num+=newBook->num;
return pB;
}
pB=pB->next;
}
newBook->left=newBook->num;
newBook->BID=book_id;
newBook->next=book_head;
newBook->prev=book_head->prev;
book_head->prev->next=newBook;
book_head->prev=newBook;
book_id++;
printf("\n插入成功!!!");
return newBook;
}
BOOK *LookBook(int BID)
{
BOOK *pB=book_head->next;
while(pB!=book_head)
{
if(pB->BID==BID)
{
printf("编号:%d\n书名:%s\n作者:%s\n出版商:%s\n数量:%d\n剩余:%d\n",
BID,pB->name,pB->writer,pB->publisher,pB->num,pB->left);
return pB;
}
pB=pB->next;
}
return NULL;
}
BOOK *LookNameB(char Name[])
{
BOOK *pB=book_head->next;
while(pB!=book_head)
{
if((strcmp(pB->name,Name))==0)
{
printf("编号:%d\n书名:%s\n作者:%s\n出版商:%s\n数量:%d\n剩余:%d\n",
pB->BID,pB->name,pB->writer,pB->publisher,pB->num,pB->left);
return pB;
}
pB=pB->next;
}
return NULL;
}
void DeleteBook()
{
int Bid;
BOOK *pB;
RECORD *pR;
int choice;
printf("请输入要删除的书的编号");
scanf("%d",&Bid);
pB=LookBook(Bid);
if(pB!=NULL)
{
printf("你确定要删除吗?是的请按1:");
scanf("%d",&choice);
if(choice==1)
{
pB->next->prev=pB->prev;
pB->prev->next=pB->next;
free(pB);
pR=record_head->next;
while(pR!=record_head)
{
if(pR->bookID==Bid)
{
pR->next->prev=pR->prev;
pR->prev->next=pR->next;
free(pR);
}
pR=pR->next;
}
printf("删除完成");
}
}
}
STUDENT *AddStudent()
{
STUDENT *newStudent;
newStudent=(STUDENT *)malloc(sizeof(STUDENT));
printf("请输入姓名:");
scanf("%s",newStudent->name);
printf("请输入班别:");
scanf("%s",newStudent->Class);
newStudent->SID=student_id;
newStudent->borrow=0;
newStudent->next=student_head;
newStudent->prev=student_head->prev;
student_head->prev->next=newStudent;
student_head->prev=newStudent;
student_id++;
printf("\n插入成功!!!");
return newStudent;
}
STUDENT *LookStudent(int SID)
{
STUDENT *pS=student_head->next;
int i;
while(pS!=student_head)
{
if(pS->SID==SID)
{
printf("学号:%d\n姓名:%s\n班级:%s\n借书数:%d\n",
pS->SID,pS->name,pS->Class,pS->borrow);
printf("\n借的书的ID:");
for(i=0;i<pS->borrow;i++)
printf("%d\t",pS->bookID[i]);
return pS;
}
pS=pS->next;
}
return NULL;
}
STUDENT *LookNameS(char Name[])
{
STUDENT *pS=student_head->next;
int i;
while(pS!=student_head)
{
if((strcmp(pS->name,Name))==0)
{
printf("学号:%d\n姓名:%s\n班级:%s\n借书数:%d\n",
pS->SID,pS->name,pS->Class,pS->borrow);
printf("\n借的书的ID:");
for(i=0;i<pS->borrow;i++)
printf("%d\t",pS->bookID[i]);
return pS;
}
pS=pS->next;
}
return NULL;
}
void LookClassS(char ClassName[])
{
STUDENT *pS=student_head->next;
int i;
while(pS!=student_head)
{
if((strcmp(pS->Class,ClassName))==0)
{
printf("学号:%d\n姓名:%s\n班级:%s\n借书数:%d\n",
pS->SID,pS->name,pS->Class,pS->borrow);
printf("\n借的书的ID:");
for(i=0;i<pS->borrow;i++)
printf("%d\t",pS->bookID[i]);
}
pS=pS->next;
}
return ;
}
void DeleteStudent()
{
STUDENT *pS;
int id;
int choice;
printf("输入要删除的学生学号:");
scanf("%d",&id);
pS=LookStudent(id);
if(pS!=NULL)
{
printf("你确定要删除?是的请按1");
scanf("%d",&choice);
if(choice==1)
{
pS->prev->next=pS->next;
pS->next->prev=pS->prev;
free(pS);
printf("删除完成\n");
return;
}
}
printf("没找到对应的学生!\n");
}
void Lend()
{
BOOK *pB;
STUDENT *pS;
RECORD *pR;
int iBook;
int iStudent;
printf("请输入要借的书的编号:");
scanf("%d",&iBook);
pB=LookBook(iBook);
if(pB==NULL)
{
printf("没有找到这本书");
return;
}
else
{
if(pB->left==0)
{
printf("这本书已经借完了\n");
return;
}
printf("请输入要借书的学生学号:");
scanf("%d",&iStudent);
pS=LookStudent(iStudent);
if(pS==NULL)
{
printf("这个学号的学生不存在!\n");
return;
}
if(pS->borrow>=N)
{
printf("\n你借的书数超过上限了");
return;
}
pS->bookID[pS->borrow]=pB->BID;
pS->borrow++;
pB->left--;
pR=(RECORD *)malloc(sizeof(RECORD));
pR->RID=record_id++;
pR->bookID=pB->BID;
pR->studentID=iStudent;
time(&(pR->lendTime));
pR->returnTime=0;
pR->prev=record_head->prev;
pR->next=record_head;
pR->prev->next=pR;
record_head->prev=pR;
printf("\n借书成功!");
}
}
void Return()
{
BOOK *pB;
RECORD *pR;
STUDENT *pS;
int iBook,istudent,i,j;
struct tm *theTime;
int choice;
printf("请输入要归还的书的编号:");
scanf("%d",&iBook);
pB=LookBook(iBook);
printf("请输入借书人ID:");
scanf("%d",&istudent);
pS=LookStudent(istudent);
if(pB==NULL)
{
printf("此书不存在\n");
return;
}
if(pB->left==pB->num)
{
printf("此书尚未借出\n");
return;
}
pR=record_head->prev;
while(pR!=record_head)
{
if(pR->bookID==iBook && pR->returnTime==0)
{
theTime=localtime(&(pR->lendTime));
printf("借阅人:\n");
pS=LookStudent(pR->studentID);
printf("借阅时间:%d-%d-%d %d:%d\n",theTime->tm_year+1900,theTime->tm_mon+1,theTime->tm_mday,theTime->tm_hour,theTime->tm_min,theTime->tm_sec);
printf("确定要归还?是的请按1:");
scanf("%d",&choice);
if(choice==1)
{
time(&(pR->returnTime));
pB->left++;
for(i=0;i<N;i++)
{
if(pS->bookID[i]==pB->BID)
{
for(j=i;j<pS->borrow-1;j++)
{
pS->bookID[j]=pS->bookID[j+1];
}
break;
}
}
pS->borrow--;
printf("已经归还\n");
}
/* free(theTime); */
return;
}
}
}
void LookBookIDR(int ID)
{
RECORD *pR;
BOOK *pB;
int i=0;
struct tm *theTime;
pB=LookBook(ID);
printf("借阅记录:\n\n");
pR=record_head->prev;
while(pR!=record_head)
{
if(pR->bookID==ID)
{
theTime=localtime(&(pR->lendTime));
printf("借阅人:\n");
LookStudent(pR->studentID);
printf("借阅时间:%d-%d-%d %d:%d\n",theTime->tm_year+1900,theTime->tm_mon+1,theTime->tm_mday,theTime->tm_hour,theTime->tm_min,theTime->tm_sec);
if(pR->returnTime==0)
{
printf("归还时间:尚未归还\n");
}
else
{
theTime=localtime(&(pR->returnTime));
printf("归还时间:%d-%d-%d %d:%d\n",theTime->tm_year+1900,theT
评论1