#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define HEADER1 " -----------------------------BOOK---------------------------------- \n"
#define HEADER2 " | booknum | book name| author | publish | price | count | \n"
#define FORMAT " | %-4d |%-10s |%-10s|%-10s| %8.2f |%4d |\n"
#define DATA p->bknum,p->bname,p->author,p->pub,p->price,p->count
int saveflag=0;
typedef struct book{
int bknum; /*描述图书编号成员*/
char bname[30]; /*描述书名*/
char author[20] ; /*描述作者*/
char pub[30]; /*出版社信息*/
float price; /*定价*/
int count; /*现存数量*/
struct book *next; /*指针域*/
}Book;
typedef struct lender {
int lendnum; /*借书证号*/
char name[20]; /*学生姓名*/
int stunum; /*学生编号*/
char sex; /*性别*/
Book lend[3]; /*所借图书信息*/
int lendcount;/*借书数目*/
struct lender *next;
} Bk_lender;
void disp_bl(Bk_lender *head);
void disp_bk(Book *head);
/*输入记录信息建立书库表*/
/*当输入书的编号为-1的时候,结束图书的建立*/
void new_bk() {
FILE *fp;/*表文件的指针*/
Book s;
float x;
char bname[30];
printf("input new book datebase name :");
gets(bname);
if ((fp=fopen(bname,"wb"))==NULL) { /*打开文件*/
printf("the new book file error.\n");
exit (0);
}
do {
printf("input the book index:");
scanf("%d",&s.bknum); getchar();
if (s.bknum!=-1) {
printf("input the book name :");
gets(s.bname);
printf("input the author:");
gets(s.author);
printf("input the publish:");
gets(s.pub);
printf("input the price:");
scanf("%f",&x);
s.price=x;
printf("the number of book:");
scanf("%d",&s.count);
s.next=NULL;
fwrite(&s,sizeof(Book),1,fp);}
else break;
}while ( 1);
saveflag=1;
fclose (fp);
}
void new_bl( ) {/*新建一个借阅者表,存储到磁盘中,当编号输入-1的时候,结束建立*/
FILE *fp; /*文件指针*/
Bk_lender s;
char bname[30];
printf("input new lender datebase name :");
gets(bname);
if ((fp=fopen(bname,"wb"))==NULL) {
printf("the new lender file error.\n");
exit (0);
}
do {
printf("input the lender index:");
scanf("%d",&s.lendnum); getchar();
if (s.lendnum!=-1) {
printf("input the name :");
gets(s.name);
printf("input the number of student:");
scanf("%d",&s.stunum); getchar();
printf("input the \'m\'or \'f\',instead of sex:");
s.sex=getchar();
s.lendcount=0;
s.next=NULL;
fwrite(&s,sizeof(Bk_lender),1,fp); }
else {
printf("input over.\n");
break;
}
}while ( 1);
fclose (fp);
}
/*新建一个书籍库表,存储到磁盘中*/
/*保存读入内存中的书籍信息*/
void save_bk(Book *head) {
FILE* fp;
Book *p;
char bname[30];
printf("input the saving file name:");
gets(bname);
fp=fopen(bname,"wb");/*以只写方式打开二进制文件*/
if(fp==NULL){ /*打开文件失败*/
printf("\n open file error!\n");
return;
}
p=head->next; /*当前链表中所有数据写入内存*/
while(p){
fwrite(p,sizeof(Book),1,fp);/*每次写一条记录或一个节点信息至文件*/
p=p->next;
}
fclose(fp); /*关闭此文件*/
}
/*保存读者信息*/
void save_bl(Bk_lender *head) {
FILE* fp;
Bk_lender *p;
char bname[30];
printf("input the saving file name:");
gets(bname);
fp=fopen(bname,"wb");/*以只写方式打开二进制文件*/
if(fp==NULL){ /*打开文件失败*/
printf("\n open file error!\n");
return;
}
p=head->next; /*当前链表中所有数据写入内存*/
while(p){
fwrite(p,sizeof(Bk_lender),1,fp);/*每次写一条记录或一个节点信息至文件*/
p=p->next;
}
fclose(fp); /*关闭此文件*/
}
Bk_lender *read_bl() {/*从磁盘中把所有的借阅者信息读出来*/
FILE *fp;
Bk_lender *bk_head;/*指向表中第一项的指针*/
Bk_lender *bk_last;/*最后一项的指针*/
Bk_lender *p;/*工作指针*/
char bname[30];
bk_head=(Bk_lender *)malloc(sizeof(Bk_lender));
bk_head->next=NULL;
bk_last=bk_head;
printf("input the lender database name:");
gets(bname);
fp=fopen(bname,"rb") ; /*以读的方式打开文件*/
if(fp==NULL) {
printf("\ncan not open file!,creat database\n");
return NULL;
}
/*从文件中读出读者记录,放到链表中存放*/
while(!feof(fp)){
p=(Bk_lender *)malloc(sizeof( Bk_lender ));
if(fread(p,sizeof(Bk_lender),1,fp)==1) { /*一次从文件中读取一条有关于书的记录*/
p->next=NULL;
bk_last->next=p;
bk_last=p;
} }
fclose (fp);
return bk_head; /*返回读出的数据库文件的指针*/
}
Book *read_bk() { /*从磁盘中把所有的图书信息全部读出来*/
FILE *fp;
Book *bk_head;/*指向表中第一项的指针*/
Book *bk_last;/*最后一项的指针*/
Book *p;/*工作指针*/
char bname[30];
bk_head=(Book *)malloc(sizeof(Book));
bk_head->next=NULL;
bk_last=bk_head;
printf("input the book database name:");
gets(bname);
fp=fopen(bname,"rb") ; /*以读的方式打开文件*/
if(fp==NULL) {
printf("\ncan not open file!,creat database\n");
return NULL;
}
/*从文件中读出书籍记录,放到链表中存放*/
while(!feof(fp)){
p=(Book *)malloc(sizeof( Book ));
if(fread(p,sizeof(Book),1,fp)==1) { /*一次从文件中读取一条有关于书的记录*/
p->next=NULL;
bk_last->next=p;
bk_last=p;
} }
fclose (fp);
return bk_head; /*返回读出的数据库文件的指针*/
}
Book * search_bk(Book *head) { /*按书号信息或者书名信息,查询书籍记录*/
int select; /*1:按书名查,2:按书号查,其他:返回*/
Book *p,*r=NULL;
int num;
char str[30];
if(!head->next) { /*若链表为空*/
system("cls"); /*调用清屏函数清屏*/
printf("\nNo book record!\n");
getchar();
return NULL;
}
system("cls");
printf("\n 1 Search by bookname 2 Search by book num \n");
printf(" please choice[1,2]:");
scanf("%d",&select); getchar();
switch (select) {
case 1: /*按书名查询,可以查询多本同名图书,查询结果输出到屏幕上*/
/*同时返回第一个找到的书名的地址*/
printf("input the book name:");
gets(str);
p=head->next;
while(p) {
if(strcmp(p->bname,str)==0) {/*若找到输入的书名记录*/
if (r==NULL) r=p; /*记录第一个找到记录的地址*/
printf(FORMAT,DATA);
}
p=p->next;
}
getchar();
return r; /*返回第一个找到记录的地址*/
case 2: /*按照书号进行查询,只可以查到一本书,返回查找的位置,同时输出信息*/
printf("input the book number:");
scanf("%d",&num);
p=head->next;
while(p) {
if(p->bknum==num) {
printf(FORMAT,DATA);
getchar();
return p; }
p=p->next;
}
}
getchar();
return NULL;
}
int check1(Book *head,int num) { /*查看书号是否已经存在*/
Book *p;
p=head->next;
while(p){
if (p->bknum==num) return 1;
p=p->next;
}
return 0;
}
/*函数的功能是往文件中插入记录结点,函数返回值,插入以后返回链表的头指针*/
Book *insert_bk(Book *head){
Book *p,*r; /*实现添加操作的临时的结构体指针变量*/
int num;
float x;
r=head;
while(r->next!=NULL)
r=r->next; /*将指针移至于链表最末尾,准备添加记录*/
while(1) {
/*一次可输入多条记录,直至输入图书编号为-1时结束记录结点添加操作*/
/*输入学号,保证该书号没有被使用,若输入书籍编号为-1,则退出添加记录操作*/
printf("input the book num:");
p=(Book *) malloc(sizeof(Book));
scanf("%d",&p->bknum); getchar();
while (check1(head,p->bknum)&&p->bknum!=-1){
printf