扫一扫下方二维码,关注本站官方公众号
获取永久解锁本站全部文章的验证码
还能不定期领现金红包

C++ Primer Plus 第六版 第十六章课后编程练习答案-Go语言中文社区

C++ Primer Plus 第六版 第十六章课后编程练习答案


1.

play.cpp

#include <iostream>
using namespace std;
#include <string>

bool IsHui(const string &str);

int main()
{
    cout << "Enter input(empty input to quit): ";
    string input;
    getline(cin, input);
    while (cin && input.size()>0)
    {
        if(IsHui(input))
            cout << "Yes.n";
        else
            cout << "No.n";
        cout << "Enter input(empty input to quit): ";
        getline(cin, input);
    }
    cout << "End.n";

    return 0;
}

bool IsHui(const string &str)
{
    string f(str.rbegin(), str.rend());//字符串反转
    return (str == f);
}

2.

本题要求在考虑空格、大小写和标点符号的情况下来测试回文,整体程序框架可以与上一题完全一样,只需要修改判断是否是回文的bool函数。

在判断是否是回文之前,我们必须把空格和标点符号去掉,然后把大小写归一化才行。对于去掉空格和标点符号,最简单的方法是调用cctype库中的erase()函数,该函数会删去指定的字符,那么我们可以使用isalpha()函数来判断是否是字母,如果不是字母,即空格或者各式各样的标点符号,则调用erase()函数将其删去。对于大小写的归一化,我们可以统一到一种格式上,比如按照题目中举的例子,就是统一都格式化成小写,那么就是使用tolower()函数。

#include <iostream>
using namespace std;
#include <string>
#include <cctype>

bool IsHui(const string &str);

int main()
{
    cout << "Enter input(empty input to quit): ";
    string input;
    getline(cin, input);
    while (cin && input.size()>0)
    {
        if(IsHui(input))
            cout << "Yes.n";
        else
            cout << "No.n";
        cout << "Enter input(empty input to quit): ";
        getline(cin, input);
    }
    cout << "End.n";

    return 0;
}

bool IsHui(const string &str)
{
    string str_test = str;//必须有
    for(auto i = str_test.begin(); i != str_test.end();) //i是str_test各元素所在的位置,*i是元素
    {
        if(!isalpha(*i))
        {
            i = str_test.erase(i);//该位置的元素去掉
            continue;
        }
        else
        {
            *i = tolower(*i);
            i++;
        }
    }
    string f(str_test.rbegin(), str_test.rend());
    return (str_test == f);
}

3.

play.cpp

//改自清单16.3
#include <iostream>
using namespace std;
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <fstream>

int main()
{
    const string filename = "wordlist.txt";
    vector<string> wordlist;
    ifstream fin;
    fin.open(filename.c_str());
    if(fin.is_open() == false)
    {
        cerr << "Can't open file.Bye.n";
        exit(EXIT_FAILURE);
    }
    string temp;
    while (fin >> temp)
        wordlist.push_back(temp);

//以下按清单16.3
    const int NUM = wordlist.size();
    srand(time(0));
    char play;
    cout << "Will you play a word game? <y/n>";
    cin >> play;
    play = tolower(play);
    while (play == 'y')
    {
        string target = wordlist[rand() % NUM];
        int length = target.length();
        string attempt(length, '-');
        string badchars;
        int guesses = 6;
        cout << "Guess my secret word. It has " << length
        << " letters, and you guessn"
        << "one letter at a time. You get " << guesses
        << " wrong guesses.n";
        cout << "Your word: " << attempt << endl;
        while (guesses > 0 && attempt != target)
        {
            char letter;
            cout << "Guess a letter: ";
            cin >> letter;
            if(badchars.find(letter) != string::npos
            || attempt.find(letter) != string::npos)
            {
                cout << "You already guessed that. Try again.n";
                continue;
            }
            int loc = target.find(letter);
            if(loc == string::npos)
            {
                cout << "Oh, bad guess!n";
                --guesses;
                badchars += letter;
            }
            else
            {
                cout << "Good guess!n";
                attempt[loc] = letter;
                loc = target.find(letter, loc + 1);
                while (loc != string::npos)
                {
                    attempt[loc] = letter;
                    loc = target.find(letter, loc + 1);
                }
            }
            cout << "Your word: " << attempt << endl;
            if(attempt != target)
            {
                if(badchars.length() > 0)
                    cout << "Bad choices: " << badchars << endl;
                cout << guesses << " bad guesses leftn";
            }
        }
        if(guesses > 0)
            cout << "That's right!n";
        else
            cout << "Sorry, the word is " << target << ".n";
        cout << "Will you play another? <y/n>";
        cin >> play;
        play = tolower(play);
    }
    cout << "Byen";

    return 0;
}

4.

play.cpp

//排序,去重
#include <iostream>
using namespace std;
#include <algorithm>

int reduce(long ar[], int n);

int main()
{
    long arr_test[10] = {156, 124, 23, 43, 35, 23, 124, 199, 210, 56};
    cout << "Original array:n";
    for(int i = 0; i < 10; i++)
        cout << arr_test[i] << " ";
    cout << endl;

    int newsize = reduce(arr_test, 10);//返回缩减后数组中的元素数目
    cout << "Newsize: " << newsize << endl;
    cout << "After reduction, array:n";
    for(int i = 0; i < newsize; i++)
        cout << arr_test[i] << " ";
    cout << endl;

    return 0;
}

int reduce(long ar[], int n)
{
    sort(ar, ar + n);//排序
    long * arr_new;
    arr_new = unique(ar, ar + n);//去重
    //用unique()函数返回结果区间结尾的特点,将该结尾指针地址赋给一个long指针,将该地址减去实参的数组名地址,即是缩减后的元素数目
    return arr_new - ar;
}

5.

本题是编写练习4的延伸,要求我们把原本的long数组修改为模板类,然后分别使用long实例和string实例来验证。

我们依然沿用编程练习4的思路和程序框架,首先把reduce()函数的实参进行修改,ar[]现在不再是long数组了,而是模板类T的数组,那么对应的在函数内部声明的new_ar指针的类型也就相应变成T模板类。

play.cpp

//改自第4题
#include <iostream>
using namespace std;
#include <algorithm>

template <class T>
int reduce(T ar[], int n);

int main()
{
    long arr_test[10] = {156, 124, 23, 43, 35, 23, 124, 199, 210, 56};
    cout << "Original array:n";
    for(int i = 0; i < 10; i++)
        cout << arr_test[i] << " ";
    cout << endl;
    int newsize = reduce(arr_test, 10);//返回缩减后数组中的元素数目
    cout << "Newsize: " << newsize << endl;
    cout << "After reduction, the long array:n";
    for(int i = 0; i < newsize; i++)
        cout << arr_test[i] << " ";
    cout << endl;

    cout << "The second test array: n";
    string arr_test2[10] = {"hah", "china", "play", "end", "hah", "play", "speak", "play", "zero", "apple"};
    cout << "Original array:n";
    for(int i = 0; i < 10; i++)
        cout << arr_test2[i] << " ";
    cout << endl;
    int newsize2 = reduce(arr_test2, 10);//返回缩减后数组中的元素数目
    cout << "Newsize: " << newsize2 << endl;
    cout << "After reduction, the long array:n";
    for(int i = 0; i < newsize2; i++)
        cout << arr_test2[i] << " ";
    cout << endl;

    return 0;
}

template <class T>
int reduce(T ar[], int n)
{
    sort(ar, ar + n);//排序
    T * arr_new;
    arr_new = unique(ar, ar + n);//去重
    //用unique()函数返回结果区间结尾的特点,将该结尾指针地址赋给一个long指针,将该地址减去实参的数组名地址,即是缩减后的元素数目
    return arr_new - ar;
}

6.

本题要求我们对程序清单12.12进行修改,使用STL queue模板类来代替Queue类。我们可以沿用程序清单12.12的程序框架,把"queue.h"换成<queue>即可。同时将queue.h内声明的Customer类放在同一个文件中声明和定义,放弃Queue类,转而使用queue<Customer>模板类来代替。

所以我们只需要把main()函数里面的Queue类声明改为queue<Customer>类声明即可.

play.cpp

//改自清单12.12
#include <iostream>
using namespace std;
#include <queue>
#include <cstdlib>
#include <ctime>

class Customer
{
    long arrive;
    int processtime;
public:
    Customer() {arrive = processtime = 0;}
    void set(long when) {processtime = rand()%3 + 1; arrive = when;}
    long when() const { return arrive;}
    int ptime() const { return processtime;}
};


const int MIN_PER_HR = 60;
bool newcustomer(double x);

int main()
{
    srand(time(0));
    cout << "Case Study: Bank of Heather Automatic Tellern";
    cout << "Enter maximum size of queue: ";
    int qs;
    cin >> qs;
    queue<Customer> line;

    cout << "Enter the number of simulation hours: ";
    int hours;
    cin >> hours;
    long cyclelimit = MIN_PER_HR * hours;

    cout << "Enter the average number of customers per hour: ";
    double perhour;
    cin >> perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HR / perhour;

    Customer temp;
    long turnaways = 0;
    long customers = 0;
    long served = 0;
    long sum_line = 0;
    int wait_time = 0;
    long line_wait = 0;
    for(int cycle = 0; cycle < cyclelimit; cycle++)
    {
        if(newcustomer(min_per_cust))
        {
            if(qs == line.size())
                turnaways++;
            else
            {
                customers++;
                temp.set(cycle);
                line.push(temp);
            }
        }
        if(wait_time <= 0 && !line.empty())
        {
            temp = line.front();
            wait_time = temp.ptime();
            line_wait += cycle - temp.when();
            served++;
            line.pop();
        }
        if(wait_time > 0)
            wait_time--;
        sum_line += line.size();
    }

    if(customers > 0)
    {
        cout << "customers accepted: " << customers << endl;
        cout << "  customers served: " << served << endl;
        cout << "         turnaways: " << turnaways << endl;
        cout << "average queue size: " ;
        cout.precision(2);
        cout.setf(ios_base::fixed, ios_base::floatfield);
        cout << (double)sum_line / cyclelimit << endl;
        cout << " average wait time: "
        << (double)line_wait / served << " minutesn";
    }
    else
        cout << "No customers!n";
    cout << "Done!n";

    return 0;
}

bool newcustomer(double x)
{
    return (rand() * x / RAND_MAX < 1);
}

7.

play.cpp

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
vector<int> Lotto(int cnum, int rnum);

int main()
{
    vector<int> winners;
    winners = Lotto(51, 6);
    cout << "Winner's number:n";
    for(auto i = winners.begin(); i != winners.end(); i++)
        cout << *i << " ";
    cout << endl;

    return 0;
}

vector<int> Lotto(int cnum, int rnum)
{
    vector<int> ticket(cnum);
    for(int i = 0; i < cnum; i++)
        ticket[i] = i + 1;
    random_shuffle(ticket.begin(), ticket.end());
    vector<int> choose(rnum);
    for(int i = 0; i < rnum; i++)
        choose[i] = ticket[i];
    sort(choose.begin(), choose.end());

    return choose;
}

8.

play.cpp

#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <algorithm>

int main()
{
    vector<string> mat_f, pat_f, total_f;//3个容器
    cout << "Enter Mat's friends(empty line to quit):n";
    string temp;
    while (getline(cin, temp))
    {
        mat_f.push_back(temp);
        total_f.push_back(temp);
        if(temp[0] == '')
            break;
    }
    cout << "The following are Mat's friends:n";
    sort(mat_f.begin(), mat_f.end());
    for(auto i = mat_f.begin(); i != mat_f.end(); i++)
        cout << *i << endl;
    cout << endl << endl;

    cout << "Enter Pat's friends(empty line to quit):n";
    while (getline(cin, temp))
    {
        pat_f.push_back(temp);
        total_f.push_back(temp);
        if(temp[0] == '')
            break;
    }
    cout << "The following are Pat's friends:n";
    sort(pat_f.begin(), pat_f.end());
    for(auto i = pat_f.begin(); i != pat_f.end(); i++)
        cout << *i << endl;
    cout << endl << endl;

    sort(total_f.begin(), total_f.end());
    unique(total_f.begin(), total_f.end());
    cout << "There are " << total_f.size() << " friends to be invited in total:n";
    for(auto i = total_f.begin(); i != total_f.end(); i++)
        cout << *i << endl;
    cout << endl << endl;

    return 0;
}

9.

play.cpp

#include <iostream>
using namespace std;
#include <algorithm>
#include <ctime>
#include <vector>
#include <list>
#include <string>

const int NUM = 10000000;
int main()
{
    cout << "-------begin--------" << endl;
    srand(time(0));
    vector<int> vi0(NUM);
    for(int i = 0; i < NUM; i++)
        vi0[i] = rand();

    vector<int> vi(vi0);//创建vector<int>对象vi和list<int>对象li,它们的长度都和初始值与vi0相同
    list<int> li(vi0.begin(), vi0.end());
    cout << "Measure the sort() algorithm's running time for vector<int>:n";
    clock_t vector_start = clock();
    sort(vi.begin(), vi.end());
    clock_t vector_end = clock();
    cout << "The time is " << (double)(vector_end - vector_start) / CLOCKS_PER_SEC << "s.n";

    cout << "Measure the sort() algorithm's running time for list<int>:n";
    clock_t list_start = clock();
    li.sort();
    clock_t list_end = clock();
    cout << "The time is " << (double)(list_end - list_start) / CLOCKS_PER_SEC << "s.n";

    cout << "Measure the copy() and sort() algorithm's running time:n";
    copy(vi0.begin(), vi0.end(), li.begin());//li重置为排序的vi0的内容
    clock_t copy_start = clock();
    copy(li.begin(), li.end(), vi.begin());//将li的内容复制到vi中
    sort(vi.begin(), vi.end());//对vi进行排序
    copy(vi.begin(), vi.end(), li.begin());//并将结果复制到li中
    clock_t copy_end = clock();
    cout << "The time is " << (double)(copy_end - copy_start) / CLOCKS_PER_SEC << "s.n";

    return 0;
}

10.

play.cpp

//改自清单16.9
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <string>

struct Review
{
    string title;
    int rating;
    double price;
};

//按题,不使用vector<Review>来存储输入,而使用vector<shared_ptr<Review>>
bool operator <(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
//添加betterthan(),expensivethan(),cheapthan()函数,来让用户选择按评级降序显示,按价格降序显示,按价格升序显示
bool worsethan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool betterthan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool expensivethan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool cheapthan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2);
bool FillReview(Review &rr);
void ShowReview(const shared_ptr<Review> &rr);

int main()
{
    vector<shared_ptr<Review>> books;
    Review temp;
    while (FillReview(temp))
    {
        shared_ptr<Review> newtemp(new Review(temp));
        books.push_back(newtemp);
    }
    if(books.size() > 0)
    {
        cout << "Thank you. You entered the following "
        << books.size() << " ratings:n"
        << "RatingtBookn";
        for_each(books.begin(), books.end(), ShowReview);

        cout << "Please choose a type to show(0 for original order, 1 for alphabet order, 2 for rating ascend order, n3 for rating discend order, 4 for price ascend order, 5 for price discend order, q for quit): ";
        int input;
        while (cin >> input)
        {
            switch (input)
            {
                case 0:
                    break;
                case 1:
                    sort(books.begin(), books.end());
                    break;
                case 2:
                    sort(books.begin(), books.end(), worsethan);
                    break;
                case 3:
                    sort(books.begin(), books.end(), betterthan);
                    break;
                case 4:
                    sort(books.begin(), books.end(), cheapthan);
                    break;
                case 5:
                    sort(books.begin(), books.end(), expensivethan);
                    break;
                default:
                    break;
            }
            for_each(books.begin(), books.end(), ShowReview);
            cout << "Please choose a type to show(0 for original order, 1 for alphabet order, 2 for rating ascend order, n3 for rating discend order, 4 for price ascend order, 5 for price discend order, q for quit): ";
        }

    }
    else
        cout << "No entries. ";
    cout << "Bye.n";

    return 0;
}

bool operator <(const shared_ptr<Review> &r1, const 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 shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
    if(r1->rating < r2->rating)
        return true;
    else
        return false;
}

bool betterthan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
    if(r1->rating > r2->rating)
        return true;
    else
        return false;
}

bool expensivethan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
    if(r1->price > r2->price)
        return true;
    else
        return false;
}

bool cheapthan(const shared_ptr<Review> &r1, const shared_ptr<Review> &r2)
{
    if(r1->price < r2->price)
        return true;
    else
        return false;
}

bool FillReview(Review & rr)
{
    cout << "Enter book title(q to quit): ";
    getline(cin, rr.title);
    if(rr.title == "q")
        return false;
    cout << "Enter book rating: ";
    cin >> rr.rating;
    cout << "Enter book price: ";
    cin >> rr.price;
    if(!cin)
        return false;
    while (cin.get() != 'n')
        continue;
    return true;
}

void ShowReview(const shared_ptr<Review> &rr)
{
    cout << rr->rating << "t" << rr->price << "t" << rr->title << endl;
}

版权声明1:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_39938666/article/details/105540770
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 17:15
  • 阅读 ( 299 )

0 条评论

请先 登录 后评论

官方社群