通过自定义函数和单链条进行图书管理,可以实现将图书信息存储到文件中,通过单链条进行读取修改等一系列操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int num;//书号
char bookname[20];//书名
char author[20];//作者
char isbn[20];//isbn
char chuban[20];//出版社
char chubanage[20];//出版时间
float price;//价格
struct node *next;
} NODE;
NODE *chuangjian(int n)//创建
{
int i;
NODE *h,*p,*q;
h=(NODE *)malloc(sizeof(NODE));
h->next=NULL;
p=h;
//第一个结点
for(i=1;i<=n;i++)
{
q=(NODE *)malloc(sizeof(NODE));
printf("输入第%d个图书编号:",i);
scanf("%d",&q->num);
printf("\n输入第%d个图书名称:",i);
scanf("%s",q->bookname);
printf("\n输入第%d个图书作者:",i);
scanf("%s",q->author);
printf("\n输入第%d个图书ISBN号:",i);
scanf("%s",q->isbn);
printf("\n输入第%d个图书出版单位:",i);
scanf("%s",q->chuban);
printf("\n输入第%d个图书出版时间:",i);
scanf("%s",&q->chubanage);
printf("\n输入第%d个图书单价:",i);
scanf("%f",&q->price);
p->next=q;
p=q;
}
p->next=NULL;
return h;
}
void baocun(NODE *h)//保存到文件
{
FILE *fp;
fp=fopen("D:/c语言文件/613文件.txt","w+");
if(fp==NULL)
{
printf("文件打开失败");
exit(0);
}
NODE *p;
p=h->next;
while(p!=NULL)
{
fprintf(fp,"%d %s %s %s %s %s %.2f\n",p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,p->price);
p=p->next;
}
fclose(fp);
}
void liulan(NODE *h)//浏览
{
NODE *p;
p=h->next;
while(p)
{
printf("编号:%d 书名:%s 作者:%s ISBN:%s 出版社:%s 出版时间:%s 价格:%.2f\n",p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,p->price);
p=p->next;
}
}
void numchaxun(NODE *h)//根据书号查询
{
int n,flag=1;
printf("输入查询的书号:");
scanf("%d",&n);
NODE *p;
p=h->next;
while(p)
{
if(p->num==n)
{
flag=1;
printf("编号:%d 书名:%s 作者:%s ISBN:%s 出版社:%s 出版时间:%s 价格:%.2f\n",p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,p->price);
break;
}
else
{
flag=0;
}
p=p->next;
}
if(flag==0)
{
printf("不存在");
}
}
void authorchaxun(NODE *h)//根据作者姓名查询
{
char author[20];
int flag=0;
printf("输入查询的作者:");
scanf("%s",author);
NODE *p;
p=h->next;
while(p)
{
if(strcmp(p->author,author)==0)
{
flag=1;
printf("编号:%d 书名:%s 作者:%s ISBN:%s 出版社:%s 出版时间:%s 价格:%.2f\n",p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,p->price);
}
p=p->next;
}
if(flag==0)
{
printf("不存在");
}
}
void bookchaxun(NODE *h)//根据书名查询
{
int flag=0;
char bookname[20];
printf("输入查询的书名:");
scanf("%s",bookname);
NODE *p,*q;
p=h->next;
q=p->next;
while(p)
{
if(strcmp(p->bookname,bookname)==0)
{
flag=1;
printf("编号:%d 书名:%s 作者:%s ISBN:%s 出版社:%s 出版时间:%s 价格:%.2f\n",p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,p->price);
}
p=p->next;
}
if(flag==0)
{
printf("不存在");
}
}
NODE *take()//文件读入链表中
{
NODE *h,*p,*q;
h=(NODE *)malloc(sizeof(NODE));
h->next=NULL;
p=q=h;
int num;
char bookname[20];
char author[20];
char isbn[20];
char chuban[20];
char chubanage[20];
float price;
FILE *fp;
fp=fopen("D:/c语言文件/613文件.txt","r");
if(fp==NULL)
{
printf("文件打开失败");
exit(0);
}
while(fscanf(fp,"%d %s %s %s %s %s %f",&num,bookname,author,isbn,chuban,chubanage,&price)!=EOF)
{
q=(NODE *)malloc(sizeof(NODE));
q->num=num;
strcpy(q->bookname,bookname);
strcpy(q->author,author);
strcpy(q->isbn,isbn);
strcpy(q->chuban,chuban);
strcpy(q->chubanage,chubanage);
q->price=price;
p->next=q;
p=q;
}
p->next=NULL;
return h;
}
void numrevise(NODE *h)//根据书号修改
{
int n;
printf("输入修改的书号:");
scanf("%d",&n);
NODE *p;
p=h->next;
while(p)
{
if(p->num==n)
{
printf("编号| 书名| 作者| ISBN| 出版社| 出版时间| 价格|\n");
scanf("%d %s %s %s %s %s %f",&p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,&p->price);
break;
}
p=p->next;
}
baocun(h);
}
void bookrevise(NODE *h)//根据书名修改
{
char bookname[20];
printf("输入修改的书名:");
scanf("%s",bookname);
NODE *p;
p=h->next;
while(p!=NULL)
{
if(strcmp(p->bookname,bookname)==0)
{
printf("编号| 书名| 作者| ISBN| 出版社| 出版时间| 价格|\n");
scanf("%d %s %s %s %s %s %f",&p->num,p->bookname,p->author,p->isbn,p->chuban,p->chubanage,&p->price);
break;
}
p=p->next;
}
baocun(h);
}
void numdel(NODE *h)//根据书号删除
{
int n;
printf("输入需要删除的书号:");
scanf("%d",&n);
NODE *pb,*pf;
pb=h->next;
while(pb!=NULL)
{
if(pb->num==n)
{
pf->next=pb->next;
break;
}
pb=pb->next;
pf=pf->next;
}
baocun(h);
}
void bookdel(NODE *h)//根据书名删除
{
char bookname[20];
printf("输入需要删除的书名:");
scanf("%s",bookname);
NODE *pb,*pf;
pb=h->next;
pf=h;
while(pb!=NULL)
{
if(strcmp(pb->bookname,bookname)==0)
{
pf->next=pb->next;
break;
}
pb=pb->next;
pf=pf->next;
}
baocun(h);
}
void numsort(NODE *h)//根据书号排序
{
NODE *tail,*p,*q;
tail = NULL;
while((h->next->next) != tail)
{
p = h;
q = h->next;
/*冒泡排序的本质将所找到的数都下沉,所以内层循环比外层小一个节点*/
while((q->next) != tail)
{
if((q->num) > (q->next->num)) //不停判断两个相邻的数并且交换顺序
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
q = p->next; //始终保存p,q是相邻的位置
}
q = q->next;
p = p->next;
}
/*最重要的一步,将tail不停向前移动*/
tail = q;
}
baocun(h);
}
main()
{
int x;
float a,b;
float q,w,e,r;
char ch;
do
{
printf("\t\t|--------------------------------|\n");
printf("\t\t| 欢迎使用图书信息管理系统 |\n");
printf("\t\t|--------------------------------|\n");
printf("\t\t|\t |\n");
printf("\t\t|\t 1---录入图书信息 |\n");
printf("\t\t|\t 2---浏览图书信息 |\n");
printf("\t\t|\t 3---图书信息查询 |\n");
printf("\t\t|\t 4---图书信息删除 |\n");
printf("\t\t|\t 5---图书信息修改 |\n");
printf("\t\t|\t 6---图书信息排序 |\n");
printf("\t\t|\t 7---退出信息系统 |\n");
printf("\t\t|--------------------------------|\n");
printf("请选择你的操作(1-7):") ;
scanf("%d",&x);
if(x==1)
{
int n,a;
printf("录入图书信息\n");
printf("请输入需要添加的图书数量:");
scanf("%d",&n);
if(n>1000)
{
printf("请重新输入");
}
NODE *h;
h=chuangjian(n);
printf("\n");
printf("是否保存?('0'不保存'1'保存)");
scanf("%d",&a);
if(a==1)
baocun(h);
}
if(x==2)
{
printf("浏览图书信息\n");
NODE *h;
h=take();
liulan(h);
}
if(x==3)
{
char ch1;
int b;
printf("\t\t|------------------------|\n");
printf("\t\t| 图书信息查询 |\n");
printf("\t\t|------------------------|\n");
printf("\t\t|\t |\n");
printf("\t\t| 1---图书编号查询 |\n");
printf("\t\t| 2---图书名称查询 |\n");
printf("\t\t| 3---图书作者查询 |\n");
printf("\t\t| 4---返回主菜单 |\n");
printf("\t\t|\t |\n");
printf("\t\t|------------------------|\n");
printf("请选择服务类型(1-4):") ;
scanf("%d",&b);
if(b==1)
{
NODE *h;
h=take();
numchaxun(h);
}
if(b==2)
{
NODE *h;
h=take();
bookchaxun(h);
}
if(b==3)
{
NODE *h;
h=take();
authorchaxun(h);
}
if(b==4)
;
}
if(x==4)
{
printf("\t\t|------------------------|\n");
printf("\t\t| 图书信息删除 |\n");
printf("\t\t|------------------------|\n");
printf("\t\t|\t |\n");
printf("\t\t| 1---图书编号删除 |\n");
printf("\t\t| 2---图书名称删除 |\n");
printf("\t\t| 3---返回主菜单 |\n");
printf("\t\t|\t |\n");
printf("\t\t|------------------------|\n");
printf("请选择服务类型(1-3):") ;
int b;
scanf("%d",&b);
if(b==1)
{
NODE *h;
h=take();
numdel(h);
}
if(b==2)
{
NODE *h;
h=take();
bookdel(h);
}
if(b==3)
{
;
}
}
if(x==5)
{
printf("\t\t|------------------------|\n");
printf("\t\t| 图书信息修改 |\n");
printf("\t\t|------------------------|\n");
printf("\t\t|\t |\n");
printf("\t\t| 1---图书编号修改 |\n");
printf("\t\t| 2---图书名称修改 |\n");
printf("\t\t| 3---返回主菜单 |\n");
printf("\t\t|\t |\n");
printf("\t\t|------------------------|\n");
printf("请选择服务类型(1-3):") ;
int b;
scanf("%d",&b);
if(b==1)
{
NODE *h;
h=take();
numrevise(h);
}
if(b==2)
{
NODE *h;
h=take();
bookrevise(h);
}
if(b==3)
;
}
if(x==6)
{
printf("\t\t|------------------------|\n");
printf("\t\t| 图书信息排序 |\n");
printf("\t\t|------------------------|\n");
printf("\t\t|\t |\n");
printf("\t\t| 1---图书编号排序 |\n");
printf("\t\t| 2---返回主菜单 |\n");
printf("\t\t|\t |\n");
printf("\t\t|------------------------|\n");
printf("请选择服务类型(1-2):") ;
int b;
scanf("%d",&b);
if(b==1)
{
NODE *h;
h=take();
numsort(h);
}
if(b==2)
;
}
if(x==7)
{
printf("退出");
break;
}
printf("\n");
printf("是否返回主菜单(输入Y或y继续,其他字符退出)?");
scanf(" %c",&ch);
system("cls");
}while(ch=='y'||ch=='Y');
}