這個圖書管理系統功能只完善了一部分
另一部分將在最近補充上去
各部分的註釋非常清晰 希望對大家有點用處
#define true 1
#define false 0
#include <stdio.h>
#include <stdlib.h>
typedef struct book //圖書的信息
{
int key;//編號
char bookname[20];//書名
char authorname[20];//作者
char publishinghouse[20];//出版社
double price;//價格
int quantity;//存餘數量
struct book * Next;
}BookNodeList;
BookNodeList *array_information[10]={0};//保存找到的指針
//查找
BookNodeList *makelist(void);//建立圖書信息鏈表
void findwithname(BookNodeList *L);//通過圖書名字查找
int IsEmpty(BookNodeList* L);
BookNodeList *findwithkey(BookNodeList *L);//通過圖書編號查找,這裏的返回值對確認是否查找成功有幫助
void findwithauthor(BookNodeList *L);//通過圖書作者查找
void findwithpublishname(BookNodeList *L);//通過出版社查找
void AddItem(BookNodeList * L);//添加項目
void deletebook(BookNodeList *L);//刪除圖書
void modify(BookNodeList *L);//修改信息
void initialize_record(BookNodeList *L);//從文件中提取信息初始化鏈表
void save(BookNodeList *L);//將鏈表的數據存儲
int main(void)
{
BookNodeList* X;
//創建一個圖書信息鏈表
BookNodeList* L;
L=makelist();
//L是鏈表的頭指針
initialize_record(L);
char c='0';
int a;
loop :
printf(" Book Management System \n");
printf("\t*************************Menu**********************************\n");
printf("\t* 1 Add book 2 delete book *\n");
printf("\t* 3 find book by key 4 modify record *\n");
printf("\t* 5 Add member 6 borrow book *\n");
printf("\t* 7 return book 8 save book *\n");
printf("\t* 9 save member 10 find book by author *\n");
printf("\t* 11 find book by publishname 12 quit system *\n");
printf("\t***************************************************************\n");
scanf("%d",&a);
getchar();
switch (a){
case 1:AddItem(L);break;
case 2:deletebook(L);break;
case 3:findwithkey(L);break;
case 4:modify(L);break;
case 5:;break;
case 6:;break;
case 7:;break;
case 8:save(L);break;
case 9:;break;
case 10:findwithauthor(L)
;break;
case 11:findwithpublishname(L);break;
case 12:;break;
default:printf("error\n");
}
printf(" 是否要回到主界面?\n");
printf(" 如果需要回到主界面請輸入B或者b\n" );
scanf("%c",&c);
getchar();
system("cls"); /*清屏*/
if(c=='B'||c=='b')
{
goto loop;
}
//deletebook(2,L);
//添加數據
//AddItem(L);
//char bookname[20]="a";
//char authorname[20]="xie";
//char publishname[20]="xie";
//findwithauthor(authorname,L);
//findwithpublishname(publishname,L);
//findwithname(bookname,L);
return 0;
}
//創建圖書鏈表
BookNodeList* makelist(void)
{
BookNodeList *P;
P=malloc(sizeof(BookNodeList));
P->Next=NULL;
printf("------------------------圖書鏈表創建成功-------------------------\n\n");
return P;
}
//判斷鏈表是否爲空
int IsEmpty(BookNodeList* L)
{
return L->Next==NULL;
}
//通過圖書名查找圖書
void findwithname(BookNodeList *L)
{
char name[20];
printf("請輸入要查找的圖書的名稱\n");
gets(name);
BookNodeList *P;
P=L->Next;
while(P!=NULL&&strcmp(P->bookname,name))
P=P->Next;
if(P==NULL)
{
printf("沒有找到目標圖書");
return ;
}
printf("%d %f %d %s %s %s\n\n",P->key,P->price,
P->quantity,P->bookname,
P->authorname,P->publishinghouse);
}
//通過圖書編號查找圖書
BookNodeList *findwithkey(BookNodeList *L)
{
int key;
printf("請輸入要查找的圖書的編號!\n");
scanf("%d",&key);
getchar();
BookNodeList *P;
P=L->Next;
while(P!=NULL&&key!=P->key)
P=P->Next;
if(NULL==P)
{
printf("沒有找到圖書");
}
return P;
}
//通過圖書作者查找圖書
void findwithauthor(BookNodeList *L)
{
char authorname[20];
printf("請輸入要查找圖書的作者名稱\n");
gets(authorname);
int i;
int j=0;
BookNodeList *P;
P=L->Next;
while(P!=NULL)
{
if(!strcmp(authorname,P->authorname))
array_information[j++]=P;
P=P->Next;
}
for(i=0;i<j;i++)//J的值比數量多一
{printf("%d %f %d %s %s %s\n\n",array_information[i]->key,array_information[i]->price,
array_information[i]->quantity,array_information[i]->bookname,
array_information[i]->authorname,array_information[i]->publishinghouse);
}
}
//通過出版社名稱查找圖書
void findwithpublishname(BookNodeList *L)//將找到的信息放入指針中
{
char publishinghouse[20];
printf("請輸入要查找書本的出版社名稱!\n");
gets(publishinghouse);
int i;
int j=0;
BookNodeList *P;
P=L->Next;
while(P!=NULL)
{
if(!strcmp(publishinghouse,P->publishinghouse))
array_information[j++]=P;
P=P->Next;
}
for(i=0;i<j;i++)//J的值比數量多一
{printf("%d %f %d %s %s %s\n",array_information[i]->key,array_information[i]->price,
array_information[i]->quantity,array_information[i]->bookname,
array_information[i]->authorname,array_information[i]->publishinghouse);
}
}
//添加圖書項目
void AddItem(BookNodeList * L)
{
BookNodeList * P=NULL;
P=(BookNodeList *)malloc(sizeof(BookNodeList));
if(P==NULL)
{
printf("can't malloc");
exit(0);
}
P->Next=NULL;
printf("請輸入圖書編號");
scanf("%d",&P->key);
getchar();
printf("請輸入價格");
scanf("%lf",&P->price);
getchar();
printf("請輸入存餘數量");
scanf("%d",&P->quantity);
getchar();
printf("請輸入圖書名稱");
gets(P->bookname);
printf("請輸入作者名稱");
gets(P->authorname);
printf("請輸入出版社名稱");
gets(P->publishinghouse);
while(L->Next!=NULL)
{
L=L->Next;
}
L->Next=P;
FILE *fp;
if((fp=fopen("圖書數據.txt","a"))==NULL)//對文件是否成功打開的測試
{
printf("can't open a test!\n");
printf("程序即將關閉\n");
exit(2);
}
else
{
printf("---------------------------文件打開成功------------------------------\n");
}
/*此處將數據添加進文件當中*/
fprintf(fp,"%d %f %d %s %s %s\n",P->key,P->price,P->quantity,P->bookname,P->authorname,P->publishinghouse);
printf("-------------------------添加成功!------------------------\n");
if(fclose(fp)==EOF)
{
printf("文件沒有成功關閉!");
exit(5);
}
}
//根據圖書編號刪除圖書
void deletebook(BookNodeList *L)
{
int key;
printf("請輸入要刪除書本的編號!\n");
scanf("%d",&key);
getchar();
if(NULL==findwithkey(L))
{
return;
}
FILE *fp;
if((fp=fopen("test.txt","w+"))==NULL)//對文件是否成功打開的測試
{
printf("can't open a test!\n");
printf("程序即將關閉\n");
exit(2);
}
BookNodeList * pr=L;
BookNodeList * P=pr->Next;
while(P->key!=key)
{
pr=P;
P=P->Next;
}
pr->Next=P->Next;
P->Next=NULL;
free(P);
printf("---------------------------該圖書信息刪除成功----------------------------\n");
save(L);
if(fclose(fp)==EOF)
{
printf("文件沒有成功關閉!");
exit(5);
}
}
//修改圖書信息
void modify(BookNodeList *L)
{
int key;
printf("請輸入要修改圖書的圖書編號!\n");
scanf("%d",&key);
getchar();
BookNodeList *P;
P=L->Next;
while(P!=NULL&&P->key!=key)
P=P->Next;
if(NULL==P)
{
printf("沒有找到要修改的書籍\n");
return;
}
printf("請輸入修改後的圖書名稱");
gets(P->bookname);
printf("請輸入修改後的作者名稱");
gets(P->authorname);
printf("請輸入修改後出版社名稱");
gets(P->publishinghouse);
printf("請輸入修改後的價格");
scanf("%d",&P->price);
getchar();
printf("請輸入修改後存餘數量");
scanf("%d",&P->quantity);
getchar();
printf("-----------------------------圖書信息修改成功------------------------\n");
save(L);
}
//讀取文件信息,初始化圖書鏈表
void initialize_record(BookNodeList *L)
{
BookNodeList bookinformation;
//打開文件
FILE *fp;
if((fp=fopen("圖書數據.txt","r"))==NULL)//對文件是否成功打開的測試
{
printf("can't open a test!\n");
printf("程序即將關閉\n");
exit(2);
}
else
{
printf("---------------------------文件打開成功------------------------------\n");
}
while((fscanf(fp,"%d %lf %d %s %s %s\n",&bookinformation.key,&bookinformation.price,
&bookinformation.quantity,bookinformation.bookname,bookinformation.authorname,
bookinformation.publishinghouse))!=EOF)
{
//給
BookNodeList* P;
P=malloc(sizeof(BookNodeList));
P->Next=NULL;
strcpy(P->bookname,bookinformation.bookname);
P->key=bookinformation.key;
strcpy(P->authorname,bookinformation.authorname);
strcpy(P->publishinghouse,bookinformation.publishinghouse);
P->price=bookinformation.price;
P->quantity=bookinformation.quantity;
L->Next=P;
L=P;
}
if(fclose(fp)==EOF)
{
printf("文件沒有成功關閉!\n");
exit(5);
}
}
//保存當前的圖書鏈表到文件當中
void save(BookNodeList *L)
{
FILE *fp;
if((fp=(fopen("圖書數據.txt","w")))==NULL)
{
printf("不能創建文檔!,程序即將結束,請輸入任意字符結束程序\n");
getchar();
exit(4);
}
else
{
printf("文檔創建成功,請按任意鍵開始存儲!\n");
getchar();
}
while(L->Next!=NULL)
{
fprintf(fp,"%d %f %d %s %s %s\n",L->Next->key,L->Next->price,L->Next->quantity,L->Next->bookname,L->Next->authorname,L->Next->publishinghouse);
L=L->Next;
}
printf("圖書數據存儲成功。!\n");
//關閉文件
if(fclose(fp)==EOF)
{
printf("文件沒有成功關閉!\n");
exit(5);
}
}