C++学习-Day-33

C++学习-Day-33

一、编程练习

  1. 16_1
#include<iostream>
#include<string>
#include<cctype>
int main()
{
    using std::string;
    using std::cin;
    using std::cout;
    using std::endl;
    string put;
    string temp;
    cout<<"Please enter a string (q to quit): ";
    while(cin>>put && put!="q")
    {
        string::reverse_iterator t=put.rbegin();
        string::iterator p=put.begin();
        int le=put.size();
        for(int i=0;i<le;i++)
        {
            temp[i]=*t;
            t++;
        }
        temp[le]='\0';
        for(int i=0;i<le/2;i++)
        {
            if(temp[i]==*p)
                p++;
            else
            {
                cout<<"false";
                exit(0);
            }
        }
        cout<<"true";
    }
}

  1. 16_3
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cctype>
#include<typeinfo>
int main()
{
    using namespace std;
    vector<string> get;
    vector<string>::const_iterator v;
    ifstream ifile;
    ifile.open("16_3.txt");
    string word;
    cout<<"Start to read data from file...\n"<<endl;
    while(ifile>>word)
    {
        get.push_back(word);
    }
    cout<<"The length of words list are "<<get.size()<<endl;
    cout<<"\nThe words list: \n"<<endl;
    for(v=get.begin();v!=get.end();v++)
    {
        cout<<*v<<" ";
        if(*v==",")
        cout<<endl;
    }
}

  1. 16_7
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> lotto(int n,int x)
{
    vector<int> temp;
    vector<int> a(x);
    if(x<=n)
    {
        for(int i=0;i<n;i++)
        {
            temp.push_back(i+1);
        }
        random_shuffle(temp.begin(),temp.end());
        copy(temp.begin(),temp.begin()+50,a.begin());
        sort(a.begin(),a.end());
        return a;
    }
    else
    {
        cout<<"invalid parameter x(x<=n)";
    }
}
int main()
{
    srand(time(0));
    vector<int> v;
    vector<int>::iterator ite;
    v=lotto(61,6);
    for(ite=v.begin();ite!=v.end();ite++)
    {
        cout<<*ite<<endl;
    }
}

  1. 16_8
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
int main()
{
    vector<string> Mat;
    string temp1;
    string mat;
    cout<<"Please Mat input his friends' name: "<<endl;
    while(cin>>temp1 && temp1!="q")
    {
        Mat.push_back(temp1);
    }
    vector<string>::iterator t1;
    sort(Mat.begin(),Mat.end());
    cout<<"Mat's friends list:\n";
    for(t1=Mat.begin();t1!=Mat.end();t1++)
    {
        cout<<*t1<<endl;
    }

    vector<string> Pat;
    string temp2;
    string pat;
    cout<<"Please Pat input her friends' name: "<<endl;
    while(cin>>temp2 && temp2!="q")
    {
        Pat.push_back(temp2);
    }
    vector<string>::iterator t2;
    sort(Pat.begin(),Pat.end());
    cout<<"Pat's friends list:\n";
    for(t2=Pat.begin();t2!=Pat.end();t2++)
    {
        cout<<*t2<<endl;
    }

    vector<string> Merge(Mat);
    Merge.insert(Merge.end(),Pat.begin()+1,Pat.end());
    sort(Merge.begin(),Merge.end());
    unique(Merge.begin(),Merge.end());
    vector<string>::iterator t3;
    cout<<"Mat's and Pat's friends list:\n";
    for(t3=Merge.begin();t3!=Merge.end();t3++)
    {
        cout<<*t3<<endl;
    }
}

  1. 16_9
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<ctime>
static long SIZE=1000000;
int main()
{
    using namespace std;
    srand(time(0));
    vector<int> vi0(SIZE);
    for(int i=0;i<SIZE;i++)
        vi0[i]=rand();
    vector<int> vi(vi0);
    list<int> li(SIZE);
    copy(vi0.begin(),vi0.end(),li.begin());
    clock_t t1=clock();
    sort(vi.begin(),vi.end());
    clock_t t2=clock();
    cout<<"Vector sorting times: ";
    cout<<double(t2-t1)/CLOCKS_PER_SEC<<" s\n";

    clock_t t3=clock();
    li.sort();
    clock_t t4=clock();
    cout<<"List sorting times: ";
    cout<<double(t4-t3)/CLOCKS_PER_SEC<<" s\n";

    copy(vi0.begin(),vi0.end(),li.begin());
    clock_t t5=clock();
    copy(li.begin(),li.end(),vi.begin());
    sort(vi.begin(),vi.end());
    copy(vi.begin(),vi.end(),li.begin());
    clock_t t6=clock();
    cout<<"List copying to vector method's sorting times: ";
    cout<<double(t6-t5)/CLOCKS_PER_SEC<<" s\n";

}

  1. 16_10
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<memory>
struct Review
{
    std::string title;
    int rating;
    double price;
};
bool operator<(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2);
bool worseThan(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2);
bool priceup(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2);
bool FillReview(Review & rr);
void ShowReview(const std::shared_ptr<Review> & rr);
int main()
{
    using namespace std;
    vector<shared_ptr<Review>> books;
    Review temp;
    while(FillReview(temp))
    {
       shared_ptr<Review> pt(new Review(temp));//shared_ptr must initialize by new pointer
       books.push_back(pt);
    }
    vector<shared_ptr<Review>> base_books(books.begin(),books.end());
    if(books.size()>0)
    {
        std::cout<<"Thank you.Please you choose a choice to display the books: \n"
                 <<"0:按原始顺序 1:按字母表顺序 2:按评级升序 3:按评级降序 4:按价格升序 5:按价格降序 6:退出\n";
        int choice;
        while(std::cin>>choice && choice!=6)
        {
            switch(choice)
            {
                case 0:copy(base_books.begin(),base_books.end(),books.begin());
                        break;
                case 1:sort(books.begin(),books.end());
                        break;
                case 2:sort(books.begin(),books.end(),worseThan);
                        break;
                case 3:sort(books.rbegin(),books.rend(),worseThan);
                        break;
                case 4:sort(books.begin(),books.end(),priceup);
                        break;
                case 5:sort(books.rbegin(),books.rend(),priceup);
                        break;
            }
            std::cout<<"Rating"<<"\t"<<"Title"<<"\t "<<"Price"<<std::endl;
            for_each(books.begin(),books.end(),ShowReview);
        }

    }
    else
        std::cout<<"No entries. ";
    std::cout<<"Bye.\n";
    return 0;
}
bool operator<(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2)
{
    if(r1->title<r2->title)
        return true;
    else if(r1->title==r2->title && r1->rating<r2->rating)
        return true;
    else
        return false;
}
bool worseThan(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2)
{
    if(r1->rating<r2->rating)
        return true;
    else
        return false;
}
bool priceup(const std::shared_ptr<Review> & r1,const std::shared_ptr<Review> & r2)
{
    if(r1->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;
    std::cout<<"Enter book price: ";
    std::cin>>rr.price;
    if(!std::cin)
        return false;
    while(std::cin.get()!='\n')
        continue;
    return true;
}
void ShowReview(const std::shared_ptr<Review> & rr)
{
    std::cout<<rr->rating<<"\t"<<"\""<<rr->title<<"\""<<"\t $"<<rr->price<<std::endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值