#pragma GCC optimize(2)
#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
FILE *fp; //创建文件指针
struct wmy {
char num[88]; //编号
char name[88]; //书名
char writer[88]; //作者
char price[88]; //价格
char lable[88]; //类别
char time[88]; //出版时间
char company[88]; //出版单位;
char kind ; //标记状态是否被借出 1表示可以借阅 2表示已经借阅未归还 0表示不存在这本书
} book;
int cnt=0;
int main() {
void reader(); // 读者管理(读者等级:可分为教师、学生,并定义每类读者可借书数量和相关的借阅时间等信息)
void add(); //添加图书
void del(); //删除图书
char memu(); //菜单
void change(); //修改图书信息
void search(); //查询图书信息
void output(); //显示所有图书信息
int zhanghao,mima; //帐号密码均为666
cout<
cin>>zhanghao;
cout<
cin>>mima;
if(zhanghao==666&&mima==666) {
while(1) {
switch (memu()) {
case '1':
add();
break;
case '2':
output();
break;
case '3':
del();
break;
case '4':
change();
break;
case '5':
search();
break;
case '6':
reader();
break;
case '7':
return 0;
break;
}
}
} else {
cout<
main();
}
return 0;
}
char memu() {
char x;
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cout<
cin>>x;
return x; //作为返回值返回到主函数选择功能
}
void add() {
struct wmy ac[666]; //创建另一个结构体 作为临时储存源文件信息,
int i=0;
FILE *xi=fopen("book.txt","rb+");
while(fread(&ac[i],sizeof(book),1,xi)==1)
i++; //每次一字节读book源文件的信息
int x=i; //确定book里有几本书,从i开始继续添加
fclose(xi);
cout<
cin>>book.num;
cout<
cin>>book.name;
cout<
cin>>book.writer;
cout<
cin>>book.company;
cout<
cin>>book.time;
cout<
cin>>book.price;
cout<
cin>>book.lable;
book.kind=1;
ac[x++]=book; //添加新的图书
fp=fopen("book.txt","wb");
for(int j=0; j
fwrite(&ac[j],sizeof(book),1,fp ); //每次一个字节将新书 的信息写入book
fclose(fp); //关闭文件,保存数据
}
void del() {
FILE *fp1;
FILE *fp2;
if((fp1=fopen("book.txt","rb"))== NULL) { // 以只读的方式 打开,如果指针返回为空,则无法打开
cout<
getchar();
exit(1);//跳出语句
}
if((fp2=fopen("temp.txt","wb"))==NULL) { //建立一个临时文件temp,以只写的方式打开当temp被占用时无法打开 或指针返回为空时
cout<
getchar();
exit(1);
}
char x[88];
cout<
cin>>x;
int y=0;
while(fread(&book,sizeof(book),1,fp1)==1) {
if(strcmp(book.num,x)==0) {
y=1;
} else {
fwrite(&book,sizeof(book),1,fp2);
}
}
fclose(fp1);
fclose(fp2);
if(y) {
cout<
remove("book.txt");
rename("temp.txt","book.txt");
} else
cout<
getchar();
getchar();
}
void output() {
fp=fopen("book.txt","rb");
cout<
while(fread(&book,sizeof(book),1,fp)==1) {
printf("%s%10s%8s",book.num,book.name,book.writer);
printf("%10s%8s%12s%10s\n",book.lable,book.company,book.time,book.price );
}
fclose(fp);
getchar();
getchar();
}
void change() {
FILE *fp1,*fp2;
int y=1;
char temp[66],tempname[66],tempwriter[66],templable[66],tempprice[66],tempcompany[66],temptime[66];
cout<
cin>>temp;
if((fp1=fopen("book.txt","rb"))== NULL) { // 以只读的方式 打开,如果指针返回为空,则无法打开
cout<
getchar();
exit(1);//跳出语句
}
if((fp2=fopen("temp.txt","wb"))==NULL) { //建立一个临时文件temp,以只写的方式打开当temp被占用时无法打开 或指针返回为空时
cout<
getchar();
exit(1);
}
while(fread(&book,sizeof(book),1,fp1)==1) {
if(strcmp(book.num,temp)==0) {
y=0;
cout<
cin>>tempname;
strcpy(book.name,tempname);
cout<
cin>>tempwriter;
strcpy(book.writer,tempwriter);
cout<
cin>>templable;
strcpy(book.lable,templable);
cout<
cin>>tempcompany;
strcpy(book.company,tempcompany);
cout<
cin>>temptime;
strcpy(book.time,temptime);
cout<
cin>>tempprice;
strcpy(book.price,tempprice);
}
fwrite(&book,sizeof(book),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(!y) {
cout<
remove("book.txt");
rename("temp.txt","book.txt");
}
getchar();
getchar();
}
void reader() {
int x;
cout<
cin>>x;
if(x==1)
cout<
else if(x==2)
cout<
else
cout<
getchar();
getchar();
}
void search() {
char x[88];
cout<
int y=0;
while(fread(&book,sizeof(book),1,fp)) {
if(strcmp(x,book.num)==0) {
y=1;
cout<
printf("%s%10s%10s ",book.num,book.name,book.writer);
printf("%10s%8s%10s%10s\n ",book.lable,book.company,book.time,book.price );
}
}
if(y==0)
cout<
getchar();
getchar();
}