图书管理系统 C语言

这个图书管理系统功能只完善了一部分
另一部分将在最近补充上去


各部分的注释非常清晰 希望对大家有点用处 

#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(char name[20],BookNodeList *L);//通过图书名字查找
int IsEmpty(BookNodeList* L);
BookNodeList *findwithkey(int key,BookNodeList *L);//通过图书编号查找
void findwithauthor(char authorname[20],BookNodeList *L);//通过图书作者查找
void findwithpublishname(char publishinghouse[20],BookNodeList *L);//通过出版社查找
void AddItem(BookNodeList * L);//添加项目
void deletebook(int key,BookNodeList *L);//删除图书
void modify(int key,BookNodeList *L);//修改信息
void initialize_record(BookNodeList *L);//从文件中提取信息初始化链表
void save(BookNodeList *L);//将链表的数据存储


int main(void)
{








    BookNodeList* X;
    //创建一个图书信息链表
    BookNodeList* L;
    L=makelist();
    //L是链表的头指针
    initialize_record(L);
    //deletebook(2,L);


    //添加数据



    AddItem(L);
    //AddItem(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(char name[20],BookNodeList *L)
{
    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(int key,BookNodeList *L)
{
    BookNodeList *P;
    P=L->Next;

    while(P!=NULL&&key!=P->key)
        P=P->Next;

    if(NULL==P)
    {
        printf("没有找到图书");
    }

    return P;
}
//通过图书作者查找图书
void findwithauthor(char authorname[20],BookNodeList *L)
{
    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(char publishinghouse[20],BookNodeList *L)//将找到的信息放入指针中
{
    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(int key,BookNodeList *L)
{
    if(NULL==findwithkey(1,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(int key,BookNodeList *L)
{



    BookNodeList *P;
    P=L->Next;

    while(P!=NULL&&P->key!=key)
        P=P->Next;


    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("文件没有成功关闭!");
        exit(5);
    }

}
//保存当前的图书链表到文件当中
void save(BookNodeList *L)
{
    FILE *fp;
    if((fp=(fopen("图书数据.txt","w")))==NULL)
    {
        printf("不能创建文档!,程序即将结束,请输入任意字符结束程序");
        getchar();
        exit(4);
    }
    else
    {
        printf("文档创建成功,请按任意键开始存储!");
        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("图书数据存储成功。!");

    //关闭文件
    if(fclose(fp)==EOF)
    {
        printf("文件没有成功关闭!");
        exit(5);
    }

}




  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值