这是图书管理系统的源代码,大一的课程设计,只供参考哦
#include
#include
#include
#include
struct book
{
char name[20];
char year[10];
char zz[20];
int cs;
int jg;
struct book *next;
};
struct book * lianbiao() //产生头结点
{
struct book *head;
head=(struct book *)malloc(sizeof(struct book));
head->next=NULL;
return head;
}
int tianjia(struct book *head,int b) //添加一本新书
{
struct book *ptemp=head,*pnew;
pnew=(struct book *)malloc(sizeof(struct book));
fflush(stdin);
printf("请输入书籍名称:");
gets(pnew->name);
printf("请输入出版日期:");
gets(pnew->year);
printf("请输入书籍作者:");
gets(pnew->zz);
printf("请输入书籍册数:");
scanf("%d",&pnew->cs);
printf("请输入书籍价格:");
scanf("%d",&pnew->jg);
fflush(stdin);
pnew->next=NULL;
while(ptemp->next!=NULL)
{
ptemp=ptemp->next;
}
ptemp->next=pnew;
b++;
return b;
}
int huifu(struct book *head) //将文件中数据恢复至内存
{
FILE * fp;
int i=0,j=0;
struct book *ptemp=head,*pnew;
fp=fopen("a.mr","rt");
while(1)
{
pnew=(struct book *)malloc(sizeof(struct book));
i=fscanf(fp," %s %s %s %d %d",pnew->name,pnew->year,pnew->zz,&pnew->cs,&pnew->jg);
if(i==EOF)break;
pnew->next=NULL;
ptemp->next=pnew;
ptemp=ptemp->next;
j++;
}
free(pnew);
fclose(fp);
return j;
}
void tuichu(struct book * head) //将内存数据保存至文件
{
FILE * fp;
struct book * ptemp=head;
fp=fopen("a.mr","w");
while(ptemp->next!=NULL)
{
ptemp=ptemp->next;
fprintf(fp," %s %s %s %d %d",ptemp->name,ptemp->year,ptemp->zz,ptemp->cs,ptemp->jg);
}
fclose(fp);
}
void fshuming(struct book *head) //按书名查找
{
struct book *ptemp=head;
char a[20];
int b=1;
printf("请输入书籍名称