图书管理系统源代码.docx
《图书管理系统源代码.docx》由会员分享,可在线阅读,更多相关《图书管理系统源代码.docx(37页珍藏版)》请在冰豆网上搜索。
图书管理系统源代码
图书管理系统源程序
#include
#include
#include
structBook{
intnum;//图书的编号
charname[80];//图书的名字
charauthor[80];//图书作者
doubleprice;//图书价格
inttime;//出版日期
charpub[80];//图书的出版社
charkind[80];//图书的类别
structBook*next;
};
structBook*Create();//创建图书信息
structBook*addlist(structBook*head,structBook*book);//增加新图书信息
intyanzheng(structBook*head,intm);//验证编号是否存在
voidmenu();//菜单函数
structBook*Delete(structBook*head);//删除图书
voidtraverse(structBook*head);//浏览图书
voidfind(structBook*head);//查找图书
voidfind_num(structBook*head);//按图书编号查找
voidfind_name(structBook*head);//按书名查找
voidfind_kind(structBook*head);//按图书类别查找
voidfind_author(structBook*head);//按图书作者名查找
voidfind_time(structBook*head);//按图书出版时间查找
voidchange(structBook*head);//修改图书信息
structBook*sort(structBook*head);//为图书排序
structBook*sort_num(structBook*head);
voidorder_num(structBook*temp_head);//按图书编号排序。
。
。
voidorder_name(structBook*temp_head);//按图书名排序
voidorder_time(structBook*temp_head);//按图书出版时间排序
voidfprint(structBook*head);//将链表写入文件
structBook*load();//从文件中读取信息并建成链表
intmain(void)
{
menu();//调用菜单函数
return0;
}
voidmenu()
{
structBook*head=NULL,*p;
intchoice;
intnum,time;
doubleprice;
charname[80],author[80];
charkind[80],pub[80];
intsize=sizeof(structBook);
charchoice1,a;
inty=1,n,c,c1=123;
charb[10],b1[10]="zgy";
charanswer;
while(y){
system("cls");
printf("\n");
printf("\t\t班级:
软件工程1102\n");
printf("\t\t学号:
201116040221\n");
printf("\t\t姓名:
张国宇\n");
there2:
printf("\n");
printf("\t\t--------------------------------\n");
printf("\t\t|**********欢迎光临***********|\n");
printf("\t\t|******图书信息管理系统*******|\n");
printf("\t\t|==========1-用户登录===========|\n");
printf("\t\t|==========0-退出系统===========|\n");
printf("\t\t--------------------------------\n");
printf("请输入您的选择:
");
scanf("%d",&n);
printf("\n");
getchar();
switch(n){
case0:
y=0;
break;
case1:
there3:
printf("请输入您的用户名(zgy):
");
gets(b);
printf("\n");
printf("请输入您的密码(123):
");
scanf("%d",&c);
getchar();
printf("\n");
if(strcmp(b,b1)!
=0||c!
=c1){
printf("验证失败,请重新输入!
\a\n");
system("cls");
gotothere3;
}
else{
printf("验证通过!
请按Enter键进入!
\a\n");
system("cls");
do{
there1:
printf("\t\t-------欢迎使用图书管理系统---------\n");
printf("\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[******5.图书信息排序*******]\n");
printf("\t\t[******6.图书信息修改*******]\n");
printf("\t\t[******7.删除图书信息*******]\n");
printf("\t\t[******0.退出***************]\n");
printf("请输入你的选择:
\n");
scanf("%d",&choice);
switch(choice){
case1:
printf("此操作将清空文件内所有信息,是否确认?
确认'Y'返回'N'。
\n");//若新建,则文件夹里的信息会被清空,故需要这步判断。
。
。
scanf("%s",&choice1);
getchar();
if(choice1=='Y'||choice1=='y'){
head=Create();
printf("添加成功!
\n");
printf("是否将新信息保存到文件?
(y/n)\n");
scanf("%c",&a);
getchar();
switch(a){
case'n':
break;
case'y':
fprint(head);
traverse(head);
printf("保存成功!
\n");
getchar();
break;
}
system("PAUSE");
system("cls");
}
else{
system("PAUSE");
system("cls");
gotothere1;
}
break;
case2:
head=load();//读入文件的内容,下同
there:
do{
printf("请输入新图书编号:
");
scanf("%d",&num);
n=yanzheng(head,num);
if(n==0)
break;
else
printf("您输入的编号已存在,请重新输入!
\n");
}while
(1);
getchar();
printf("请输入新图书书名:
");
gets(name);
printf("请输入新图书作者:
");
gets(author);
printf("请输入新图书的出版社名字:
");
gets(pub);
printf("请输入新图书种类:
");
gets(kind);
printf("请输入新图书的价格:
");
scanf("%lf",&price);
getchar();
printf("请输入新图书的出版日期:
");
scanf("%d",&time);
getchar();
printf("\n");
p=(structBook*)malloc(size);
p->num=num;
strcpy(p->name,name);
strcpy(p->author,author);
p->price=price;
p->time=time;
strcpy(p->pub,pub);
strcpy(p->kind,kind);
p->next=NULL;
head=addlist(head,p);
printf("请选择是否继续添加:
Y继续;N结束;\n");
scanf("%c",&answer);
if(answer=='Y'||answer=='y')
gotothere;
printf("添加成功!
\n");
head=sort_num(head);//调入函数,使得图书默认按照图书编号排序
fprint(head);//写入文件
traverse(head);
system("PAUSE");
system("cls");
break;
case3:
head=load();//若不输入,直接从文件夹里录入,则需要传递头指针。
。
。
下同。
。
。
traverse(head);//遍历图书信息
system("PAUSE");
system("cls");
break;
case4:
head=load();
find(head);
system("PAUSE");
system("cls");
break;
case5:
head=load();
head=sort(head);//为图书排序
printf("排序成功!
\n");
fprint(head);
traverse(head);
system("PAUSE");
system("cls");
break;
case6:
head=load();
change(head);//修改图书信息
system("PAUSE");
system("cls");
break;
case7:
head=load();
head=Delete(head);//删除图书
fprint(head);
system("PAUSE");
system("cls");
break;
case0:
break;
default:
printf("无效选项!
请重新输入。
\n");
gotothere1;
system("PAUSE");
break;
}
}while(choice!
=0);
}
break;
default:
printf("无效选项!
请重新输入。
\n");
system("PAUSE");
system("cls");
gotothere2;
system("PAUSE");
break;
}
break;
}
}
//创建图书信息
structBook*Create()
{
structBook*head,*p,*tail;
intnum,time;
doubleprice;
charname[80],author[80];
charkind[80],pub[80];
intsize=sizeof(structBook);
head=tail=NULL;
printf("请输入图书编号:
");
scanf("%d",&num);
getchar();
while(num!
=0){
printf("请输入图书书名:
");
gets(name);
printf("请输入图书作者:
");
gets(author);
printf("请输入图书的出版社名字:
");
gets(pub);
printf("请输入图书种类:
");
gets(kind);
printf("请输入图书的价格:
");
scanf("%lf",&price);
getchar();
printf("请输入图书的出版日期:
");
scanf("%d",&time);
getchar();
printf("\n");
p=(structBook*)malloc(size);
p->num=num;
strcpy(p->name,name);
strcpy(p->author,author);
p->price=price;
p->time=time;
strcpy(p->pub,pub);
strcpy(p->kind,kind);
p->next=NULL;
if(head==NULL)
{
head=tail=p;
}
else
{
tail->next=p;
tail=p;
}
printf("请输入图书编号(按0即可结束输入):
");
scanf("%d",&num);
getchar();
}
returnhead;
}
//添加新图书信息
structBook*addlist(structBook*head,structBook*book)//默认添加到图书的最后。
{
structBook*ptr,*ptr1;
ptr1=head;
ptr=book;
if(head==NULL){
head=ptr;
head->next=NULL;
}
else{
while(ptr1->next!
=NULL){
ptr1=ptr1->next;
}
ptr1->next=ptr;
ptr->next=NULL;
}
returnhead;
}
//验证添加的图书编号是否已存在
intyanzheng(structBook*head,intm)
{
structBook*p;
p=head;
while(p!
=NULL){
if(p->num==m)
break;
p=p->next;
}
if(p==NULL)
return0;
else
return1;
}
//浏览图书信息
voidtraverse(structBook*head)
{
structBook*ptr;
if(head==NULL){
printf("\n无图书信息!
\n");
getchar();
return;
}
else{
printf("图书信息列表如下\n");
printf("\t================================================================================\n");
printf("\t|编号|图书名|作者名|出版社|类别|出版时间|价格|\n");
for(ptr=head;ptr!
=NULL;ptr=ptr->next){
printf("\t|%-6d|%-14s|%-10s|%-14s|%-8s|%-8d|%-7.2f|\n",ptr->num,ptr->name,ptr->author,ptr->pub,ptr->kind,ptr->time,ptr->price);
}
printf("\t================================================================================\n");
}
}
//删除图书信息
structBook*Delete(structBook*head)
{
structBook*p1,*p2;
intnum;
if(head==NULL)
{
printf("无图书信息!
请先录入信息!
\n");
returnhead;
}
else{
printf("请输入你要删的书号:
");
scanf("%d",&num);
getchar();
if(head->num==num){//删除头指针数据
p1=head;
head=head->next;
free(p1);
printf("删除成功!
\n");
returnhead;
}
else{
p1=head;
p2=head->next;
while(p2!
=NULL){
if(p2->num==num){
p1->next=p2->next;
free(p2);
printf("删除成功!
\n");
returnhead;
}
else{
p1=p2;
p2=p1->next;
}
if(p2==NULL){
printf("你输入的图书不存在!
请重新输入。
\n");
break;
}
}
}
}
returnhead;
}
//将新链表写入文件中
voidfprint(structBook*head)
{
FILE*fp;
structBook*p1;
if((fp=fopen("图书信息.txt","w"))==NULL){
printf("文件图书信息打开失败!
\n");
exit(0);
}
for(p1=head;p1;p1=p1->next){
fprintf(fp,"%d%s%s%s%s%d%lf\n",p1->num,p1->name,p1->author,p1->pub,p1->kind,p1->time,p1->price);
}
fclose(fp);
}
//从文件中读取图书信息
structBook*load()
{
FILE*fp;
structBook*head,*tail,*p1;
head=tail=NULL;
if((fp=fopen("图书信息.txt","r"))==NULL){//判断是否为空,如果为空就关闭了直接返回null,如果不是空文本那就把这个关了下面重新打开文本
printf("文件图书信息打开失败!
\n");
exit(0);
}
intisend=fgetc(fp);//用于判断是否是空文本
fclose(fp);
if(isend!
=-1){
if((fp=fopen("图书信息.txt","r"))==NULL){
printf("文件图书信息打开失败!
\n");
exit(0);
}
while(!
feof(fp)){
p1=(structBook*)malloc(sizeof(structBook));
fscanf(fp,"%d%s%s%s%s%d%lf\n",&p1->num,p1->name,p1->author,p1->pub,p1->kind,&p1->time,&p1->price);
if(head==NULL)
head=p1;
else
tail->next=p1;
tail=p1;
}
tail->next=NULL;