图书信息管理系统设计

/*

题目二:  图书信息管理系统设计

问题描述:试设计一图书信息管理系统,图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格等。

实现功能:1、系统以菜单方式工作

2、图书信息录入功能(图书信息用文件保存)--输入

3、图书信息浏览功能--输出

4、查询和排序功能:(至少一种查询方式)--算法

按书名查询

按作者名查询

5、图书信息的删除与修改

知 识 点:结构体、数组、函数、文件等的应用

    明:系统可录入的图书最大数值为100,要求界面友好,易于操作。

 

程序代码:

*/

 

 

// 001.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdio.h"

#include "string.h"

#include "stdlib.h"

#include "malloc.h"

 

int shouldsave=0;//用于图书信息的保存

struct book1

{

    char num[30];//书籍的编号

 

    char name[30];//书籍的名字

 

    char auth[30];//书籍的作者

 

    char birth[30];//出版日期

 

    char place[30];//出版社

 

    long page;//书籍的字数

 

    float  price;//书籍的价格

 

};

 struct book

{

    struct book1  data;

 

    struct book *next;

};

void menu()

{

    printf("/n/n/n");

 

    printf("***********************************欢迎光临***********************************/n");

 

    printf("/n/n");

 

    printf("/t/t 0  退出图书系统/n");

 

    printf("/t/t 1  登记书籍信息/t/t  2 删除书籍信息/n");

 

    printf("/t/t 3  查询书籍信息/t/t  4 修改书籍信息/n");

 

    printf("/t/t 5  保存书籍信息/t/t  6 打印书籍信息/n");

 

    printf("/n/n");

 

    printf("******************************************************************************/n");

 

    printf("                                                      电信10709    tianmo     /n");

 

    printf("                                                      时候: 2009年3月21     /n");

 

}

void printstart()

{

    printf("------------------------------------------------------------------------------/n");

}

void wrong()

{

    printf("/n======>提示:输入错误!/n");

}

void nofind()

{

    printf("/n=====>提示:没有找到相应的书籍!/n");

}

void printc()//用于输出书籍的相关中文

{

    printf("书籍编号  书籍名字 书籍字数 出版日期 出版社 书籍作者书籍价格/n");

}

void printe(struct book *p)//用于输出书籍的信息

{

    printf("%-12s/t%s/t%ld/t%s/t%s/t%s/t%5.2f/n",p->data.num,p->data.name,p->data.page,p->data.birth,p->data.place,p->data.auth,p->data.price);

}

struct book *Locate(struct book *head,char findmess[],char nameornum[]) //该函数用于定位连表中符合要求的接点,并返回该指针

{

        struct book *r;

 

        if(strcmp(nameornum,"num")==0) // 按书籍编号查询

        {

            r=head->next; //r链接在头指针里面

 

            while(r!=NULL)

            {

                         if(strcmp(r->data.num,findmess)==0)

 

                         return r;//返回结点

 

                         r=r->next; //结点向后移动1

            }

        }

        else if(strcmp(nameornum,"name")==0) //按书籍名字查询

        {

                         r=head->next; //r链接在头指针里面

 

                         while(r!=NULL)

                         {

                                     if(strcmp(r->data.name,findmess)==0)

 

                                     return r; //返回结点

 

                                     r=r->next; //结点向后移动1

                         }

        }

        return 0;

}

///

void   Add(struct book *head)  //添加书籍

{

    struct book *p,*p1,*p2;

 

    char num[20];

 

    p1=head;//把头指针付给p1

 

    p2=p1->next;//p2连接在p1后面

 

    while(p1->next !=NULL)//p1放在最后

    {

        p1=p1->next; //结点向后移动一

    }

 

    while(1)//利用无穷循环

    {

        printf("/n");

 

        printf("请您输入书籍编号(0返回上一级菜单):");

 

        scanf("%s",num);

 

        if(strcmp(num,"0")==0)//技术性处理返回主菜单

        {

            break;

        }

 

        while(p2)//利用循环在记录里面查找,看该书籍号码是否已经存在

        {

            if(strcmp(p2->data.num,num)==0)

            {

                printf("====>提示书籍号为'%s'已经存在,若要修改,请选择'4'/n",num);

 

                printf("/n");

 

                printstart();//调用函数

 

                printc();//调用函数

 

                printe(p2);//调用函数

 

                printstart();//调用函数

 

                printf("/n");

 

            }

            p2=p2->next;//结点向后移动1

        }

        p=(struct book *)malloc(sizeof(struct book));//申请一个动态存储区域,它的大小为sizeof(struct book)

 

        strcpy(p->data.num,num);

 

        printf("请你输入书籍名字:");

 

        scanf("%s",p->data.name); //注意:因为name是首地址,故不用加上'&'

 

        getchar(); //用于接收enter

 

        printf("请你输入书籍字数:");

 

        scanf("%ld",&p->data.page); //注意:因为page是长整型数值,必须用上'&'

 

        getchar();  //用于接收enter

 

        printf("请你输入出版日期:");

 

        scanf("%s",p->data.birth); //注意:因为birth是首地址,故不用加上'&'

 

        getchar();  //用于接收enter

 

        printf("请输入书籍的出版社:");

 

        scanf("%s",p->data.place); //注意:因为place是首地址,故不用加上'&'

 

        getchar();  //用于接收enter

 

        printf("请输入书籍的作者:");

 

        scanf("%s",p->data.auth);//注意:因为auth是首地址,故不用加上'&'

 

        getchar(); //用于接收enter

 

        printf("请输入书籍的价格:");

 

        scanf("%f",&p->data.price);//注意:因为price是浮点型数值,必须用上'&'

 

        getchar(); //用于接收enter

 

        p->next =NULL;//p后面指空

 

        p1->next =p;//p连接在p1的后面

 

        p1=p;//p赋给p1

 

        shouldsave=1;//用于后面的保存

       

    }

}

///

void  Qur(struct book *head) // 查询书籍 

{

     int sel;

 

     char findmess[20];

 

     struct book *p;

 

     if(head->next == NULL) //判断head后面是否连接有内容

     {

                  printf("/n=====>提示:没有资料可以查询!/n");

                

     }

     printf("/n=====>1按书籍编号查找/n=====>2按书籍名字查找/n");

 

     scanf("%d",&sel);

 

     if(sel==1)//按书籍编号查询

     {

                  printf("请你输入要查找的书籍的编号:");

 

                  scanf("%s",findmess);

 

                  p=Locate(head,findmess,"num"); //调用locate函数,并且返回指向要查找书籍编号的指针

 

                  if(p)

                  {

                             printf("/t/t/t/t查找结果/n");

 

                             printstart(); //调用函数

 

                             printc();//调用函数

 

                             printe(p); //调用函数

 

                             printstart(); //调用函数

                  }

                  else

                         nofind();

     }

     else if(sel==2) //按书籍名字查询

     {

              printf("请你输入要查找的书籍的名字:");

 

              scanf("%s",findmess);

 

              p=Locate(head,findmess,"name"); //调用locate函数,并且返回指向要查找书籍名字的指针

 

              if(p)

              {

                          printf("/t/t/t/t查找结果/n");

 

                          printstart(); //调用函数

 

                          printc(); //调用函数

 

                          printe(p); //调用函数

 

                          printstart(); //调用函数

              }

              else

                          nofind();

     }

     else

           wrong();

}

///

void  Delete(struct book *head) //删除书籍

{

          int sel;

 

          struct book *p1,*p2;

 

          char findmess[20];

 

         if(head->next==NULL) //判断head后面是否连接有内容

         {

                       printf("/n提示:没有资料可以删除!/n");

                    

         }

 

         printf("/n=====>1按书籍编号删除/n=====>2按书籍名字删除/n");

 

         scanf("%d",&sel);

 

         if(sel==1)

         {

                        printf("请你输入要删除的编号:");

 

                        scanf("%s",findmess);

 

                        p1=Locate(head,findmess,"num"); //调用locate函数,并且返回指向要查找书籍编号的指针

 

                        if(p1)

                        {

                                     p2=head;

 

                                     while(p2->next!=p1) //利用循环找到要指向删除的书籍编号的指针p1

                                     {

                                           p2=p2->next;

                                     }

 

                                     p2->next=p1->next; //p1后面的一个指针连接在p2->next,从而删除了p1->data.num

 

                                     free(p1); //释放p1所占的内存

 

                                     printf("/n提示:该书籍已经成功删除!/n");

                                     

                                     shouldsave=1; //用于后面的保存

                        }

                        else

                        {

                                 nofind();

                        }

         }

         else if(sel==2)

         {

                       printf("请你输入要删除的书籍名字:");

 

                       scanf("%s",findmess);

 

                       p1=Locate(head,findmess,"name"); //调用locate函数,并且返回指向要查找书籍名字的指针

 

                       if(p1)

                       {

                                     p2=head;

 

                                     while(p2->next!=p1) //利用循环找到要指向删除的书籍编号的指针p1

                                     {

                                           p2=p2->next;

                                     }

 

                                     p2->next=p1->next; //p1后面的一个指针连接在p2->next,从而删除了p1->data.name

 

                                     free(p1); //释放p1所占的内存

 

                                     printf("/n提示:该书籍已经成功删除!/n");

 

                                     shouldsave=1;//用于后面的保存

                       }

                       else

                       {

                                     nofind();

                       }

         }

         else

             

             wrong();

}

///

void  Modify(struct book *head) //书籍信息的修改

{

           struct book *p;

 

            char findmess[20];

 

            if(head->next==NULL)

            {

                             printf("/n=====>提示:没有资料可以修改!/n");

 

            }

 

            printf("请你输入要修改的书籍编号:");

 

            scanf("%s",findmess);

 

            p=Locate(head,findmess,"num"); //调用locate函数,并且返回指向要修改书籍编号的指针

 

           if(p)

           {

                             printf("请您输入新书籍编号:");

 

                             scanf("%s",p->data.num); //注意:因为num是首地址,故不用加上'&'

 

                             getchar(); //用于接收enter

 

                             printf("请您输入新书名:");

 

                             scanf("%s",p->data.name); //注意:因为name是首地址,故不用加上'&'

 

                             getchar(); //用于接收enter

 

                             printf("请您输入新的字数:");

 

                             scanf("%ld",&p->data.page); //注意:因为page是长整型数值,必须用上'&'

 

                             getchar(); //用于接收enter

 

                             printf("请您输入新的出版日期:");

 

                             scanf("%s",p->data.birth);

 

                             getchar(); //用于接收enter

 

                             printf("请您输入新的作者:");

 

                             scanf("%s",p->data.auth);

 

                             printf("请您输入新的出版社:");

 

                             scanf("%s",p->data.place);

 

                             printf("请您输入书籍的价格:");

 

                           scanf("%d",&p->data.price);//注意:因为price是浮点型数值,必须用上'&'

 

                            printf("/n=====>提示:资料修改成功!/n");

 

                             shouldsave=1;

           }

          else

                     nofind();

}

///

void   Save(struct book *head) //保存书籍的相关信息

{

          FILE* fp;

 

          struct book *p;

 

          int flag=1,count=0;

 

          fp = fopen("D://tianmomo","wb"); //为输出打开一个二进制的文件

 

          if(fp == NULL)

          {

                    printf("/n=====>提示:重新打开文件时发生错误!/n");

 

                    exit(1); //注意:逃离函数,它在stdlib库里面

 

          }

          p=head->next; //p连接在head的后面

 

          while(p) //判断p是否为空,以便进行循环

          {

                     if(fwrite(p,sizeof(struct book),1,fp)==1) //p中的内容写到fp

                     {

                                        p=p->next; //p向后移动一

 

                                        count++;//用于统计保存书籍记录的数目

                     }

                     else

                     {

                                        flag=0;

 

                                        break; //如果fwrite(p,sizeof(struct book),1,fp)返回值不是一,提前跳出循环

                     }

          }

         if(flag==1)

         {

                               printf("/n=====>提示:文件保存成功.(%d条记录已经保存)./n",count);

 

                               shouldsave=0;

         }

         fclose(fp); //关闭fp文件

}

 

 

///

void   Display(struct book *head) //用于显示保存的书籍的相关信息

{

             int count=0;

 

             struct book *p;

 

             p=head->next; //p连接在head的后面

 

             if(p==NULL)

 

             {

                        printf("/n=====>提示:没有资料可以显示!/n");

 

             }

             printf("/t/t/t/t显示结果/n");

 

             printstart();

 

             printc();

 

             printf("/n");

 

             while(p) //利用循环把书籍的所有信息全部显示

             {

                           printe(p);

 

                           p=p->next; //p向后移动一

             }

            printstart();

 

            printf("/n");

}

///

int main(int argc, char* argv[])

{

    printf("/n");

 

    printf("长江大学电子信息学院");

 

    printf("/t/t 图书管理系统/n");

 

    struct book *head,*p1,*p2;

 

    FILE  *fp;

 

    int count=0,n;

 

    char ch,jian;

 

    p1=head=(struct book *)malloc(sizeof(struct book));//申请一个动态存储区域,并且强制转化为struct book结构体型指针

 

    head->next =NULL;//head后置空

 

    p1=head;//head的赋给p1

 

    if((fp=fopen("D://tianmomo","rb"))==NULL)//为输入打开一个二进制文件,并且判断是否为空

    {

        printf("/n===>提示:文件还不存在,是否创建?(y/n)/n");

 

        scanf("%c",&jian);

 

        if(jian=='y'||jian=='Y')

        {

            fp=fopen("D://tianmomo","wb");//为输出打开一个二进制文件

        }

        else

        {

            exit(0);//逃离函数

        }

    }

    while(!feof(fp))//判断fp文件中的内容是否已经真的读完。注意:如果真的读完,它的返回值为1;否则,返回值为0

    {

          p2=(struct book *)malloc(sizeof(struct book));

 

          if(fread(p2,sizeof(struct book),1,fp)==1)//fp文件中的内容全部放在p2

          {

                p2->next =NULL;//p2后面指置空

 

                p1->next =p2;//p2连接在p1的后面

 

                p1=p2;//p2赋给p1

 

                count++;

 

          }

    }

    fclose(fp);//关闭文件

 

    while(1)//无穷循环

    {

        menu();

 

        printf("请您选择:");

 

        scanf("%d",&n);

 

        if(n==0)

        {

            if(shouldsave==1) //用于为后面的内容保存

            {

                getchar();

 

                printf("/n====>提示:资料已改动,是否将改动保存到文件中?(y/n)/n");

               

                scanf("%c",&ch);

 

                if(ch=='y'||ch=='Y')

 

                Save(head); //调用函数

            }

       

            printf("/n====>提示:已经退出系统!/n");

 

            break;

        }

       

                    switch(n)//选择调用函数

                    {

                    case 1:Add(head);break;

 

                    case 2:Delete(head);break;

 

                    case 3:Qur(head);break;

 

                    case 4:Modify(head);break;

 

                    case 5:Save(head);break;

 

                    case 6:Display(head);break;

 

                    default:wrong();break;

                    }

    }

    return 0;

}


最新代码:

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"

#define MAXLEN 30

int shouldsave=0;//用于图书信息的保存

struct book1
{
    char num[MAXLEN];//书籍的编号
    char name[MAXLEN];//书籍的名字
    char auth[MAXLEN];//书籍的作者
    char birth[MAXLEN];//出版日期
    char place[MAXLEN];//出版社
    long page;//书籍的字数
    float  price;//书籍的价格
};

struct book
{
    struct book1  data;
    struct book *next;

};

void menu()
{
    printf("\n\n\n");
    printf("***********************************欢迎光临***********************************\n");
    printf("\n\n");
    printf("\t\t 0  退出图书系统\n");
    printf("\t\t 1  登记书籍信息\t\t  2 删除书籍信息\n");
    printf("\t\t 3  查询书籍信息\t\t  4 修改书籍信息\n");
    printf("\t\t 5  保存书籍信息\t\t  6 打印书籍信息\n");
    printf("\n\n");
    printf("******************************************************************************\n");
    printf("                                                      电信10709    tianmo     \n");
    printf("                                                      时候: 2009年3月21日     \n");
}

void printstart()
{
    printf("------------------------------------------------------------------------------/n");
}

void wrong()
{
    printf("\n======>提示:输入错误!\n");
}

void nofind()
{
    printf("\n=====>提示:没有找到相应的书籍!\n");
}

void printc()//用于输出书籍的相关中文
{
    printf("书籍编号  书籍名字  书籍字数  出版日期  出版社  书籍作者   书籍价格\n");
}

void printe(struct book *p)//用于输出书籍的信息
{
    printf("%-12s\t%s\t%ld\t%s\t%s\t%s\t%5.2f\n",p->data.num,p->data.name,p->data.page,p->data.birth,p->data.place,p->data.auth,p->data.price);
}

struct book *Locate(struct book *head,char findmess[],char nameornum[]) //该函数用于定位连表中符合要求的接点,并返回该指针
{
	struct book *r;
	if(strcmp(nameornum,"num") == 0) // 按书籍编号查询
	{
		r = head; //把r链接在头指针里面
		while(r != NULL)
		{
			if(strcmp(r->data.num,findmess) == 0)
			{
				return r;//返回结点
			}
			r = r->next; //结点向后移动1
		}
	}
	else if(strcmp(nameornum,"name") == 0) //按书籍名字查询
	{
		r = head; //把r链接在头指针里面
		while(r != NULL)
		{
			if(strcmp(r->data.name,findmess) == 0)
			{
				return r; //返回结点
			}
			r = r->next; //结点向后移动1
		}
	}
	return 0;
}
///
void  Add(struct book *head)  //添加书籍
{
	char num[MAXLEN];
    struct book *p,*p1,*p2;    
    p1 = head;
    p2 = head;

    while(p1->next != NULL)//把p1放在最后
    {
        p1 = p1->next; //结点向后移动一
    }
    while(1)//利用无穷循环添加
    {      
        printf("\n请您输入书籍编号(以0返回上一级菜单):");
        scanf("%s",num);
        if(strcmp(num,"0") == 0)//技术性处理返回主菜单
        {
            break;
        }
        while(p2 == NULL)//利用循环在记录里面查找,看该书籍号码是否已经存在
        {
            if(strcmp(p2->data.num,num)==0)
            {
                printf("====>提示书籍号为'%s'已经存在,若要修改,请选择'4'\n",num);
                printf("\n");
                printstart();//调用函数
                printc();
                printe(p2);//调用函数
                printstart();
                printf("\n");
            }
            p2 = p2->next;//结点向后移动1
        }

        p = (struct book *)malloc(sizeof(struct book));//申请一个动态存储区域,它的大小为sizeof(struct book)
        strcpy(p->data.num,num);
        printf("请你输入书籍名字:");
        scanf("%s",p->data.name); //注意:因为name是首地址,故不用加上'&'
        getchar(); 
        printf("请你输入书籍字数:");
        scanf("%ld",&p->data.page); //注意:因为page是长整型数值,必须用上'&'
        getchar();  
        printf("请你输入出版日期:");
        scanf("%s",p->data.birth); //注意:因为birth是首地址,故不用加上'&'
        getchar();  
        printf("请输入书籍的出版社:");
        scanf("%s",p->data.place); //注意:因为place是首地址,故不用加上'&'
        getchar();  
        printf("请输入书籍的作者:");
        scanf("%s",p->data.auth);//注意:因为auth是首地址,故不用加上'&'
        getchar(); 
        printf("请输入书籍的价格:");
        scanf("%f",&p->data.price);//注意:因为price是浮点型数值,必须用上'&'
        getchar(); 

        p->next = NULL;//将p后面指空
        p1->next = p;//把p连接在p1的后面
        p1 = p;//把p赋给p1
        shouldsave=1;//用于后面的保存 
    }
}
///
void  Qur(struct book *head) // 查询书籍 
{
	int nChoice;
    char findMess[MAXLEN];
    struct book *p;
    if(head == NULL) //判断head是否有内容
    {
		printf("\n=====>提示:没有资料可以查询!\n"); 
	}
    printf("\n=====>1按书籍编号查找\n=====>2按书籍名字查找\n");
    scanf("%d",&nChoice);
    if(nChoice == 1)//按书籍编号查询
    {
		printf("请你输入要查找的书籍的编号:");
		scanf("%s",findMess);
		p = Locate(head,findMess,"num"); //调用locate函数,并且返回指向要查找书籍编号的指针
		if(p != NULL)
		{
			printf("\t\t\t\t查找结果\n");
			printstart(); 
			printc();//调用函数
			printe(p); 
			printstart(); //调用函数
		}
		else
		{
			nofind();
		}

     }
     else if(nChoice == 2) //按书籍名字查询
     {
		 printf("请你输入要查找的书籍的名字:");
		 scanf("%s",findMess);
		 p=Locate(head,findMess,"name"); //调用locate函数,并且返回指向要查找书籍名字的指针
		 if(p != NULL)
		 {
			 printf("\t\t\t\t查找结果\n");
			 printstart(); 
			 printc(); //调用函数
			 printe(p); 
			 printstart(); //调用函数
		 }
		 else
		 {
			 nofind();
		 }
     }
     else
	 {
		 wrong();
	 }
}
///
void  Delete(struct book *head) //删除书籍
{
	int nChoice;
    struct book *p1,*p2;
    char findMess[MAXLEN];
    if(head == NULL) //判断head后面是否连接有内容
    {
		printf("\n提示:没有资料可以删除!\n");                    

    }
    printf("\n=====>1按书籍编号删除\n=====>2按书籍名字删除\n");
    scanf("%d",&nChoice);
    if(nChoice == 1)
	{
        printf("请你输入要删除的编号:");
        scanf("%s",findMess);
        p1 = Locate(head,findMess,"num"); //调用locate函数,并且返回指向要查找书籍编号的指针
        if(p1 != NULL)
		{
			p2 = head;
            while(p2->next != p1) //利用循环找到要指向删除的书籍编号的指针p1
            {
                 p2 = p2->next;
            }
			p2->next = p1->next; //将p1后面的一个指针连接在p2->next,从而删除了p1->data.num
            free(p1); //释放p1所占的内存
			printf("\n提示:该书籍已经成功删除!\n");
			shouldsave = 1; //用于后面的保存
		}
		else
		{
			nofind();
        }
	}
	else if(nChoice == 2)
	{
		printf("请你输入要删除的书籍名字:");
		scanf("%s",findMess);
		p1 = Locate(head,findMess,"name"); //调用locate函数,并且返回指向要查找书籍名字的指针
		if(p1 != NULL)
		{
			p2 = head;
			while(p2->next != p1) //利用循环找到要指向删除的书籍编号的指针p1
			{
				p2 = p2->next;
			}
			p2->next = p1->next; //将p1后面的一个指针连接在p2->next,从而删除了p1->data.name
			free(p1); //释放p1所占的内存
			printf("\n提示:该书籍已经成功删除!\n");
			shouldsave = 1;//用于后面的保存
		}
		else
		{
			nofind();
		}
	}
	else
	{
		wrong();
	}
}
///
void  Modify(struct book *head) //书籍信息的修改
{
	struct book *p;
	char findMess[MAXLEN];
	if(head == NULL)
	{
		printf("\n=====>提示:没有资料可以修改!\n");
	}
	printf("请你输入要修改的书籍编号:");
	scanf("%s",findMess);
	p = Locate(head,findMess,"num"); //调用locate函数,并且返回指向要修改书籍编号的指针
	if(p != NULL)
	{
		printf("请您输入新书籍编号:");
		scanf("%s",p->data.num); //注意:因为num是首地址,故不用加上'&'
		getchar(); 
		printf("请您输入新书名:");
		scanf("%s",p->data.name); //注意:因为name是首地址,故不用加上'&'
		getchar(); 
		printf("请您输入新的字数:");
		scanf("%ld",&p->data.page); //注意:因为page是长整型数值,必须用上'&'
		getchar(); 
		printf("请您输入新的出版日期:");
		scanf("%s",p->data.birth);
		getchar(); 
		printf("请您输入新的作者:");
		scanf("%s",p->data.auth);
		printf("请您输入新的出版社:");
		scanf("%s",p->data.place);
		printf("请您输入书籍的价格:");
		scanf("%d",&p->data.price);//注意:因为price是浮点型数值,必须用上'&'
		printf("\n=====>提示:资料修改成功!\n");
		shouldsave = 1;
	}
	else
	{
		nofind();
	}

}
///
void   Save(struct book *head) //保存书籍的相关信息
{
	FILE* fp;
	struct book *p;
	int flag = 1,nCount = 0;		
	p = head;
	if(p == NULL)
	{
		printf("您还没有要保持的内容!\n");
	}
	if((fp = fopen("D://tianmomo","wb")) == NULL)//为输出打开一个二进制的文件
	{
		printf("\n=====>提示:重新打开文件时发生错误!\n");
		exit(1); //注意:逃离函数,它在stdlib库里面
	}
	while(p != NULL) //判断p是否为空,以便进行循环
	{
		if(fwrite(p,sizeof(struct book),1,fp) == 1) //把p中的内容写到fp中
		{
			p = p->next; //p向后移动一
			nCount++;//用于统计保存书籍记录的数目
		}
		else
		{
			flag=0;
			break; //如果fwrite(p,sizeof(struct book),1,fp)返回值不是一,提前跳出循环
		}
	}
	if(flag == 1)
	{
		printf("\n=====>提示:文件保存成功.(有%d条记录已经保存).\n",nCount);
		shouldsave=0;
	}
	fclose(fp); //关闭fp文件
}
///
void   Display(struct book *head) //用于显示保存的书籍的相关信息
{           
	struct book *p;
	p = head; //将p连接在head的后面
	if(p == NULL)
	{
		printf("\n=====>提示:没有资料可以显示!\n");
	}
	printf("\t\t\t\t显示结果\n");
	printstart();
	printc();
	printf("\n");
	while(p != NULL) //利用循环把书籍的所有信息全部显示
	{
		printe(p);
		p = p->next; //p向后移动一
	}
	printstart();
	printf("\n");
}
///
int main(int argc, char* argv[])
{
 	FILE  *fp;
	int nCount=0,nChoice;
    char strSave,strBuild;
	struct book *head = NULL,*p1,*p2;
    printf("\n长江大学电子信息学院\t\t  图书管理系统\n");    
    p1 = (struct book *)malloc(sizeof(struct book));//申请一个动态存储区域,并且强制转化为struct book结构体型指针
	head = p1;
    if( (fp=fopen("D://tianmomo","rb")) == NULL)//为输入打开一个二进制文件,并且判断是否为空
    {
        printf("\n===>提示:文件还不存在,是否创建?(y/n)\n");
        scanf("%c",&strBuild);
        if((strBuild == 'y') || (strBuild == 'Y'))
        {
            fp=fopen("D://tianmomo","wb");//为输出打开一个二进制文件
			printf("您已经建立文件成功,请重新运行程序!\n");			
        }
        exit(1);//退出程序

    }
    while( feof(fp) == 0 )//判断fp文件中的内容是否已经真的读完。注意:如果真的读完,它的返回值为1;否则,返回值为0
    {
          p2 = (struct book *)malloc(sizeof(struct book));
          if(fread(p2,sizeof(struct book),1,fp) == 1)//把fp文件中的内容全部放在p2
          {
                p2->next = NULL;//p2后面指置空
                p1->next = p2;//将p2连接在p1的后面
                p1 = p2;//将p2赋给p1
                nCount ++;
          }
    }
    while(1)//无穷循环
    {
        menu();
        printf("请您选择:");
        scanf("%d",&nChoice);
        if(nChoice == 0)
        {
            if(shouldsave == 1) //用于内容保存
            {
                getchar();
                printf("\n====>提示:资料已改动,是否将改动保存到文件中?(y/n)\n");    
                scanf("%c",&strSave);
                if((strSave == 'y') || (strSave =='Y'))
                Save(head); //调用函数

            }
            printf("\n====>提示:已经退出系统!\n");
            break;

        }
        switch(nChoice)//选择调用函数
		{
			case 1:Add(head);break;
            case 2:Delete(head);break;
            case 3:Qur(head);break;
            case 4:Modify(head);break;
            case 5:Save(head);break;
            case 6:Display(head);break;
            default:wrong();break;
         }

    }
	fclose(fp);//关闭文件
    return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值