//全部都是原创的,自己写的,历时近五天。
//运用了链表进行数据处理,然后用了文件存储数据。每次处理后又重新存进文件。
#include<>
#include<>
#include<string.h>
//这个student就是读者,也就是借书证的信息
struct student
{
char stu_num[10];
char stu_name[20];
char borrow_book[50];
struct student *next;
}*stu1,*stu2;
//这是书籍信息
struct book
{
char book_num[10];
char book_name[60];
char book_writer[20];
int book_price;
int here;
char borrow_book_man[20];
struct book *next;
}*book1,*book2;
//创建卡,也就是新的借书证,有了新读者
int creat_ka(struct student *head1,int n)
{
FILE *fp;
int i = 0;
struct student *tail,*pnew;
tail=head1;
system("cls");
for(i=0;i<n;i++)
{
tail=tail->next;
}
pnew=(struct student *)malloc(sizeof(struct student));
printf("输入新办的借书证号:");
scanf("%s",&pnew->stu_num);
printf("输入新的读者名:");
scanf("%s",pnew->stu_name);
strcpy(pnew->borrow_book,"无");
pnew->next=NULL;
tail->next=pnew;
fp=fopen("","a");
fwrite(pnew,sizeof(struct student),1,fp);
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return ++n;
}
//增加书
int add_book(struct book *head,int n)
{
FILE *fp;
struct book *tail,*pnew;
int i = 0;
tail=head;
system("cls");
for(i=0;i<n;i++)
{
tail=tail->next;
}
pnew=(struct book *)malloc(sizeof(struct book));
printf("新书的书号:\n");
scanf("%s",pnew->book_num);
printf("新书的书名:\n");
scanf("%s",pnew->book_name);
printf("新书作者:\n");
scanf("%s",pnew->book_writer);
printf("单价:\n");
scanf("%d",&pnew->book_price);
printf("请你设定书的状态(1在架/0不在):\n");
scanf("%d",&pnew->here);
pnew->next=NULL;
tail->next=pnew;
fp=fopen("","a");
fwrite(pnew,sizeof(struct book),1,fp);
fclose(fp);
printf("***************************[操作成功]*****************************\n\n");
return ++n;
}
/****************************
* 删除书
* head:头节点 n:书籍数
* 返回值为书籍数
****************************
*/
int del_book(struct book *head,int n)
{
FILE *fp;
struct book *tail,*pnew1,*pnew2;
char booknum[10];
int i,j,biaozhi_NoBook = 1;
tail=head->next;
pnew1=head;
system("cls");
printf("输入要删除的书的书号:\n");
scanf("%s",booknum);
//遍历链表,寻找是否存在该书籍
for(i=0;tail!=NULL;i++)
{
//若匹配
if(strcmp(tail->book_num,booknum)==0)
{
i++;
biaozhi_NoBook=0;//存在则标志位清零
printf("存在这个书号,第 %d 本\n", i);//第i本就是该书籍
break;
}
//若不匹配,则指向下一个
tail=tail->next;
}
//若遍历结束也不存在
if(biaozhi_NoBook)
{
printf("不存在这个书号\n");
return n;
}
//pnew1指向要删除的节点的前一个
for(j=0 ; j < i - 1 ; j++)
{
pnew1=pnew1->next;
}
//删除操作
pnew1->next=tail->next;
free(tail);
//重新写入文件(覆盖原有)
fp=fopen("","w");
//若删除一个后还有书籍
if(head->next != NULL)
{
pnew2=head->next;
do
{
fwrite(pnew2,sizeof(struct book),1,fp);
if((pnew2->next) != NULL)
{
pnew2=pnew2->next;
}
else
{
break;
}
}while((pnew2->next) != NULL);
fwrite(pnew2,sizeof(struct book),1,fp);//存最后一个
}
else
{
//若删除一个后没有书籍
fwrite(NULL,0,1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return --n;
}
/****************************
* 注销借书证
* head:头节点 n:卡数
* 返回值为卡数
****************************
*/
int del_ka(struct student *head,int n)
{
FILE *fp;
struct student *tail,*pnew1,*pnew2;
char ka_num[10];
int i,j,biaozhi_NoKa = 1;
tail=head->next;
pnew1=head;
system("cls");
printf("要删除的卡号:\n");
scanf("%s",ka_num);
//遍历链表,寻找是否存在该卡
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,ka_num)==0)
{
i++;
biaozhi_NoKa=0;
break;
}
tail=tail->next;
}
//若遍历结束也不存在
if(biaozhi_NoKa)
{
printf("不存在这个卡号\n");
return n;
}
//pnew1指向要删除的节点的前一个
for(j=0 ; j < i - 1; j++)
{
pnew1=pnew1->next;
}
//删除操作
pnew1->next=tail->next;
free(tail);
//重新写入文件(覆盖原有)
fp=fopen("","w");
//若删除一个后还有卡
if(head->next != NULL)
{
pnew2=head->next;
do
{
fwrite(pnew2,sizeof(struct student),1,fp);
if((pnew2->next) != NULL)
{
pnew2=pnew2->next;
}
else
{
break;
}
}while((pnew2->next) != NULL);
fwrite(pnew2,sizeof(struct student),1,fp);//存最后一个
}
else
{
fwrite(NULL,0,1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return --n;
}
//按照书名搜索书籍信息
int search_book_1(struct book *head)
{
int i;
char bookname[50];
struct book *tail;
tail=head->next;
system("cls");
printf("请输入要找的书的书名:");
scanf("%s",bookname);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_name,bookname)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("书籍库为空\n");
}
else
{
if(tail==0)
{
printf("书库中没有该书\n");
return 0;
}
}
system("cls");
printf("书号:%s 书名:%s 价格:%d元 状态:",tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在\n");
}
else
printf("在架\n");
printf("\n");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//按照作者名字搜索书籍信息
//可以搜索同一作者的所有书籍
int search_book_2(struct book *head)
{
int i=0,xunhuan;
char writername[50];
struct book *tail;
tail=head->next;
system("cls");
printf("请输入作者姓名:");
scanf("%s",writername);
for(xunhuan=0;tail!=0;xunhuan++)
{
for(;tail!=0;i++)
{
if(strcmp(tail->book_writer,writername)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("书籍库为空\n");
return 0;
}
else
{
if((tail==0)&&(xunhuan==0))
{
printf("书库中没有该作者的书\n");
return 0;
}
}
if(xunhuan==0)
{
system("cls");
}
printf("作者:%s 书号:%s 书名:%s 价格:%d元 状态:",tail->book_writer,tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在\n");
}
else
{
printf("在架\n");
}
tail=tail->next;
}
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//按照书的书号搜书
int search_book_3(struct book *head)
{
int i;
char booknum[10];
struct book *tail;
tail=head->next;
system("cls");
printf("请输入要找的书号:");
scanf("%s",booknum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_num,booknum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("书籍库为空");
return 0;
}
else
{
if(tail==0)
{
printf("书库中没有该书");
return 0;
}
}
system("cls");
printf("书号:%s 书名:%s 价格:%d元 状态:",tail->book_num,tail->book_name,tail->book_price);
if(tail->here==0)
{
printf("不在,借书人:%s \n",tail->borrow_book_man);
}
else
printf("在架\n");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//根据借书证号找读者信息
int search_ka_1(struct student *head)
{
int i;
char kanum[10];
struct student *tail;
tail=head->next;
system("cls");
printf("请输入要找的书证号:");
scanf("%s",kanum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,kanum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系统中借书证信息为空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系统中没有该借书证\n");
return 0;
}
}
system("cls");
printf("读者借书证号:%s 读者姓名:%s 所借书籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//根据读者姓名找读者信息
int search_ka_2(struct student *head)
{
int i;
char studentname[50];
struct student *tail;
tail=head->next;
system("cls");
printf("请输入读者姓名:");
scanf("%s",studentname);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_name,studentname)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系统中借书证信息为空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系统中没有该借书证\n");
return 0;
}
}
system("cls");
printf("读者借书证号:%s 读者姓名:%s 所借书籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//根据所借书籍找读者信息
int search_ka_3(struct student *head)
{
int i;
char borrow_book_name[50];
struct student *tail;
tail=head->next;
system("cls");
printf("请输入书籍名:");
scanf("%s",borrow_book_name);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->borrow_book,borrow_book_name)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系统中借书证信息为空\n");
return 0;
}
else
{
if(tail==0)
{
printf("系统中没有该借书证\n");
return 0;
}
}
system("cls");
printf("读者借书证号:%s 读者姓名:%s 所借书籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
printf("\n***************************[操作成功]*****************************\n\n");
return 0;
}
//搜借书证的开始的程序
int search_ka(struct student *head)
{
int k;
system("cls");
printf("1 按借书证号查找\n2 按读者名字查找\n3 按所借书籍查找\n0 返回上一层\n");
shuru_2:
printf("请选择功能0-3:");
scanf("%d",&k);
printf("**********************************\n");
switch(k)
{
case 1:search_ka_1(head);break;
case 2:search_ka_2(head);break;
case 3:search_ka_3(head);break;
case 0:
system("cls");
printf("****************[操作成功]********************\n\n");
return 0;
default:
printf("输入错误,");goto shuru_2;
}
return 0;
}
//搜书的开始程序
int search_book(struct book *head)
{
int k;
system("cls");
printf("1 按书名查找\n2 按作者查找\n3 按书号查找\n0 返回上一层\n");
shuru_2:
printf("请选择功能0-3:");
scanf("%d",&k);
printf("**********************************\n");
switch(k)
{
case 1:search_book_1(head);break;
case 2:search_book_2(head);break;
case 3:search_book_3(head);break;
case 0:
system("cls");
printf("****************[操作成功]********************\n\n");
return 0;
default:
printf("输入错误,");goto shuru_2;
}
return 0;
}
//借书
int borrow_book(struct student *head1,struct book *head2)
{
FILE *fp,*fp2;
struct student *tail,*pnew;
struct book *tail_2,*pnew_2;
int i,j, biaozhi_book = 0;
char student_num[10];
char borrow_num[10];
system("cls");
tail=head1->next;
tail_2=head2->next;
pnew=head1->next;
pnew_2=head2->next;
printf("请输入借书证号:");
scanf("%s",student_num);
for(i=0;tail!=0;i++)//根据借书证号找到该学生
{
if(strcmp(tail->stu_num,student_num)==0)
{
i++;
biaozhi_book = 0;//标志位清零,书籍信息匹配
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系统中没有借书证信息,为空。\n");
return 0;
}
if(biaozhi_book)
{
printf("系统中没有该书籍信息。\n");
return 0;
}
if(strcmp(tail->borrow_book,"无"))//如果借了书
{
printf("该读者仍未还书,无法借书。\n");
return 0;
}
printf("请输入书号:");
scanf("%s",borrow_num);
for(j=0;tail_2!=0;j++)//根据书号找到该书籍
{
if(strcmp(tail_2->book_num,borrow_num)==0)
{
j++;
break;
}
tail_2=tail_2->next;
}
if(j==0)
{
printf("书库中不存在该书。\n");
return 0;
}
if(tail_2->here==0)
{
printf("该书不在架,已经外借\n");
return 0;
}
strcpy(tail->borrow_book,tail_2->book_name);
tail_2->here=0;
strcpy(tail_2->borrow_book_man,tail->stu_name);
fp=fopen("","w");
while(pnew->next)
{
fwrite(pnew,sizeof(struct student),1,fp);
pnew=pnew->next;
}
fwrite(pnew,sizeof(struct student),1,fp);//存最后一个学生信息
fclose(fp);
fp2=fopen("","w");
while(pnew_2->next != NULL)
{
fwrite(pnew_2,sizeof(struct book),1,fp2);
pnew_2=pnew_2->next;
}
fwrite(pnew_2,sizeof(struct book),1,fp2);//存最后一本书籍信息
fclose(fp2);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//还书
int back_book(struct student *head,struct book *head2)
{
FILE *fp,*fp2;
struct student *tail,*pnew;
struct book *tail_2,*pnew_2;
int i;
int chose;
char kanum[10];
system("cls");
tail=head->next;
tail_2=head2->next;
pnew=head->next;
pnew_2=head2->next;
printf("请输入卡号:");
scanf("%s",kanum);
for(i=0;tail!=0;i++)//根据卡号找到该学生
{
if(strcmp(tail->stu_num,kanum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("系统中没有该借书证号。\n");
return 0;
}
if(strcmp(tail->borrow_book,"无"))//如果借了书
{
fanhui_1:
printf("借的书是:%s\n",tail->borrow_book);
printf("确认归还该书?\n(1 确认 0 取消)");
scanf("%d",&chose);
switch(chose)
{
case 1:
while(strcmp(tail->borrow_book,tail_2->book_name))//根据学生借的书找到该书
{
tail_2=tail_2->next;
}
tail_2->here=1;//更改书的状态
strcpy(tail_2->borrow_book_man,"");
strcpy(tail->borrow_book,"");
break;
case 0:return 0;
default:printf("请输入1或0\n");goto fanhui_1;
}
}
else
{
printf("该读者还没借书\n");
return 0;
}
fp=fopen("","w");
fp2=fopen("","w");
while(pnew->next)
{
fwrite(pnew,sizeof(struct student),1,fp);
pnew=pnew->next;
}
while(pnew_2)
{
fwrite(pnew_2,sizeof(struct book),1,fp2);
pnew_2=pnew_2->next;
}
fclose(fp);
fclose(fp2);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//显示书库中的所有书籍
void printf_book(struct book *head)
{
struct book *tail;//*pnew;
int i;
tail=head->next;
system("cls");
for(i=1;tail!=0;i++)
{
printf("第%d本书是:\n",i);
printf("书号:%s 书名:%s 作者:%s 单价:%d元 状态:",tail->book_num,tail->book_name,tail->book_writer,tail->book_price) ;
if(tail->here==0)
printf("不在,借书人为:%s",tail->borrow_book_man);
else
printf("在");
printf("\n");
tail=tail->next;
}
if(i==1)
{
printf("*****************书库为空*******************\n");
}
printf("***************************[操作成功]*****************************\n\n");
}
//显示所有读者信息
void printf_ka(struct student *head)
{
struct student *tail;//*pnew;
int i;
tail=head->next;
system("cls");
for(i=1;tail!=0;i++)
{
printf("第%d个读者是:\n",i);
printf("借书证号:%s 姓名:%s 所借书籍:%s\n",tail->stu_num,tail->stu_name,tail->borrow_book);
tail=tail->next;
}
if(i==1)
{
printf("*****************借书证存储系统为空*******************\n");
}
printf("***************************[操作成功]*****************************\n\n");
}
//修改书籍信息的程序
int change_1(struct book *head)
{
FILE *fp;
struct book *tail,*pnew;
struct book *new_change;
char booknum[20];
int i,chose;
system("cls");
new_change=(struct book*)malloc(sizeof(struct book));
tail=head->next;
pnew=head->next;
printf("请输入要修改的书的书号:");
scanf("%s",booknum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->book_num,booknum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("书库为空\n");
return 0;
}
if(tail==0)
{
printf("书库中不存在该书\n");
return 0;
}
printf("书籍原信息为:\n");
printf("书号:%s 书名:%s 作者:%s 单价:%d元 状态:",tail->book_num,tail->book_name,tail->book_writer,tail->book_price) ;
if(tail->here==0)
{
printf("不在,借书人为:%s",tail->borrow_book_man);
}
else
{
printf("在");
}
shuru_9:
printf("\n1 修改书号\n2 修改书名\n3 修改作者\n4 修改单价\n5 修改状态\n6 修改借书人\n0 取消,返回上一级\n");
scanf("%d",&chose);
switch(chose)
{
case 1:
printf("请输入新书号:");
scanf("%s",new_change->book_num);
strcpy(tail->book_num,new_change->book_num);
break;
case 2:
printf("请输入新书名:");
scanf("%s",new_change->book_name);
strcpy(tail->book_name,new_change->book_name);
break;
case 3:
printf("请输入新作者:");
scanf("%s",new_change->book_writer);
strcpy(tail->book_writer,new_change->book_writer);
break;
case 4:
printf("请输入新单价:");
scanf("%d",&new_change->book_price);
tail->book_price=new_change->book_price;
break;
case 5:
printf("请输入新状态(1 在架;2 不在):");
scanf("%d",&new_change->here);
tail->here=new_change->here;
break;
case 6:
printf("请输入新的借书人:");
scanf("%s",new_change->borrow_book_man);
strcpy(tail->borrow_book_man,new_change->borrow_book_man);
break;
case 0:return 0;
default:printf("输入错误,请重新输入。\n");goto shuru_9;
}
free(new_change);
fp=fopen("","w");
while(pnew)
{
fwrite(pnew,sizeof(struct book),1,fp);
pnew=pnew->next;
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
//修改借书证信息的程序
int change_2(struct student *head)
{
FILE *fp;
struct student *tail,*pnew;
struct student *new_change;
char studentnum[10];
int i,chose;
system("cls");
tail=head->next;
pnew=head->next;
printf("请输入要修改的借书证的证号:");
scanf("%s",studentnum);
for(i=0;tail!=0;i++)
{
if(strcmp(tail->stu_num,studentnum)==0)
{
i++;
break;
}
tail=tail->next;
}
if(i==0)
{
printf("借书证信息为空\n");
return 0;
}
if(tail->next==0)
{
printf("系统中不存在该借书证\n");
return 0;
}
printf("读者原信息为:\n");
printf("借书证号:%s 姓名:%s 所借书籍:%s",tail->stu_num,tail->stu_name,tail->borrow_book);
shuru_8:
printf("\n1 修改借书证号\n2 修改读者姓名\n3 修改所借书籍\n0 取消\n");
scanf("%d",&chose);
new_change=(struct student *)malloc(sizeof(struct student));
switch(chose)
{
case 1:printf("请输入新借书证号:");scanf("%s",new_change->stu_num);strcpy(tail->stu_num,new_change->stu_num);break;
case 2:printf("请输入新读者姓名:");scanf("%s",new_change->stu_name);strcpy(tail->stu_num,new_change->stu_num);break;
case 3:printf("请输入新所借书籍:");scanf("%s",new_change->borrow_book);strcpy(tail->borrow_book,new_change->borrow_book);break;
case 0:return 0;
default:printf("输入错误,请重新输入。\n");goto shuru_8;
}
free(new_change);
fp=fopen("","w");
while(pnew)
{
fwrite(pnew,sizeof(struct student),1,fp);
}
fclose(fp);
system("cls");
printf("***************************[操作成功]*****************************\n\n");
return 0;
}
/*
*主函数
*链表的头节点不存数据,最后一个节点的next为NULL
*/
int main()
{
int a[5],i=0,k=0;
//头节点
struct student *s1;
struct book *b1;
//指向文件的指针
FILE *fp_ka,*fp_book;
//申请空间,stu1和book1指向各自申请的空间
stu1 = (struct student *)malloc(sizeof (struct student));
book1 = (struct book *)malloc(sizeof (struct book));
stu1->next = NULL;
book1->next = NULL;
//头节点s1与b1指向头节点的空间
s1 = stu1;
b1 = book1;
//再次申请空间,stu2和book2指向各自申请的空间
stu2=(struct student *)malloc(sizeof(struct student));
book2=(struct book *)malloc(sizeof(struct book));
//打开文件
fp_ka = fopen("","a+");
fp_book = fopen("","a+");
//读取文件,获取已存储的卡数(也就是学生数)
while(fread(stu2,sizeof(struct student),1,fp_ka)!=0)
{
//stu1与stu2为临时变量
stu2->next=NULL;
stu1->next=stu2;
stu1=stu2;
stu2=(struct student *)malloc(sizeof(struct student));//为下一次存储准备的新空间
i++;
}
fclose(fp_ka);
//读取文件,获取已存储的书籍数
while(fread(book2,sizeof(struct book),1,fp_book)!=0)
{
//book1与book2为临时变量
book2->next=NULL;
book1->next=book2;
book1=book2;
book2=(struct book *)malloc(sizeof(struct book));
k++;
}
fclose(fp_book);
printf("\t\tGDOU通信工程1154班\t【陈恒钊同学】独立创作\n");
printf("\t\t目前系统内已存储了:%d本书%d个借书证\n",k,i);
shuru_3:
printf("\t********************第1级页面_超级图书管理系统******************\n");
printf("【1】 借书\n【2】 还书\n【3】 图书查询\n【4】 借书证信息查询\n【5】 系统管理\n【0】 关闭系统\n");
printf("请选择功能0-5:");
scanf("%d",&a[0]);
switch(a[0])
{
case 0:printf("\t**************[成功关闭系统]*****************");return 0;
case 1:
printf("\t***************第2级页面_借书系统**************\n");
borrow_book(s1,b1);goto shuru_3;
case 2:
printf("\t***************第2级页面_还书系统**************\n");
back_book(s1,b1);goto shuru_3;
case 3:
printf("\t***************第2级页面_图书查询**************\n");
search_book(b1);
goto shuru_3;
case 4:
printf("\t***************第2级页面_借书证信息查询**************\n");
search_ka(s1);goto shuru_3;
case 5:
second_1:
system("cls");
second_11:
printf("\t***************第2级页面_系统管理**************\n");
printf("【1】 新办借书证\n【2】 注销借书证\n【3】 新增图书\n【4】 删除图书\n【5】 修改旧书信息\n【6】 显示书库中所有书籍\n【7】 显示所有借书证信息\n【0】 返回上一级\n");
printf("请选择功能0-5:");
scanf("%d",&a[1]);
switch(a[1])
{
case 1:i=creat_ka(s1,i);goto second_11;
case 2:i=del_ka(s1,i);goto second_11;
case 3:k=add_book(b1,k);goto second_11;
case 4:k=del_book(b1,k);goto second_11;
case 5:change_1(b1);goto second_11;
case 6:printf_book(b1);goto second_11;
case 7:printf_ka(s1);goto second_11;
case 8:change_2(s1);goto second_1;
case 0:system("cls");goto shuru_3;
default:goto second_1;
}
default:printf("***********输入错误***************\n");goto shuru_3;
}
}