#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#include<string>
class book
{ protected:
string isbn;
string name;
string writer;
string publicer;
string time;
public:
book(){}
book(string a,string b,string c,string d,string e)
{ isbn=a;
name= b;
writer=c;
publicer=d;
time=e;
}
void change_name(string a)
{ name=a;}
void change_isbn(string a)
{ isbn=a;}
void change_writer(string a)
{ writer=a;}
void change_publicer(string a)
{ publicer=a;}
void change_time(string a)
{ time=a;}
string get_name()
{return name;}
string get_isbn()
{return isbn;}
string get_writer()
{return writer;}
void display()
{
cout<<isbn<<" "<<name<<" "<<writer<<" "<<publicer<<" "<<time<<endl;
}
};
class manager:public book
{private:
book buffer[100];
int sum;
public:
manager(){sum=0;}
void add(book a)
{
if(sum>=10)
{ cout<<"添加失败,内存已满!"<<endl;
}
else
{buffer[sum]=a;
sum++;
cout<<"添加新书目成功,以下是本书信息"<<endl;
}
}
void delete_by_name(book a)
{ int flag=0;
if(sum==0)
{cout<<"删除失败,已经没有存档了!"<<endl;
}
else
{
for(int i=0;i<sum;i++)
{ if(buffer[i].get_name()==a.get_name())
{ cout<<"删除书目成功,以下是删除的书信息"<<endl;
buffer[i].display ();
for(int j=i;j<sum-1;j++)
{ buffer[j]=buffer[j+1]; }
flag=1;
sum--;
break;}
}
if(!flag)
{cout<<"the book does't exist!"<<endl;
}
}
}
void search_by_name(string names)
{ int flag=0;
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==names)
{ buffer[i].display ();
flag=1;}
}
if(flag==0)
{cout<<"the book does't exist!"<<endl;
}
}
void search_by_isbn(string names)
{ int flag=0;
for(int i=0;i<sum;i++)
{if(buffer[i].get_isbn()==names)
{ buffer[i].display ();
flag=1;}
}
if(flag==0)
{cout<<"the book does't exist!"<<endl;
}
}
void search_by_writer(string names)
{int flag=0;
for(int i=0;i<sum;i++)
{if(buffer[i].get_writer()==names)
{ buffer[i].display ();
flag=1;}
}
if(flag==0)
{cout<<"the book does't exist!"<<endl;
}
}
void change_isbns(string nam,string isn)
{
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==nam)
{ buffer[i].change_isbn(isn);}
}
}
void change_names(string nam,string isn)
{
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==nam)
{ buffer[i].change_name(isn);}
}
}
void change_writers(string nam,string isn)
{
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==nam)
{ buffer[i].change_writer(isn);}
}
}
void change_publicers(string nam,string isn)
{
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==nam)
{ buffer[i].change_publicer(isn);}
}
}
void change_times(string nam,string isn)
{
for(int i=0;i<sum;i++)
{if(buffer[i].get_name()==nam)
{ buffer[i].change_time(isn);}
}
}
void display_all()
{
for(int i=0;i<sum;i++)
{buffer[i].display (); }
}
void read()
{ FILE *fp;
if((fp=fopen("C:\\Users\\fei\\Desktop\\图书馆.txt","rb+"))==NULL)//注意路径
{cout<<"文件打开失败\n";
system("PAUSE");
exit(0);
}
fread(&sum,4,1,fp);
for(int i=0;i<sum;i++)
{
fread(&buffer[i],sizeof(buffer[i]),1,fp);}
fclose(fp);
}
void write()
{
FILE *fp2;
if((fp2=fopen("C:\\Users\\fei\\Desktop\\图书馆.txt","wb+"))==NULL)//注意路径
{cout<<"文件打开失败\n";
system("PAUSE");
exit(0);
}
fwrite(&sum,4,1,fp2);
for(int i=0;i<sum;i++)
{
fwrite(&buffer[i],sizeof(buffer[i]),1,fp2);}
fputc('EOF',fp2);
fclose(fp2);
}
};
//主函数
void main()
{ manager worker;
char function='0';
//worker.read ();
//界面
while (function!= '6')
{
for(int i=0;i<34;i++)
{cout<<"*";}
cout<<endl;
cout<<"欢迎使用本图书管理系统,请选择操作\n"<<endl;
cout<<"1 创建新书目"<<endl;
cout<<"2 删除书目"<<endl;
cout<<"3 查询书目"<<endl;
cout<<"4 修改信息"<<endl;
cout<<"5 显示所有藏书"<<endl;
cout<<"6 退出系统并保存修改"<<endl;
cin>>function;
if(function=='1')
{ string a,b,c,d,e;
cout<<"请输入书的isbn编号"<<endl;
cin>>a;
cout<<"请输入书名"<<endl;
cin>>b;
cout<<"请输入书作者名"<<endl;
cin>>c;
cout<<"请输入书的出版社"<<endl;
cin>>d;
cout<<"请输入书的出版时间"<<endl;
cin>>e;
book newbook(a,b,c,d,e);
worker.add(newbook);
newbook.display();
system("PAUSE");
system("CLS");
}
else if(function=='5')
{cout<<"这是所有藏书:"<<endl;
worker.display_all ();
system("PAUSE");
system("CLS");
}
else if(function=='2')
{ string a;
cout<<"请输入要删除的书名"<<endl;
cin>>a;
book del(" ",a," "," "," ");
worker.delete_by_name (del);
system("PAUSE");
system("CLS");
}
else if(function=='3')
{ cout<<"请选择查询方式"<<endl;
cout<<"1 按ISBN查询"<<endl;
cout<<"2 按书名查询"<<endl;
cout<<"3 按作者查询"<<endl;
char j;
string search;
cin>>j;
if(j=='1')
{ cout<<"请输入ISBN号"<<endl;
cin>>search;
cout<<"以下是查询结果"<<endl;
worker.search_by_isbn (search);
system("PAUSE");
system("CLS");
}
else if(j=='2')
{ cout<<"请输入书名"<<endl;
cin>>search;
cout<<"以下是查询结果"<<endl;
worker.search_by_name (search);
system("PAUSE");
system("CLS");
}
else if(j=='3')
{ cout<<"请输入作者名"<<endl;
cin>>search;
cout<<"以下是查询结果"<<endl;
worker.search_by_writer (search);
system("PAUSE");
system("CLS");
}
else
{ cout<<"输入错误,请重新操作!"<<endl;
system("PAUSE");
system("CLS");
}
}
else if(function=='4')
{ cout<<"请输入要修改的书的名字"<<endl;
string changes;
cin>>changes;
cout<<"以下是欲修改目标现在的信息"<<endl;
worker.search_by_name (changes);
cout<<"请选择下一步"<<endl;
cout<<"1 继续修改"<<endl;
cout<<"2 返回首页"<<endl;
char choose;
cin>>choose;
if(choose=='1')
{
cout<<"请选择要修改的选项"<<endl;
cout<<"1 修改ISBN编号"<<endl;
cout<<"2 修改书名"<<endl;
cout<<"3 修改作者"<<endl;
cout<<"4 修改出版社"<<endl;
cout<<"5 修改出版时间"<<endl;
char c;
cin>>c;
if(c=='1')
{ string news;
cout<<"请输入新的ISBN号码"<<endl;
cin>>news;
worker.change_isbns(changes,news);
cout<<"修改成功!"<<endl;
system("PAUSE");
system("CLS");
}
else if(c=='5')
{ string news;
cout<<"请输入新的出版时间"<<endl;
cin>>news;
worker.change_times(changes,news);
cout<<"修改成功!"<<endl;
system("PAUSE");
system("CLS");
}
else if(c=='2')
{ string news;
cout<<"请输入新的书名"<<endl;
cin>>news;
worker.change_names(changes,news);
cout<<"修改成功!"<<endl;
system("PAUSE");
system("CLS");
}
else if(c=='3')
{ string news;
cout<<"请输入新的作者"<<endl;
cin>>news;
worker.change_writers(changes,news);
cout<<"修改成功!"<<endl;
system("PAUSE");
system("CLS");
}
else if(c=='4')
{ string news;
cout<<"请输入新的出版社"<<endl;
cin>>news;
worker.change_publicers(changes,news);
cout<<"修改成功!"<<endl;
system("PAUSE");
system("CLS");
}
else
{cout<<"输入错误,请重新操作!"<<endl;
system("PAUSE");
system("CLS");
}
}
else
{ system("CLS"); }
}
else if(function=='6')
{;}
else
{cout<<"输入错误,请重新选择!"<<endl;
system("PAUSE");
system("CLS");
}
}
//退出系统
system("CLS");
// worker.write ();
for(int i=0;i<8;i++)
{ cout<<" "<<endl;}
for(int i=0;i<55;i++)
{cout<<"*";}
cout<<endl;
cout<<"您已退出本图书管理系统,您的修改已经保存,欢迎再次使用!"<<endl;
for(int i=0;i<55;i++)
{cout<<"*";}
cout<<endl;
system("PAUSE");
exit(0);
}