#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);
}