
求解C++程序:图书馆的图书检索卡记载的内容包括书名,作者姓名,出版日期,页数,定价等内容。
根据上述内容定义一个结构体类型,并按照作者姓名进行查找,按照出版日期顺序从远到近打印出该作者的所有著作。急需程序代码~拜托了各位~...
根据上述内容定义一个结构体类型,并按照作者姓名进行查找,按照出版日期顺序从远到近打印出该作者的所有著作。急需程序代码~拜托了各位~
展开
1个回答
展开全部
#include <iostream>
#include <string>
#include <list>
using namespace std;
// 图书检索卡结构体
struct BookIndex
{
//编号
int uid;
//书名
string name;
//作者姓名
string author;
//出版日期(格式YYYY-MM-DD)
string publicDate;
//页数
int pages;
//定价
float price;
friend istream& operator >> (istream &is,struct BookIndex &b)
{
cout<<"书名:";
is>>b.name;
cout<<"作者:";
is>>b.author;
cout<<"出版日期:(格式YYYY-MM-DD) ";
is>>b.publicDate;
cout<<"页数:";
is>>b.pages;
cout<<"定价:";
is>>b.price;
return is;
};
friend ostream& operator << (ostream &os,const struct BookIndex &b)
{
os<<"-------------------------"<<endl;
os<<"编号:"<<b.uid<<endl;
os<<"书名:"<<b.name<<endl;
os<<"作者:"<<b.author<<endl;
os<<"出版日期:"<<b.publicDate<<endl;
os<<"页数:"<<b.pages<<endl;
os<<"定价:"<<b.price<<endl;
os<<"-------------------------"<<endl;
return os;
}
friend bool operator <(struct BookIndex &b1,struct BookIndex &b2)
{
return b1.publicDate < b2.publicDate;
}
friend bool operator >(struct BookIndex &b1,struct BookIndex &b2)
{
return b1.publicDate > b2.publicDate;
}
private:
static int UID;
static int getUID(){return UID++;}
};
int BookIndex::UID = 1;
// 图书馆类
class Library
{
public:
Library(){}
// 登记 n 本图书
void registerBooks(int n)
{
for(int i=0;i<n;i++)
{
struct BookIndex b;
cout<<"输入第:"<<(i+1)<<"本书信息:"<<endl;
cin>>b;
books.push_back(b);
cout<<endl;
}
}
// 按照作者姓名进行查找,
// 并且按照出版日期顺序从远到近排列该找到的所有著作
list<struct BookIndex> searchBooks(string author)
{
list<struct BookIndex> result;
for(list<struct BookIndex>::iterator i=books.begin();i!=books.end();i++)
{
if(i->author == author)
result.push_back(*i);
}
result.sort();
return result;
}
private:
list<struct BookIndex> books;
};
int main(int argc, char *argv[])
{
Library library;
library.registerBooks(10);
list<struct BookIndex> books=library.searchBooks("angel");
for(list<struct BookIndex>::iterator i=books.begin();i!=books.end();i++)
{
cout<<*i<<endl;
}
return 0;
}
/*
book_aaa
angel
2012-01-01
100
100
book_bbb
blue
2000-11-02
136
14.5
book_ccc
angel
2010-03-12
56
123.5
book_ddd
angelfish
1989-04-14
1200
210
book_eee
angel
1991-11-23
456
123
book_fff
angel
2001-07-07
789
424
book_ggg
angel
2004-09-30
321
9
book_hhh
angel
2002-06-06
999
56
book_iii
angel
2008-08-08
654
81
book_jjj
angel
1998-09-09
111
99
*/
#include <string>
#include <list>
using namespace std;
// 图书检索卡结构体
struct BookIndex
{
//编号
int uid;
//书名
string name;
//作者姓名
string author;
//出版日期(格式YYYY-MM-DD)
string publicDate;
//页数
int pages;
//定价
float price;
friend istream& operator >> (istream &is,struct BookIndex &b)
{
cout<<"书名:";
is>>b.name;
cout<<"作者:";
is>>b.author;
cout<<"出版日期:(格式YYYY-MM-DD) ";
is>>b.publicDate;
cout<<"页数:";
is>>b.pages;
cout<<"定价:";
is>>b.price;
return is;
};
friend ostream& operator << (ostream &os,const struct BookIndex &b)
{
os<<"-------------------------"<<endl;
os<<"编号:"<<b.uid<<endl;
os<<"书名:"<<b.name<<endl;
os<<"作者:"<<b.author<<endl;
os<<"出版日期:"<<b.publicDate<<endl;
os<<"页数:"<<b.pages<<endl;
os<<"定价:"<<b.price<<endl;
os<<"-------------------------"<<endl;
return os;
}
friend bool operator <(struct BookIndex &b1,struct BookIndex &b2)
{
return b1.publicDate < b2.publicDate;
}
friend bool operator >(struct BookIndex &b1,struct BookIndex &b2)
{
return b1.publicDate > b2.publicDate;
}
private:
static int UID;
static int getUID(){return UID++;}
};
int BookIndex::UID = 1;
// 图书馆类
class Library
{
public:
Library(){}
// 登记 n 本图书
void registerBooks(int n)
{
for(int i=0;i<n;i++)
{
struct BookIndex b;
cout<<"输入第:"<<(i+1)<<"本书信息:"<<endl;
cin>>b;
books.push_back(b);
cout<<endl;
}
}
// 按照作者姓名进行查找,
// 并且按照出版日期顺序从远到近排列该找到的所有著作
list<struct BookIndex> searchBooks(string author)
{
list<struct BookIndex> result;
for(list<struct BookIndex>::iterator i=books.begin();i!=books.end();i++)
{
if(i->author == author)
result.push_back(*i);
}
result.sort();
return result;
}
private:
list<struct BookIndex> books;
};
int main(int argc, char *argv[])
{
Library library;
library.registerBooks(10);
list<struct BookIndex> books=library.searchBooks("angel");
for(list<struct BookIndex>::iterator i=books.begin();i!=books.end();i++)
{
cout<<*i<<endl;
}
return 0;
}
/*
book_aaa
angel
2012-01-01
100
100
book_bbb
blue
2000-11-02
136
14.5
book_ccc
angel
2010-03-12
56
123.5
book_ddd
angelfish
1989-04-14
1200
210
book_eee
angel
1991-11-23
456
123
book_fff
angel
2001-07-07
789
424
book_ggg
angel
2004-09-30
321
9
book_hhh
angel
2002-06-06
999
56
book_iii
angel
2008-08-08
654
81
book_jjj
angel
1998-09-09
111
99
*/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询