c语言编程——图书信息管理
#include<stdio.h> /*输入输出函数*/ #include<string.h> /*字符串操作函数*/
#include<stdlib.h> /*数值转换函数*/
#include<conio.h> /*控制台输入输出函数*/
#define MAX 100 /*宏定义?参数为形参*/
struct book /*图书信息结构定义*/
{ int num; /*图书编号*/
char name[100]; /*书名*/
char author[100]; /*作者名*/
char classNo[100]; /*分类号*/
char publisher[100]; /*出版单位*/
char pubTime[100]; /*出版时间*/
float price; /*价格*/
}bk[MAX];
void Input()/*输入*/
{
FILE *fp;
int i;
fp=fopen("book","ab");/*建立一个新二进制文件*/
for(i=0;i<MAX;i++)
{
printf("i=%d 输入序号i(当输入i=-1时,返回),i=",i++);
scanf("%d",&i);
if(i==-1)
{
fclose(fp);
return;/*默认不返回值*/
}
else
{
printf("请输入:\n编号 书名 作者名 分类号 出版社 出版时间 价格 输入时用逗号隔开)\n");
scanf("%d,%s,%s,%s,%s,%s,%f",&bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,&bk[i].price);
fwrite(&bk[i],sizeof(struct book),1,fp);/*写入文件*/
} }
fclose(fp);
}
void Output()/*输出*/
{
FILE *fp;
int i;
fp=fopen("book","rb");
printf("================================================================================\n");
printf(" 图书列表\n");
printf("--------------------------------------------------------------------------------\n");
printf("编号 书名 作者名 分类号 出版社 出版时间 价格");
for(i=0;fread(&bk[i],sizeof(struct book),1,fp)==1;i++) /*记录写入临时文件*/
{
printf("\n%d%s%s%s%8s%s%f\n",bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,bk[i].price);
}
fclose(fp);
printf("--------------------------------------------------------------------------------\n");
}
void Find()/*查找*/
{
int i;
int choose,t;
char m[100];
do
{
printf("================================================================================\n");
printf("\t\t\t\t 查找菜单\n");
printf("\t\t\t\t1.按书名查找\n");
printf("\t\t\t\t2.按作者名查找\n");
printf("\t\t\t\t3.返回主菜单\n");
printf("===============================================================================\n");
printf("输入选项代号:");
scanf("%d",&choose);
if(choose==1)
{
printf("输入所查书名:\n");
scanf("%s",m);
t=-1;
if(choose==1)
{
for(i=0;i<MAX;i++)
if(strcmp(m,bk[i].name)==0)
{
t=i;
printf("%d%s%s%s%s%8s %f\n",bk[t].num,bk[t].name,bk[t].author,bk[t].classNo,bk[t].publisher,bk[t].pubTime,bk[t].price);
} }
if(t==-1)
printf("不存在该信息\n");
}
else if(choose==2)
{
printf("输入所查作者名?\n");
scanf("%s",m);
t=-1;
if(choose==2)
{
for(i=0;i<MAX;i++)
if(strcmp(m,bk[i].author)==0)
{
t=i;
printf("%d%s%s%s%s%s%f\n",bk[t].num,bk[t].name,bk[t].author,bk[t].classNo,bk[t].publisher,bk[t].pubTime,bk[t].price);
} }
if(t==-1) printf("不存在该信息\n");
}
else return;
}
while(1);
}
void Delete()/*删除*/
{
FILE *fp;
int i,flag,n,j, s;
fp=fopen("book","rb+"); /*以读/写 的方式打开一个已存在的二进制文件*/
rewind(fp);/*把文件指针fp重新指向文件的开始处*/
printf(" 图书列表\n");
printf("--------------------------------------------------------------------------------\n");
printf(" 编号书名作者名分类号出版单位出版时间价格\n");
printf("---------------------------------------------------------------------------------\n");
for(i=0;fread(&bk[i],sizeof(struct book),1,fp)==1;i++)
{ printf("%d%s%s%s%s%s%f\n",bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,bk[i].price);
printf("\n"); }
n=i;
printf("输入待删除图书编号:\n");
scanf("%d",&s);
for(i=0,flag=1;flag&&i<n;i++)
{ if(s==bk[i].num)
{ for(j=i;j<n-1;j++)
{ bk[j].num=bk[j+1].num;
strcpy(bk[j].name,bk[j+1].name); /*把bk[j+1].name复制到bk[j].name*/
strcpy(bk[j].author,bk[j+1].author);
strcpy(bk[j].classNo,bk[j+1].classNo);
strcpy(bk[j].publisher,bk[j+1].publisher);
strcpy(bk[j].pubTime,bk[j+1].pubTime);
bk[j].price=bk[j+1].price;
}
flag=0;
} }
if(!flag)
n=n-1;
else
printf("没有此号\n");
fp=fopen("book","wb");
for(i=0;i<n;i++)
fwrite(&bk[i],sizeof(struct book),1,fp); /*将&bk[i]所指的地址的1个长度为sizeof的数据项写入到fp所指的文件流中去。*/
fclose(fp);
fp=fopen("book","r"); /*以只读方式打开一个文本文件*/
printf(" 图书列表\n");
printf("-------------------------------------------------------------------------------\n");
printf(" 编号书名作者名分类号出版单位出版时间价格\n");
printf("-------------------------------------------------------------------------------\n");
for(i=0;i<n;i++)
{ fread(&bk[i],sizeof(struct book),1,fp);/*从fp所指的文件中读取1个数据项,每个数据项的长度为sizeof个字节,放到&bk[i]所指的快中*/
printf("%d %s%s%s%s%s%f\n",bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,bk[i].price);
printf("\n");
fclose(fp); } }
void Change()/*修改*/
{
int i,n;char num; int flag=0; FILE *fp;
printf("请输入要修改的图书编号:");
scanf("%d",&num);
for(i=0;i<=MAX;i++)
if(bk[i].num==num)
{
printf(" 图书列表\n");
printf("--------------------------------------------------------------------------------\n");
printf(" 编号书名作者名分类号出版单位出版时间价格\n");
printf("---------------------------------------------------------------------------------\n");
printf("%d%s%s%s%s%s%f",bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,bk[i].price);
printf("--------------------------------------------------------------------------------\n\n");
n=i; flag=1; break; }
if(flag==0)
{
printf("==============================================================================\n");
printf("\t\t\t输入错误! \n");
printf("\t\t\t请按任意键返回! \n");
printf("===============================================================================\n");
return;
}
printf("\n\n\n");
fp=fopen("bk","rb+");
fseek(fp,n*sizeof(struct book),0);/*将fp所指的文件指针,从文件开始起移动n*sizeof(struct book)个位置(0:文件开始1:当前位置2:文件末尾)fseek:文件定位函数*/
printf("编号书名作者名分类号出版单位出版时间价格\n");
scanf("%d%s%s%s%s%s%f",&bk[n].num,bk[n].name,bk[n].author,bk[n].classNo,bk[n].publisher,bk[n].pubTime,&bk[n].price);
fwrite(&bk[i],sizeof(struct book),1,fp);
fclose(fp);
fp=fopen("book","rb");
printf(" 图书列表\n");
printf("--------------------------------------------------------------------------------\n");
printf(" 编号书名作者名分类号出版单位出版时间价格\n");
printf("--------------------------------------------------------------------------------\n");
for(i=0;
fread(&bk[i],sizeof(struct book),1,fp)==1;i++)
{
printf("%d%s%s%s%s%s%f",bk[i].num,bk[i].name,bk[i].author,bk[i].classNo,bk[i].publisher,bk[i].pubTime,bk[i].price); }
printf("--------------------------------------------------------------------------------\n\n");
fclose(fp);
}
void main()/*主函数*/
{
int n;
printf("\n");
printf("================================================================================\n");
printf("\t\t\t欢迎来玉溪师范学院图书信息管理系统\n");
printf("\t\t\t\t1.管理员模式\n\t\t\t\t2.读者模式\n\t\t\t\t3.退出系统\n");
printf("===============================================================================\n");
printf("请输入选择项(1-3):");
scanf("%d",&n);
printf("\n");
if(n==1)
{
int m;
printf("请输入管理员密码:");
scanf("%d",&m);
if(m==100)
{
for(;;)/*省略"初始化"、"条件表达式"和"增量" ,起作用*/
{
int s;
printf("\n");
printf("===============================================================================\n");
printf("\t\t\t欢迎来玉溪师范学院图书信息管理系统\n");
printf("\t\t\t\t 主菜单\n");
printf("\t\t\t\t1.图书信息录入\n");
printf("\t\t\t\t2.图书信息浏览\n");
printf("\t\t\t\t3.图书信息查询\n");
printf("\t\t\t\t4.图书信息删除\n");
printf("\t\t\t\t5.图书信息修改\n");
printf("\t\t\t\t6.退出系统\n");
printf("===============================================================================\n\n");
printf("请输入选择项(1-6):");
scanf("%d",&s);
printf("\n");
if(s>0&&s<7)
{
switch(s)
{
case 1:Input();break;
case 2:Output();break;
case 3:Find();break;
case 4:Delete();break;
case 5:Change();break;
case 6:printf("=============================================================================\n");
printf("\t\t\t\t谢谢使用! \n");
printf("\t\t\t\t再见! \n");
printf("===============================================================================\n");
exit(0); } } } }
else
printf("密码错误,请您重新进入系统\n");
}
else if(n==2)
{
int t;
printf("请输入读者密码:");
scanf("%d",&t);
if(t==123)
{ for(;;)/*省略"初始化"、"条件表达式"和"增量" ,起作用*/
{
int j;
printf("\n");
printf("===============================================================================\n");
printf("\t\t\t欢迎来玉溪师范学院图书信息管理系统\n");
printf("\t\t\t\t 主菜单\n");
printf("\t\t\t\t1.图书信息浏览\n");
printf("\t\t\t\t2.图书信息查询\n");
printf("\t\t\t\t3.退出系统\n");
printf("===============================================================================\n\n");
printf("请输入选择项(1-3):");
scanf("%d",&j);
printf("\n");
if(j>0&&j<4)
{ switch(j)
{
case 1:Output();break;
case 2:Find();break;
case 3:printf("===============================================================================\n");
printf("\t\t\t\t谢谢使用! \n");
printf("\t\t\t\t再见! \n");
printf("===============================================================================\n");
exit(0); } } } }
else
printf("密码错误,请您重新进入系统\n");
}
}