C++primer plus第六版课后编程题答案16.10

main169.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <memory>
using namespace std;
static 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(shared_ptr<Review> *rr)
static bool FillReview(Review &rr)
{
	//rr=new Review;
	shared_ptr<Review> ps(new Review);//创建一个shared_ptr指针
	cout<<"\nEnter book title (q to quit):";
	getline(cin,ps->title);
	//if(rr.title[0]=='q'&&sizeof(rr.title)==1)//这里是ps好吧,我就奇了怪了
	if(ps->title=="q")
		return false;
	cout<<"\nEnter the book rating:";
	cin>>ps->rating;
	cout<<"\nEnter the price:";
	cin>>ps->price;
	if(!cin)
		return false;
	while(cin.get()!='\n')
		continue;
	rr=*ps;//p667  ps是一个指针,对其解除*
	return true;
};
static void output(const Review &rr)
{
	cout<<rr.rating<<"\t"<<rr.title<<"\t"<<rr.price<<endl;
}



void main1610()
{
	//vector<shared_ptr<Review> > books;
	vector<Review> books;
	//shared_ptr<Review> *temp;
	Review temp;
	while(FillReview(temp))
		books.push_back(temp);
	if(books.size()>0)
	{
		cout<<"Thank you ,You entered the following "
			<<books.size()<<" ratings:"<<endl
			<<"Rating\tBook\tPrices"<<endl;
		for_each(books.begin(),books.end(),output);
	}
	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;//我只演示一个逆序输出f
	char select;
	cin>>select;
	while(select!='q')
	{
		/*
		while(cin.get()!='\n')
			continue;*/
		switch(select)
		{
			case 't':
				sort(books.begin(),books.end(),sortT),
				cout<<"sort by title:"<<endl;
				for_each(books.begin(),books.end(),output),
				cout<<endl;
				break;
			case 'f':
				sort(books.begin(),books.end(),sortT),
				cout<<"sort by title:"<<endl;
				//for_each(books.rend(),books.rbegin(),output),
				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;//我只演示一个逆序输出f
			
			cin>>select;
	
	}

	cout<<"Bye"<<endl;
	cin.get();




}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值