1、#include “stdio.h“#include “stdlib.h“#include “string.h“#include “io.h“typedef struct book char num15; /中图分类号char name20; /书名char author20; /作者char press20; /出版单位char time15; /出版时间char status10; /是否可借double price; /价格int count; /库存量book;void menu(); /图书馆系统界面void insert(); /插入图书信息记录void browse(); /浏览
2、图书信息void find(); /查找图书信息void deleter(); /删除图书信息void modify(); /修改图书信息void leave(); /退出图书系统void main()int choice;domenu();printf(“请选择服务项目:“);scanf(“%d“,switch(choice)case 1:insert();break;case 2:browse();break;case 3:find();break;case 4:deleter();break;case 5:modify();break;case 6:leave();break;defau
3、lt:printf(“输入数据错误,请从新输入!“);while(1);void menu()printf(“t=欢迎进入图书馆系统!=nn“);printf(“tt*1.录入图书信息*n“);printf(“tt*2.浏览图书信息*n“);printf(“tt*3.查找图书信息*n“);printf(“tt*4.删除图书信息*n“);printf(“tt*5.修改图书信息*n“);printf(“tt*6.退出*n“);void insert()FILE *fp;book a=“0“,“0“,“0“,“0“,“0“,“0“,0.0,0;if(access(“bookinf.txt“,0) /
4、如果文件不存在,则初始化文件格式if(fp=fopen(“bookinf.txt“,“w“)=NULL)printf(“file creat failure!“);exit(0);fprintf(fp,“中图分类好 书名 作者 出版社 出版时间 状态 价格 数量“);else /如果文件存在,则打开文件追加信息if(fp=fopen(“bookinf.txt“,“a+“)=NULL)printf(“file open error!“);exit(0);printf(“请输入图书信息n“);printf(“中图分类好 书名 作者 出版社 出版时间 状态 价格 数量n“);scanf(“%s%s%
5、s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,printf(“%s %s %s %s %s %s %lf %dn“,a.num,a.name,a.author,a.press,a.time,a.status,a.price,a.count);fprintf(fp,“n%-15s%-20s%-20s%-20s%-15s%-10s%-8.2lf%4d“,a.num,a.name,a.author,a.press,a.time,a.status,a.price,a.count); /-为左对齐,必须是“n 和+右对齐 ”,方便
6、实现 browse()函数fclose(fp);void browse()FILE *fp;book a=“0“,“0“,“0“,“0“,“0“,“0“,0.0,0,b=a;if(fp=fopen(“bookinf.txt“,“a+“)=NULL)printf(“file open error!“);exit(0);fscanf(fp,“%*n“); /跳过文本的第一行 fscanf(fp,“%*n%*c“);?while(!feof(fp)fscanf(fp,“%s%s%s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,p
7、rintf(“%-4s%-4s%-4s%-4s%-4s%-4s%-8.2lf%-4dn“,a.num,a.name,a.author,a.press,a.time,a.status,a.price,a.count);a=b;fclose(fp);void find()char str15=“0“;FILE *fp;int flag=0;book a=“0“,“0“,“0“,“0“,“0“,“0“,0.0,0,b=a;printf(“请输入所要查找图书的中图分类号:“);scanf(“%s“,str);if(fp=fopen(“bookinf.txt“,“r“)=NULL)printf(“fil
8、e open error!n“);exit(0);fscanf(fp,“%*n“); /跳过文本的第一行 fscanf(fp,“%*n%*c“);?while(!feof(fp)fscanf(fp,“%s%s%s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,if(!strcmp(a.num,str)flag=1;printf(“所找图书信息为:“);printf(“%-4s%-4s%-4s%-4s%-4s%-4s%-8.2lf%-4dn“,a.num,a.name,a.author,a.press,a.time,a.sta
9、tus,a.price,a.count);break;a=b; /b 的用处if(flag=0)printf(“您所找的图书不存在!n“);fclose(fp);void deleter()char str15=“0“;int flag=0;FILE *fp,*fp1;book a=“0“,“0“,“0“,“0“,“0“,“0“,0.0,0,b=a;printf(“请输入所要删除图书的中图分类号:“);scanf(“%s“,str);if(fp=fopen(“bookinf.txt“,“r“)=NULL)printf(“file open error!“);exit(0);if(fp1=fop
10、en(“f.txt“,“w“)=NULL)printf(“file open error!“);exit(0);fprintf(fp1,“中图分类好 书名 作者 出版社 出版时间 状态 价格 数量“);fscanf(fp,“%*n“); /跳过文本的第一行 fscanf(fp,“%*n%*c“);?while(!feof(fp)fscanf(fp,“%s%s%s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,if(!strcmp(str,a.num)flag=1;continue;fprintf(fp1,“n%-15s%-2
11、0s%-20s%-20s%-15s%-10s%-8.2lf%4d“,a.num,a.name,a.author,a.press,a.time,a.status,a.price,a.count);a=b;fclose(fp1);fclose(fp);remove(“bookinf.txt“);rename(“f.txt“,“bookinf.txt“);if(flag=0)printf(“所要删除的图书不存在!n“);elseprintf(“删除成功!n“);void modify()char str15=“0“;FILE *fp,*fp1;char s120=“0“;book a=“0“,“0“
12、,“0“,“0“,“0“,“0“,0.0,0;printf(“请输入所要修改图书的中图分类号:“);scanf(“%s“,str);if(fp=fopen(“bookinf.txt“,“r+“)=NULL)printf(“file open error!“);exit(0);if(fp1=fopen(“f.txt“,“w“)=NULL)printf(“file open error!“);exit(0);fgets(s,120,fp); /读取 bookinf.txt 第一行的信息,使读取位置移动到第二行fprintf(fp1,“中图分类好 书名 作者 出版社 出版时间 状态 价格 数量“);
13、/初始化新文件 f.txtwhile(!feof(fp)fscanf(fp,“%s%s%s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,if(!strcmp(str,a.num) /如果遇到所要修改的记录,则从新输入图书信息printf(“请输入图书信息,即将记录修改为:n“);printf(“中图分类好 书名 作者 出版社 出版时间 状态 价格数量n“);scanf(“%s%s%s%s%s%s%lf%d“,a.num,a.name,a.author,a.press,a.time,a.status,fprintf(fp1,“n%-15s%-20s%-20s%-20s%-15s%-10s%-8.2lf%4d“,a.num,a.name,a.author,a.press,a.time,a.status,a.price,a.count);fclose(fp1);fclose(fp);remove(“bookinf.txt“);rename(“f.txt“,“bookinf.txt“);void leave()exit(0);