C++ primer plus 编程练习16.10

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Review
{
	std::string title;
	int rating;
	int price;
};
bool operator<(const shared_ptr<Review> & rl, const shared_ptr<Review> & r2);
//bool operator<(const Review& rl, const Review& r2);
bool worseThan(const shared_ptr<Review>& rl, const shared_ptr<Review>& r2);
bool priceThan(const shared_ptr<Review>& rl, const shared_ptr<Review>& r2);
bool FillReview(Review & rr);
void ShowReview(const shared_ptr<Review>& rr);
int main() {
	//vector<Review> books;
	Review temp;
	//auto sp = std::make_shared<Review>(temp);
	//std::shared_ptr<Review> sp3(sp);
	vector<shared_ptr<Review>> books;
	//std::shared_ptr<A> sp3(sp31);
	//vector<shared_ptr<Review>> books = new ;
	
	while (FillReview(temp)) {//指针不行
		auto sp = std::make_shared<Review>(temp);
		std::shared_ptr<Review> sp3(sp);
		books.push_back(sp3);
	}
		
	if (books.size() > 0) {
		cout << "Thank you.you entered the following " << books.size() <<" ratings : \n"
		<< "Rating\tBook\n";
		cout << "choose how to showReview: (1.原始顺序。2.按字母表顺序。3.按评级升序。4.按评级降序。5.按价格升序。6.按价格降序。7.退出)" << endl;
		int choice;
		vector<shared_ptr<Review>> new_books;
		cin >> choice;
		while (choice != 7) {
			new_books = books;
			switch (choice) {
			case 1:
				cout << "原始顺序:\n";
				for_each(new_books.begin(), new_books.end(), ShowReview);
				break;
			case 2:
				sort(new_books.begin(), new_books.end());
				cout << "字母表顺序:\nRating\tBook\n";
				for_each(new_books.begin(), new_books.end(), ShowReview);
				break;
			case 3:
				sort(new_books.begin(), new_books.end(), worseThan);
				cout << "评级升序 : \nRating \tBook\n";
				for_each(new_books.begin(), new_books.end(), ShowReview);
				break;
			case 4:
				sort(new_books.begin(), new_books.end(), worseThan);
				cout << "评级降序 : \nRating \tBook\n";
				for_each(new_books.rbegin(), new_books.rend(), ShowReview);
				break;
			case 5:
				sort(new_books.begin(), new_books.end(), priceThan);
				cout << "价格升序 : \nRating \tBook\n";
				for_each(new_books.begin(), new_books.end(), ShowReview);
				break;
			case 6:
				sort(new_books.begin(), new_books.end(), priceThan);
				cout << "价格降序 : \nRating \tBook\n";
				for_each(new_books.rbegin(), new_books.rend(), ShowReview);
				break;
			default: cout << "That's not a choice;";
			}
		cout << "choose how to showReview: (1.原始顺序。2.按字母表顺序。3.按评级升序。4.按评级降序。5.按价格升序。6.按价格降序。7.退出)" << endl;
		cin >> choice;
		}
	}
	else
		cout << "No entries.";
	cout << "Bye,\n";
	return 0;
}
bool operator<(const shared_ptr<Review>& rl, const shared_ptr<Review>& r2) {
	if (rl->title < r2->title)
		return true; 
	else if (rl->title == r2->title && rl->rating < r2->rating)
		return true;
	else
		return false;
}
bool worseThan(const shared_ptr<Review>& rl, const shared_ptr<Review>& r2) {
	if (rl->rating < r2->rating)
		return true;
	else
		return false;
}
bool priceThan(const shared_ptr<Review>& rl, const shared_ptr<Review>& r2) {
	if (rl->price < r2->price)
		return true;
	else
		return false;
}
bool FillReview(Review& rr) {
	std::cout << "Enter book title (quit to quit):";
	std::getline(std::cin, rr.title);
	if (rr.title == "quit")
		return false; 
	std::cout << "Enter book rating:";
	std::cin >> rr.rating;
	if (!std::cin)
		return false;// get rid of rest of input line
	while (std::cin.get() != '\n')
		continue;
	std::cout << "Enter book price:";
	std::cin >> rr.price;
	if (!std::cin)
		return false;// get rid of rest of input line
	while (std::cin.get() != '\n')
		continue;
	return true;
}
void ShowReview(const shared_ptr<Review>& rr) {
	std::cout << rr->rating << "\t" << rr->title << "\t" << rr->price << std::endl;
}

 知识学习:

bool FillReview(Review & rr);函数只能传结构,不能传指针,传指针的话,vector存的都是一个指针。

while (FillReview(temp)) {//指针不行
        auto sp = std::make_shared<Review>(temp);
        std::shared_ptr<Review> sp3(sp);
        books.push_back(sp3);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值