霍邱县商务助理:帮忙用C编写一个图书管理的程序。

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 03:20:55
一开始要输入书本的基本信息“作者 书名 编号 出版时间”一本书输入完了要保存 还要选择是继续添加别的书 还是退出 然后就是查找功能 查找输入的书。
大家回答时最好是把程序流程一起。

除了你要的功能,我还给你多了一个浏览所有图书的功能:
#include "malloc.h"
#include "stdio.h"
#define SIZE sizeof(struct book)
struct book
{
char auther[10];
char bname[20];
char code[10];
char date[20];
};
int menu()
{
char ch;
do
{
clrscr();
printf("\n\n ***************************************\n");
printf("\n\n 1. enter the book data;");
printf("\n\n 2. search the book by auther;");
printf("\n\n 3. search the book by book's name;");
printf("\n\n 4. search the book by serial code;");
printf("\n\n 5. search the book by publish day;");
printf("\n\n 6. browse all book;");
printf("\n\n 0. exit;");
printf("\n\n ***************************************\n");
printf("\n\n please seclect a item:");
ch=getchar();
}while(ch<'0' || ch>'6');
return(ch);
}
insert()
{
struct book *p;
FILE *fp;
char ch='y';
do
{
clrscr();
fp=fopen("book.dat","a");
p=malloc(SIZE);
printf("\nplease input the data of book:\n");
scanf("%s%s%s%s",p->auther,p->bname,p->code,p->date);
printf("\nyour inserted:\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
fwrite(p,SIZE,1,fp);
fclose(fp);
printf("\ncontinue?(y/n)\n");
getchar();
ch=getchar();
}while(ch=='y');

}
list()
{
FILE *fp;
struct book *p;
clrscr();
fp=fopen("book.dat","r");
printf("\n**********************************************");
printf("\nauther book code publish\n");
while (fread(p,SIZE,1,fp)==1)
{
printf("\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
}
printf("\n**********************************************");
fclose(fp);
}
search(char *s)
{
FILE *fp;
struct book *p;
char ch='y';
char search[25];
fp=fopen("book.dat","r");
while (ch=='y')
{
clrscr();
printf("\nplease input the %s you want to search:",s);
scanf("%s",search);
printf("\n**********************************************");
printf("\nauther book code publish\n");
while (fread(p,SIZE,1,fp)==1)
{
if ((strcmp(s,"auther")==0) && (strcmp(p->auther,search)==0))
printf("\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
else if ((strcmp(s,"bname")==0) && (strcmp(p->bname,search)==0))
printf("\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
else if ((strcmp(s,"code")==0) && (strcmp(p->code,search)==0))
printf("\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
else if ((strcmp(s,"date")==0) && (strcmp(p->date,search)==0))
printf("\n%-10s%-20s%-10s%-20s\n",p->auther,p->bname,p->code,p->date);
else
;
}
printf("\ndo you want to continue to search?(y/n)\n");
getchar();
ch=getchar();
}
fclose(fp);
}
main()
{
char s;
s=menu();
if (s=='0') exit(0);
else if(s=='1') insert();
else if(s=='2') search("auther");
else if(s=='3') search("bname");
else if(s=='4') search("code");
else if(s=='6') list();
else search("date");
}

貌似又一个快毕业的学生

a