16.10

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<memory>
#include<cctype>
using namespace std;

struct Review{
	string title;
	int rating;
	double price;
};

static bool sortT(const Review &r1,const Review &r2)
{
	if(r1.title<r2.title)
	   return true;
	else
		return false;
};
static bool sortR(const Review &r1,const Review &r2)
{
	if(r1.rating<r2.rating)
		return true;
	else 
		return false;
}

static bool sortP(const Review &r1,const Review & r2)
{
	if(r1.price<r2.price)
		return true;
	else 
		return false;
}

static bool FillReview(Review &rr)
{
	shared_ptr<Review> ps(new Review);
	cout<<"\nEnter book title (q to quit): ";
	getline(cin,ps->title);
	if(ps->title=="q")
	
		return false;
	cout<<"\n enter the book rating: ";
	cin>>ps->rating;
	cout<<"\n enter the price:";
	cin>>ps->price;
	if(!cin)
		return false;
	while(cin.get()!='\n')
		continue;
	rr=*ps;
	return true;
};
static void output(const Review &rr)
{
	cout<<rr.rating<<"  "<<rr.title<< " "<<rr.price<<endl;
}
void main()
{
	vector<Review>books;
	Review temp;
	while( FillReview(temp))
		books.push_back(temp);
	if(books.size ()>0)
	{
		cout<<"you enter following: "<<books.size()<<"  ratings: "<<endl;
		for_each(books.begin(),books.end(),output);
	}
	cout<<"please select the sort: "<<endl;
	char select;
	cin>>select;
	while(select!='q')
	{
		switch(select)
		{
		case't':
			
		case'f':
			sort(books.begin(),books.end(),sortT);
			cout<<"sort by title:"<<endl;
			for_each(books.rbegin(),books.rend(),output);
				cout<<endl;
			break;
		case'r':
			sort(books.begin(),books.end(),sortR);
			cout<<"sort by rating:"<<endl;
			for_each(books.begin(),books.end(),output);
				cout<<endl;
			break;
		case'p':
			sort(books.begin(),books.end(),sortP);
			cout<<"sort by Prices:"<<endl;
			for_each(books.begin(),books.end(),output);
				cout<<endl;
			break;
		case'q':
		default:
			break;
		}
		cout<<"please select the sort: "<<endl<<"t.sort by title       r.sort by rating"<<endl  
        <<"p.sort by price       f.sort by title reserve"<<endl  
        <<"q.quit"<<endl;
		cin>>select;
	}
	cout<<"bye"<<endl;
	cin.get();
}

猜你喜欢

转载自blog.csdn.net/qq_36687355/article/details/80215718