main169.cpp
#include
#include
#include
#include
#include
#include
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
static bool FillReview(Review &rr)
{
//rr=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
vector
//shared_ptr
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();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程题答案1610/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!