p=(structfi_info*)malloc(sizeof(structfi_info));//P为动作指针,申请内存空间
fscanf(fp,"%s",p->name);
fscanf(fp,"%s",p->writer);
fscanf(fp,"%s",p->shuhao);
fscanf(fp,"%s",&p->fenlei);
fscanf(fp,"%s",p->chuban);
fscanf(fp,"%d",&p->chubantime);
fscanf(fp,"%.2f",&p->price);
fscanf(fp,"%d",&p->type);
if(p->type==1)
{
if(head==NULL){
head=p;
p->next=NULL;
}
else{
p->next=head;
head=p;
}
}
else{
if(head_==NULL){
head_=p;
p->next=NULL;
}
else{
p->next=head_;
head_=p;
}
}
fclose(fp);
return0;
}
fclose(fp);
return0;
}
intchuangjian()//创建图书信息
{
structfi_info*p=NULL;
p=(structfi_info*)malloc(sizeof(structfi_info));
printf("请输入书名:
");
scanf("%s",p->name);
printf("请输入作者名:
");
scanf("%s",p->writer);
printf("请输入书号:
");
scanf("%s",p->shuhao);
printf("请输入分类:
");
scanf("%s",&p->fenlei);
printf("请输入出版社:
");
scanf("%s",p->chuban);
printf("请输入出版时间:
");
scanf("%d",&p->chubantime);
printf("请输入价格:
");
scanf("%f",&p->price);
p->type=news;
Book_num+=1;
p->next=head;
head=p;
return0;
}
voidchazhao()//查找图书信息
{
structfi_info*p=NULL;
charname[10],writer[21];
intprice;
intb=1,i;
printf("按书名查找请按1.\n按作者名查找请按2.\n按组合查询请按3.\n");
scanf("%d",&i);
if(i==1)
{
printf("请输入要查找的书名:
");
scanf("%s",name);
for(p=head;p!
=NULL;p=p->next)//一个链表指针p的遍历,直到结尾p==NULL
{
if(strcmp(name,p->name)==0)//字符串的比较函数,若前后相等则等于0
{b=0;//若存在,则b=0
printf("找到要查找的图书信息:
\n");
printf("图书名称%s\n",p->name);
printf("图书作者%s\n",p->writer);
printf("图书书号%s\n",p->shuhao);
printf("图书分类%s\n",p->fenlei);
printf("出版社%s\n",p->chuban);
printf("出版时间%d\n",p->chubantime);
printf("图书价格%.2f\n",p->price);
}
}
if(head==NULL)printf("<查无此书>\n");
if(b)printf("<查找的图书信息不存在!
>\n");//若b=1,则不存在
}
elseif(i==2)
{
printf("请输入要查找的作者名:
");
scanf("%s",writer);
for(p=head;p!
=NULL;p=p->next)//一个链表指针p的遍历,直到结尾p==NULL
{
if(strcmp(writer,p->writer)==0)//字符串的比较函数,若前后相等则等于0
{b=0;//若存在,则b=0
printf("找到要查找的图书信息:
\n");
printf("图书名称%s\n",p->name);
printf("图书作者%s\n",p->writer);
printf("图书书号%s\n",p->shuhao);
printf("图书分类%s\n",p->fenlei);
printf("出版社%s\n",p->chuban);
printf("出版时间%d\n",p->chubantime);
printf("图书价格%.2f\n",p->price);
}
}
if(head==NULL)printf("<查无此书>\n");
if(b)printf("<查找的图书信息不存在!
>\n");//若b=1,则不存在
}
elseif(i==3)
{
printf("请输入要查找的书名价格\n:
");
scanf("%s%d",name,&price);
for(p=head;p!
=NULL;p=p->next)//一个链表指针p的遍历,直到结尾p==NULL
{
if(strcmp(name,p->name)==0&&price==p->price)//字符串的比较函数,若前后相等则等于0
{b=0;//若存在,则b=0
printf("找到要查找的图书信息:
\n");
printf("图书名称%s\n",p->name);
printf("图书作者%s\n",p->writer);
printf("图书书号%s\n",p->shuhao);
printf("图书分类%s\n",p->fenlei);
printf("出版社%s\n",p->chuban);
printf("出版时间%d\n",p->chubantime);
printf("图书价格%.2f\n",p->price);
}
}
if(head==NULL)printf("查无此书\n");
if(b)printf("<查找的图书信息不存在!
>\n");//若b=1,则不存在
}elseprintf("<没有该项功能,请重新选择:
>\n");
system("PAUSE");//暂停
}
voidshanchu()//删除图书信息
{
structfi_info*p=NULL;//定义两个结构体指针
structfi_info*q=NULL;
charname[10];
intb=1;
inta;
printf("请输入要删除的书名:
\n");
scanf("%s",name);
for(p=head,q=head;p!
=NULL;p=p->next)
{
if(strcmp(name,p->name)==0)
{
b=0;
printf("确定删除?
0(是)or1(否)\n");
scanf("%d",&a);
if(a==0)
{
if(p==head)
head=head->next;
else
q->next=p->next;
printf("删除成功!
\n");
}
}
elseq=p;
}
if(b)printf("<提示:
查无此书!
.\n");
system("PAUSE");
}
voidxiugai()//修改图书信息
{
structfi_info*p=NULL;
inta,b=1;
charname[10];
printf("请输入书名:
\n\n");
scanf("%s",name);
for(p=head;p!
=NULL;p=p->next)
{
if(strcmp(p->name,name)==0){
b=0;
printf("图书名称%s\n",p->name);
printf("图书作者%s\n",p->writer);
printf("图书书号%s\n",p->shuhao);
printf("图书分类%s\n",p->fenlei);
printf("出版社%s\n",p->chuban);
printf("出版时间%d\n",p->chubantime);
printf("图书价格%.2f\n",p->price);
printf("<是否确定修改?
0(是)or1(否)>\n");
scanf("%d",&a);
if(a==0){
printf("**********请重新输入信息*********\n");
printf("请输入书名:
");
scanf("%s",p->name);
printf("请输入作者名:
");
scanf("%s",p->writer);
printf("请输入书号:
");
scanf("%s",p->shuhao);
printf("请输入分类:
");
scanf("%s",&p->fenlei);
printf("请输入出版社:
");
scanf("%s",&p->chuban);
printf("请输入出版时间:
");
scanf("%d",&p->chubantime);
printf("请输入价格:
");
scanf("%f",&p->price);
p->type=news;
printf("\n\n<提醒:
信息修改成功!
>\n\n");
}
}
if(b==1)printf("<没有您要修改的信息!
>\n");
}
system("PAUSE");
}
voidshow()//显示图书信息
{
structfi_info*p=NULL;
system("cls");
for(p=head;p!
=NULL;p=p->next)
{
printf("图书名称%s\n",p->name);
printf("图书作者%s\n",p->writer);
printf("图书书号%s\n",p->shuhao);
printf("图书分类%s\n",p->fenlei);
printf("出版社%s\n",p->chuban);
printf("出版时间%d\n",p->chubantime);
printf("图书价格%.2f\n",p->price);
system("PAUSE");
}
}
voidpaixu()//按出版时间排序
{
printf("***********************************按出版时间排序如下***********************************");
structfi_info*a[1000],*p,*p1,*temp;
inti,k,index,n=0;
charb;
p1=head;
for(p=head;p;p=p->next)
n++;
for(i=0;ia[i]=p1;
p1=p1->next;
}
for(k=0;kindex=k;
for(i=k+1;iif(a[index]->chubantime>a[i]->chubantime)
index=i;
}
temp=a[index];
a[index]=a[k];
a[k]=temp;
}
for(i=0;iprintf("图书书名%s\n",a[i]->name);
printf("图书作者%s\n",a[i]->writer);
printf("图书书号%s\n",a[i]->shuhao);
printf("图书分类%s\n",a[i]->fenlei);
printf("出版社%s\n",a[i]->chuban);
printf("出版时间%d\n",a[i]->chubantime);
printf("图书价格%.2f\n",a[i]->price);
}
system("PAUSE");
}
voidXG()
{inta=0;
printf("---------图书信息查询-----------\n");
printf("\t\t1-----------------修改\n");
printf("\t\t2------------------删除\n");
printf("请输入菜单前的数字:
");
scanf("%d",&a);
switch(a)
{
case1:
xiugai();
break;
case2:
shanchu();
break;
default:
break;
}
}
八、调试及总结
通过这次亲自动手编写C语言图书管理系统,从中收获了很多。
很多学过的知识得以巩固,对链表、数组指针、结构体等知识有了更深一步的了解。
编写的过程中我也借鉴别人设计的一些程序,从中发现了自己的一些不足,在构建框架和编写源程序上面的一些问题。
在源程序编写好后,对源程序的检测花费了我很多时间,这也算是锻炼了我的耐心。
在检测中发现的一些细节上的毛病和粗心这个坏习惯都需要我加以改正。
同时同学对我的帮助也比较大,有些问题很感谢他们对我的帮助。
亲自编写了这个大型程序后,我领悟了“实践出真知”这个道理,体会到了C语言的巨大用处,对C语言有了更浓厚的兴趣。
在今后我也会更加深入的学习C语言,打好稳固的基础,努力掌握这门技术,能够编写出更多更好的程序。