简单的书籍管理系统,就是查找不到录入的信息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct book
{
char title[20]; /*书名*/
char author_name[20]; /*作者名*/
char class_num[20]; /*分类号*/
char publisher[20]; /*出版单位*/
char publication_data[20]; /*出版价格*/
char price[10]; /*价格*/
}BOOK;
FILE*fp;
void menu(); /*列出菜单*/
void input_data(); /*录入信息*/
void update(); /*数据的修改*/
void insert_data(); /*数据的修改*/
void delete(); /*数据的删除*/
void find_menu(); /*图书信息查找菜单*/
void find_title(); /*图书信息查找*/
void find_author_name(); /*图书信息查找*/
void input_data() /*用来实现对图书信息的录入*/
{
int key=1;
printf("\n*********************图书信息录入*********************\n");
if((fp=fopen("D:\\Books_Management_System_BMS.txt","a+"))==NULL)
{
printf("Can not open file!\n");
exit(0);
}
while(key==1)
{
printf("请输入信息:\n");
printf("书名:");
scanf("%s",BOOK.title);
fflush(stdin); /*清空缓冲区*/
printf("作者名:");
scanf("%s",BOOK.author_name);
fflush(stdin);
printf("图书分类号:");
scanf("%s",BOOK.class_num);
fflush(stdin);
printf("图书出版单位:");
scanf("%s",BOOK.publisher);
fflush(stdin);
printf("图书出版时间:");
scanf("%s",BOOK.publication_data);
fflush(stdin);
printf("图书价格:");
scanf("%s",BOOK.price);
fwrite(&BOOK,sizeof(struct book),1,fp);
printf("录入成功\n");
printf("是否继续?是的话请按1,否的话请按0退出:");
scanf("%d",&key);
}
fclose(fp);
system("pause"); /*输出类似于“Press any key to exit”的字*/
return;
}
void update() /*用来实现对图书信息的修改*/
{
int flag=0;
int key=1;
char title[20];
char new_title[20];
char author_name[20];
char class_num[20];
char publisher[20];
char publication_data[20];
char price[10];
while(key==1)
{
printf("请输入您要修改信息的图书的名称:");
scanf("%s",title);
fp=fopen("D:\\Books_Management_Systen_BMS.txt","rb+");
while((fread(&BOOK,sizeof(struct book),1,fp))==1) /*fread函数的类型为int型,如果函数执行成功,则返回值为形参count的值1,即输出项的个数*/
{
if(strcmp(BOOK.title,title)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("书名:");
printf("%s\n",BOOK.title);
printf("作者:");
printf("%s\n",BOOK.author_name);
printf("图书号分类:");
printf("%s\n",BOOK.class_num);
printf("图书出版单位:");
printf("%s\n",BOOK.publisher);
printf("图书出版时间:");
printf("%s\n",BOOK.publication_data);
printf("图书价格:");
printf("%s\n",BOOK.price);
printf("请输入新的图书信息:");
printf("书名:");
scanf("%s",new_title);
fflush(stdin);
printf("\n作者:");
scanf("%s",author_name);
fflush(stdin);
printf("\n图书分类号:");
scanf("%s",class_num);
fflush(stdin);
printf("\n图书出版单位:");
scanf("%s",publisher);
fflush(stdin);
printf("\n图书出版时间:");
scanf("%s",publication_data);
fflush(stdin);
printf("\n图书价格:");
scanf("%s",price);
fflush(stdin);
fseek(fp,-sizeof(struct book),1); /*将文件位置标记向后退一个struct book锁占的长度*/
strcpy(BOOK.title,new_title);
strcpy(BOOK.author_name,author_name);
strcpy(BOOK.class_num,class_num);
strcpy(BOOK.publisher,publisher);
strcpy(BOOK.publication_data,publication_data);
strcpy(BOOK.price,price);
fwrite(&BOOK,sizeof(struct book),1,fp);
printf("修改成功!\n");
fclose(fp);
}
else
{
printf("没有此书的信息!\n");
}
printf("是否继续?是的话请按1,否的话请按0退出:");
scanf("%d",&key);
}
system("pause");
return;
}
void insert_data() /*插入图书信息*/
{
int key=1;
fp=fopen("D:\\Books_Management_Systen_BMS.txt","a+");
while(key==1)
{
printf("请输入图书信息:\n");
printf("书名:");
scanf("%s",BOOK.title);
printf("作者:");
scanf("%s",BOOK.author_name);
printf("图书分类号:");
scanf("%s",BOOK.class_num);
printf("图书出版单位:");
scanf("%s",BOOK.publisher);
printf("图书出版时间:");
scanf("%s",BOOK.publication_data);
printf("图书价格:");
scanf("%s",BOOK.price);
fwrite(&BOOK,sizeof(struct book),1,fp);
printf("插入成功!\n");
printf("是否继续?是的话请按1,否的话请按0退出:");
scanf("%d",&key);
}
fclose(fp);
system("pause");
return;
}
void delete() /*删除图书信息*/
{
int key=1;
int flag=0;
char title[20];
while(key==1)
{
printf("请输入您要删除图书信息的名称:");
scanf("%s",title);
fp=fopen("D:\\Books_Management_Systen_BMS.txt","rb+");
while((fread(&BOOK,sizeof(struct book),1,fp))==1)
{
if(strcmp(title,BOOK.title)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
fseek(fp,-sizeof(struct book),1);
strcpy(BOOK.title,"");
strcpy(BOOK.author_name,"");
strcpy(BOOK.class_num,"");
strcpy(BOOK.publisher,"");
strcpy(BOOK.publication_data,"");
strcpy(BOOK.price,"");
fwrite(&BOOK,sizeof(struct book),1,fp);
printf("删除成功!\n");
}
else
printf("没有此书的信息\n");
fclose(fp);
printf("是否继续?是的话请按1,否的话请按0:");
scanf("%d",&key);
}
system("pause");
return;
}
void find_title() /*根据书名查找图书信息*/
{
int flag=0;
int key=1;
char title[20];
while(key==1)
{
fp=fopen("D:\\Books_Management_Systen_BMS.txt","rb");
printf("书名:");
scanf("%s",title);
while((fread(&BOOK,sizeof(struct book),1,fp))==1)
{
if(strcmp(title,BOOK.title)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("书名:");
scanf("%s\n",BOOK.title);
printf("作者:");
scanf("%s\n",BOOK.author_name);
printf("图书分类号:");
scanf("%s\n",BOOK.class_num);
printf("图书出版单位:");
scanf("%s\n",BOOK.publisher);
printf("图书出版时间:");
scanf("%s\n",BOOK.publication_data);
printf("图书价格:");
scanf("%s\n",BOOK.price);
}
else if(flag==0)
{
printf("没有此书的信息\n");
}
fclose(fp);
printf("是否继续?是的话请按1,否的话请按0:");
scanf("%d",&key);
}
system("pause");
return;
}
void find_author_name() /*根据作者名查找图书信息*/
{
int flag=0;
int key=1;
char author_name[20];
while(key==1)
{
fp=fopen("D:\\Books_Management_Systen_BMS.txt","rb");
printf("作者:");
scanf("%s",author_name);
while((fread(&BOOK,sizeof(struct book),1,fp))==1)
{
if(strcmp(author_name,BOOK.author_name)==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("书名:");
scanf("%s\n",BOOK.title);
printf("作者:");
scanf("%s\n",BOOK.author_name);
printf("图书分类号:");
scanf("%s\n",BOOK.class_num);
printf("图书出版单位:");
scanf("%s\n",BOOK.publisher);
printf("图书出版时间:");
scanf("%s\n",BOOK.publication_data);
printf("图书价格:strcmp(title,BOOK.title)==0");
printf("%s\n",BOOK.price);
}
else if(flag==0)
{
printf("没有此书的信息\n");
}
fclose(fp);
printf("是否继续?是的话请按1,否的话请按0:");
scanf("%d",&key);
}
system("pause");
return;
}
void find_menu() /*图书查询信息菜单*/
{
char key;
while(key!='3')
{
system("cls");
printf("\n");fflush(stdin);
printf("\n");
printf("|*****************图书信息查询*****************");
printf("|\t\t1.按书名查找:\n");
printf("|\t\t2.按作者查找:\n");
printf("|\t\t3.退出图书信息查询:\n");
printf("|\n");
printf("|*****************谢谢使用*****************\n\n");
key=getchar();
switch(key)
{
case'1':
find_title();
break;
case'2':
find_author_name();
break;
case'3':
system("pause");
break;
default:
printf("enter data errorr\n");
}
}
}
void menu() /*主菜单*/
{
char key;
while(key!='0')
{
system("cls");
printf("\n");
printf("\n");
printf("|*****************欢迎使用图书管理系统*****************\n\n");
printf("|\t\t1.图书信息录入:\n");
printf("|\t\t2.图书信息修改:\n");
printf("|\t\t3.图书信息插入:\n");
printf("|\t\t4.图书信息删除:\n");
printf("|\t\t5.图书查找:\n");
printf("|\t\t0.退出系统:\n");
printf("|\n");
printf("|*********************感谢使用*************************\n\n");
key=getchar();
switch(key)
{
case'1':
input_data();
break;
case'2':
update();
break;
case'3':
insert_data();
break;
case'4':
delete();
break;
case'5':
find_menu();
break;
case'0':
break;
default:
printf("enter data error\n");
}
}
}
int main()
{
menu();
printf("退出系统\n");
return 0;
}