#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include "book.h"
#include "file.h"
char *filename ="book.data";
Book *BookRecord;
void saveToFile(BookList BookRecord)
{
FILE *fp;
Book *b=BookRecord->next;
if((fp=fopen("book.data","wb"))==NULL)
{
return;
}
else
{
while(b!=NULL)
{
fwrite(b,sizeof(Book),1,fp);
b=b->next;
}
fclose(fp);
}
}
void readFromFile(BookList BookRecord)
{
FILE *f;
Book *b;
if ((f=fopen("book.dat","r"))!=NULL)
{
b = BookRecord;
while(!feof(f))
{
Book *newNode=(Book *)malloc(sizeof(Book));
memset(newNode,0,sizeof(newNode));
fread(newNode,sizeof(Book),1,f);
if(feof(f))
{
b->next = NULL;
break;
}
b->next = newNode;
b=newNode;
b->next = NULL;
}
fclose(f);
}
}
void initlist()
{
BookList BookRecord=(Book *)malloc(sizeof(Book));
BookRecord->next=NULL;
}
void bookmanager(BookList BookRecord)
{
int n;
while(n!=6)
{
system("cls");
printf("--------简易的图书管理系统---------\n");
printf("----------1.添加图书--------\n");
printf("----------2.查询图书--------\n");
printf("----------3.编辑图书--------\n");
printf("----------4.删除图书--------\n");
printf("----------5.显示图书--------\n");
printf("----------6.退出系统---------\n");
printf("输入选项代码:");
scanf("%d",&n);
if(n<1 || n>6)
{
printf("您的输入有误!,请重新输入:\n");
system("pause");
}
else
{
switch(n)
{
case 1:
addbook(BookRecord);
break;
case 2:
searchbook(BookRecord);
break;
case 3:
editbook(BookRecord);
break;
case 4:
removebook(BookRecord);
break;
case 5:
Print_BookRecord(BookRecord);
break;
case 6:
break;
default:
break;
}
}
}
}
void addbook(BookList BookRecord)
{
Book * p;
p=(Book*)malloc(sizeof(Book));
printf("请输入需要添加的书号:");
scanf("%d",&p->bookNO);
printf("请输入需要添加的书名:");
scanf("%s",p->bookName);
printf("请输入书籍的出版社:");
scanf("%s",p->publishingHouse);
printf("请输入书籍的作者:");
scanf("%s",p->wName);
p->next=BookRecord->next;
BookRecord->next=p;
}
void Print_BookRecord(BookList BookRecord)
{
Book * p;
int i=1;
printf("图书记录输出:");
printf("\n-----------------------------------------------------------------------------\n");
printf("记录号\t\t书号\t\t书名\t\t出版社\\t作者\n");
p=BookRecord->next;
while(p)
{
printf("%d\t\t%d\t\t%s\t\t%s\\t%s\n",i,p->bookNO,p->bookName,p->publishingHouse,p->wName);
p=p->next;
i++;
}
}
void searchbook(BookList BookRecord)
{
char bookName[50];
Book * p=BookRecord->next;
printf("\n请输入要查找的书名:");
fflush(stdin);
scanf("%s",bookName);
while(p!=NULL && strcmp(p->bookName,bookName)!=0)
{
p=p->next;
}
if(p!=NULL)
{
printf("找到图书!\n");
printf("书号\t\t书名\t\t出版社\\t作者\n");
printf("%d\t\t%s\t\t%s\\t%s\n",p->bookNO,p->bookName,p->publishingHouse,p->wName);
}
else
{
printf("找不到该图书!\n");
}
}
Book * getbook(int bookNO,BookList BookRecord)
{
Book * p=BookRecord->next;
while(p!=NULL && p->bookNO!=bookNO )
{
p=p->next;
}
return p;
}
void editbook(BookList BookRecord)
{
int bookNO;
char a,b;
Book * p;
printf("请输入需要编辑的书号:");
scanf("%d",&bookNO);
p=getbook(bookNO,BookRecord);
if(p)
{
printf("书名是否需要编辑?(y/n):");
scanf("%s",&a);
if(a=='y'||a=='Y')
{
printf("请输入编辑后的书名:");
scanf("%s",p->bookName);
}
if(p)
{
printf("书名是否需要编辑?(y/n):");
scanf("%s",&b);
if(b=='y'||b=='Y')
{
printf("请输入编辑后的出版社:");
scanf("%s",p->publishingHouse);
}
}
printf("编辑成功!\n");
}
else
{
printf("找不到图书!");
}
}
void removebook(BookList BookRecord)
{
int bookNO;
Book * s;
Book *p=BookRecord;
printf("请输入需要删除的书号:");
scanf("%d",&bookNO);
while(p->next!=0 && p->next->bookNO!=bookNO)
{
p=p->next;
}
if(p->next!=0)
{
s=p->next;
p->next=s->next;
free(s);
printf("删除成功!\n");
}
else
{
printf("找不到该图书!\n");
}
}
int main()
{ int n;
BookList BookRecord=(Book *)malloc(sizeof(Book));
BookRecord->next=NULL;
readFromFile(BookRecord);
while(n!=6)
{
system("cls");
printf("--------欢迎来到图书管理---------\n");
printf("--------1·图书管理--------\n");
printf("--------2·退出系统--------\n");
scanf("%d",&n);
if(n<1 || n>3)
{
printf("您的输入有误!请重新输入:\n");
system("pause");
}
else
{
switch(n)
{
case 1:bookmanager(BookRecord);break;
case 2:printf("\n-----谢谢使用------\n");break;
default:break;
}
}
}
saveToFile(BookRecord);
return 0;
}