关闭

chapter16test10

标签: c++
167人阅读 评论(0) 收藏 举报
分类:

这一题说实话我都没有看的太明白。。。我没有做到题目要求的用指针参数来定义三个比较函数,我是按照书上原来的worsethan()修改的,我会再试试指针方法,有成果在回来改程序,我是一个自强不息的水货!fighting

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<memory>
using namespace std;
struct review
{
string title;
int rating;
double price;
};
bool price(const review &r1, const review &r2);
bool rate(const review &r1, const review &r2);
bool alpha(const review &r1,const review &r2);
bool file(shared_ptr<review> r);
void show(const review &r);
int main()
{
vector<review> book;
shared_ptr<review>temp(new review);
while (file(temp))
book.push_back(*temp);
int num = book.size();
if (num > 0)
{
int ch;
cout << "Thanks for your input,choose your way to display:\n";
cout << "1.Original 2.Alphabet 3.Uprating 4.Downrating 5.UpPrice 6.Downprice \n";
while (cin>>ch)
{
switch (ch)
{
case 1:for_each(book.begin(), book.end(), show); break;
case 2:sort(book.begin(), book.end(), alpha);
for_each(book.begin(), book.end(), show); break;
case 3:sort(book.begin(), book.end(), rate); 
for_each(book.begin(), book.end(), show); break;
case 4:
for_each(book.rbegin(), book.rend(), show); break;
case 5:sort(book.begin(), book.end(), price);
for_each(book.begin(), book.end(), show); break;
case 6:
for_each(book.rbegin(), book.rend(), show); break;
default:
break;
}
}
}
else
cout << "No info inputed.\n";
return 0;
}




bool alpha(const review &r1, const review &r2)
{
if (r1.title < r2.title)
return true;
else
return false;
}
bool rate(const review &r1, const review &r2)
{
if (r1.rating < r2.rating)
return true;
else
return false;
}
bool price(const review &r1, const review &r2)
{
if (r1.price < r2.price)
return true;
else
return false;
}
bool file(shared_ptr<review> r)
{
cout << "Enter book title(enter quit to quit):";
string t; getline(cin, t);
r->title = t; cout << "OK";
if (r->title == "quit")
return false;
cout << "Enter book rating :";
cin >> r->rating;
if (!cin)
return false;
cout << "Enter book price :";
cin >> r->price;
if (!cin)
return false;
while (cin.get() != '\n')
continue;
return true;
}
void show(const review &r)
{
cout << r.rating << "\t" << r.title << "\t" << r.price<<endl;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58245次
    • 积分:2992
    • 等级:
    • 排名:第12233名
    • 原创:250篇
    • 转载:0篇
    • 译文:1篇
    • 评论:25条
    最新评论