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

本文深入探讨了C++中的类定义与异常处理机制,通过具体案例展示了如何使用类和异常来增强代码的健壮性和可维护性。文章首先介绍了TV和Remote类的设计,包括成员函数和枚举类型的运用,随后通过mean类展示了如何自定义异常类并处理运行时错误,最后讲解了Sales类中索引异常的处理方法。

1.

tv.h

//改自清单15.1
#ifndef TV_H_
#define TV_H_
class Remote;

class Tv
{
    friend class Remote;
public:
    enum { Off, On };
    enum { MinVal, MaxVal = 20 };
    enum { Antenna, Cable };
    enum { TV, DVD };

    Tv(int s = Off, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
    void onoff() { state = (state == On) ? Off : On; }
    bool ison() const { return state == On; }
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
    void set_input() { input = (input == TV) ? DVD : TV; }
    void settings() const;
    void change_r_mode(Remote & r);
private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;
};

class Remote
{
    friend class Tv;
private:
    int mode;
    int r_mode;
    enum { Regular, Interact};
public:
    Remote(int m = Tv::TV, int f = Regular) : mode(m), r_mode(f) {}
    bool volup(Tv & t) { return t.volup(); }
    bool voldown(Tv & t) { return t.voldown(); }
    void onoff(Tv & t) { t.onoff(); }
    void chanup(Tv & t) { t.chanup(); }
    void chandown(Tv & t) { t.chandown(); }
    void set_chan(Tv & t, int c) { t.channel = c; }
    void set_mode(Tv & t) { t.set_mode(); }
    void set_input(Tv & t) { t.set_input(); }
    void show_r_mode() const { std::cout << "Now the remote mode is " << (r_mode == Regular ? "Regular" : "Interact") << ".\n";}
};

inline void Tv::change_r_mode(Remote & r)
{
    if (ison())
    {
        if (r.r_mode == Remote::Regular)
            r.r_mode = Remote::Interact;
        else
            r.r_mode = Remote::Regular;
        r.show_r_mode();
    }
    else
    {
        std::cout << "The tv is off, please turn it on!\n";
    }
}

#endif

tv.cpp

// 清单15.2
#include <iostream>
#include "tv.h"

bool Tv::volup()
{
    if (volume < MaxVal)
    {
        volume++;
        return true;
    }
    else
        return false;
}

bool Tv::voldown()
{
    if (volume > MinVal)
    {
        volume--;
        return true;
    }
    else
        return false;
}

void Tv::chanup()
{
    if (channel < maxchannel)
        channel++;
    else
        channel = 1;
}

void Tv::chandown()
{
    if (channel > 1)
        channel--;
    else
        channel = maxchannel;
}

void Tv::settings() const
{
    using std::cout;
    using std::endl;
    cout << "TV is " << (state == Off ? "Off" : "On") << endl;
    if (state == On)
    {
        cout << "Volume setting = " << volume << endl;
        cout << "Channel setting = " << channel << endl;
        cout << "Mode = " << (mode == Antenna ? "antenna" : "cable") << endl;
        cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
    }
}

play.cpp

//改自清单15.3
#include <iostream>
#include "tv.h"

int main()
{
    using std::cout;
    Tv s42;
    Remote grey;
    cout << "Initial settings for 42\" TV:\n";
    s42.settings();
    grey.show_r_mode();
    s42.change_r_mode(grey);
    s42.onoff();
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.chanup();
    cout << "\nAdjusted settings for 42\" TV:\n";
    s42.settings();

    s42.change_r_mode(grey);

    grey.set_chan(s42, 10);
    grey.volup(s42);
    grey.volup(s42);
    cout << "\n42\" settings after using remote:\n";
    s42.settings();

    s42.change_r_mode(grey);

    Tv s58(Tv::On);
    s58.set_mode();
    grey.set_chan(s58, 28);
    cout << "\n58\" settings:\n";
    s58.settings();
    grey.show_r_mode();
    s58.change_r_mode(grey);

    return 0;
}

2.

mean.h

#ifndef MEAN_H
#define MEAN_H
#include <stdexcept>
#include <string>

class bad_hmean : public logic_error
{
    double v1;
    double v2;
    string name;
public:
    explicit bad_hmean(const string &n = "hmean", const string &s = "Error in hmean()\n",
                       double a = 0, double b = 0);
    void mesg();
    const char * what() { return "bad arguments in hmean()\n";}
    virtual ~bad_hmean() throw() {}
};

bad_hmean::bad_hmean(const string &n, const string &s, double a, double b)
        : name(n), logic_error(s), v1(a), v2(b)
{}

inline void bad_hmean::mesg()
{
    cout << "hmean(" << v1 << ", " << v2 << "): " << "invalid arguments: a = -b\n";
}

class bad_gmean : public logic_error
{
    string name;
public:
    double v1;
    double v2;
    explicit bad_gmean(const string &n = "gmean", const string &s = "Error in gmean()\n",
                       double a = 0, double b = 0);
    const char * mesg();
    const char * what() { return "bad arguments in gmean()\n";}
    virtual ~bad_gmean() throw() {}
};

bad_gmean::bad_gmean(const string &n, const string &s, double a, double b)
        : name(n), logic_error(s), v1(a), v2(b)
{}

inline const char * bad_gmean::mesg()
{
    return "gmean() arguments should be >=0\n";
}

#endif

mean.cpp

//改自清单15.10、15.11
#include <iostream>
using namespace std;
#include <string>
#include "mean.h"
#include <cmath>

double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    double x, y, z;
    cout << "Enter two numbers: ";
    while (cin >> x >> y)
    {
        try{
            z = hmean(x, y);
            cout << "Harmonic mean of " << x << " and " << y
                 << " is " << z << endl;
            cout << "Geometric mean of " << x << " and " << y
                 << " is " << gmean(x, y) << endl;
            cout << "Enter next set of numbers <q to quit>: ";
        }
        catch(bad_hmean &bg)
        {
            cout << bg.what();
            cout << "Error message:\n";
            bg.mesg();
            cout << "Try again.\n";
            continue;
        }
        catch (bad_gmean &hg)
        {
            cout << hg.what();
            cout << "Error message:\n";
            cout << hg.mesg();
            cout << "Values used: " << hg.v1 << ", " << hg.v2 << endl;
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }
    }

    cout << "Bye!\n";
    return 0;
}

double hmean(double a, double b)
{
    if(a == -b)
        throw bad_hmean("hmean", "Error in hmean()\n", a, b);
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
    if(a < 0 || b < 0)
        throw bad_gmean("gmean", "Error in gmean()\n", a, b);
    return sqrt(a * b);
}

3.

mean.h

#ifndef MEAN_H
#define MEAN_H
#include <stdexcept>
#include <string>

class bad : public logic_error
{
public:
    double v1;
    double v2;
    string name;
    explicit bad(const string &n, const string &s, double a = 0, double b = 0);
    void mesg();
    virtual ~bad() throw() {}
};

bad::bad(const string &n, const string &s, double a, double b)
        : name(n), logic_error(s), v1(a), v2(b)
{}

inline void bad::mesg()
{
    cout << "Error happened!\n";
}

class bad_hmean : public bad
{
public:
    explicit bad_hmean(const string &n = "hmean", const string &s = "Error in hmean()\n",
                       double a = 0, double b = 0);
    void mesg();
    const char * what() { return "bad arguments in hmean()\n";}
    virtual ~bad_hmean() throw() {}
};

bad_hmean::bad_hmean(const string &n, const string &s, double a, double b)
        : bad(n, s, a, b)
{}

inline void bad_hmean::mesg()
{
    cout << "hmean(" << v1 << ", " << v2 << "): " << "invalid arguments: a = -b\n";
}

class bad_gmean : public bad
{
public:
    explicit bad_gmean(const string &n = "gmean", const string &s = "Error in gmean()\n",
                       double a = 0, double b = 0);
    const char * mesg();
    const char * what() { return "bad arguments in gmean()\n";}
    virtual ~bad_gmean() throw() {}
};

bad_gmean::bad_gmean(const string &n, const string &s, double a, double b)
        : bad(n, s, a, b)
{}

inline const char * bad_gmean::mesg()
{
    return "gmean() arguments should be >=0\n";
}

#endif

mean.cpp

//改自清单15.10、15.11
#include <iostream>
using namespace std;
#include <string>
#include "mean.h"
#include <cmath>

double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    double x, y, z;
    cout << "Enter two numbers: ";
    while (cin >> x >> y)
    {
        try{
            z = hmean(x, y);
            cout << "Harmonic mean of " << x << " and " << y
                 << " is " << z << endl;
            cout << "Geometric mean of " << x << " and " << y
                 << " is " << gmean(x, y) << endl;
            cout << "Enter next set of numbers <q to quit>: ";
        }
        catch (bad &b)
        {
            b.mesg();
            if(b.name == "hmean")
            {
                cout << ((bad_hmean &)b).what();
                cout << "Error message: ";
                ((bad_hmean &)b).mesg();
                cout << "Sorry, you don't get to play any more.\n";
                break;
            }
            else if (b.name == "gmean")
            {
                cout << ((bad_gmean &)b).what();
                cout << "Error message: ";
                cout << ((bad_gmean &)b).mesg();
                cout << "Values used: " << ((bad_gmean &)b).v1 << ", " <<
                ((bad_gmean &)b).v2 << endl;
                cout << "Sorry, you don't get to play any more.\n";
                break;
            }
            else
            {
                cout << "Input error.\nTerminated.\n";
                exit(EXIT_FAILURE);
            }
        }
    }

    cout << "Bye!\n";
    return 0;
}

double hmean(double a, double b)
{
    if(a == -b)
        throw bad_hmean("hmean", "Error in hmean()\n", a, b);
    return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
    if(a < 0 || b < 0)
        throw bad_gmean("gmean", "Error in gmean()\n", a, b);
    return sqrt(a * b);
}

4.

sales.h

//清单15.14
#include <iostream>
#include <stdexcept>
#include <string>

class Sales
{
public:
    enum { MONTHS = 12 };;
    class bad_index : public std::logic_error
    {
    private:
        int bi;
    public:
        explicit bad_index(int ix, const std::string & s = "Index error in Sales object\n");
        int bi_val() const { return bi; }
        virtual ~bad_index() throw() {}
    };
    explicit Sales(int yy = 0);
    Sales(int yy, const double * gr, int n);
    virtual ~Sales() {}
    int Year() const { return year; }
    virtual double operator[](int i) const;
    virtual double & operator[](int i);
private:
    double gross[MONTHS];
    int year;
};

class LabeledSales : public Sales
{
public:
    class nbad_index : public Sales::bad_index
    {
    private:
        std::string lbl;
    public:
        nbad_index(const std::string & lb, int ix, const std::string & s = "Index error in LabeledSales object\n");
        const std::string & label_val() const { return lbl; }
        virtual ~nbad_index() throw() {}
    };
    explicit LabeledSales(const std::string & lb = "none", int yy = 0);
    LabeledSales(const std::string & lb, int yy, const double * gr, int n);
    virtual ~LabeledSales() {}
    const std::string & Label() const { return label; }
    virtual double operator[](int i) const;
    virtual double & operator[](int i);
private:
    std::string label;
};

sale.cpp

// 清单15.15
#include "sales.h"
using std::string;

Sales::bad_index::bad_index(int ix, const string & s) : std::logic_error(s), bi(ix)
{
}

Sales::Sales(int yy)
{
    year = yy;
    for (int i = 0; i < MONTHS; i++)
    {
        gross[i] = 0;
    }
}

Sales::Sales(int yy, const double * gr, int n)
{
    year = yy;
    int lim = (n < MONTHS) ? n : MONTHS;
    int i;
    for (i = 0; i < lim; ++i)
    {
        gross[i] = gr[i];
    }
    for (; i < MONTHS; ++i)
    {
        gross[i] = 0;
    }
}

double Sales::operator[](int i) const
{
    if (i < 0 || i >= MONTHS)
    {
        throw bad_index(i);
    }
    return gross[i];
}

double & Sales::operator[](int i)
{
    if (i < 0 || i >= MONTHS)
    {
        throw bad_index(i);
    }
    return gross[i];
}

LabeledSales::nbad_index::nbad_index(const string & lb, int ix, const string & s) : Sales::bad_index(ix, s)
{
    lbl = lb;
}

LabeledSales::LabeledSales(const string & lb, int yy) : Sales(yy)
{
    label = lb;
}

LabeledSales::LabeledSales(const string & lb, int yy, const double * gr, int n) : Sales(yy, gr, n)
{
    label = lb;
}

double LabeledSales::operator[](int i) const
{
    if (i < 0 || i >= MONTHS)
    {
        throw nbad_index(Label(), i);
    }
    return Sales::operator[](i);
}

double & LabeledSales::operator[](int i)
{
    if (i < 0 || i >= MONTHS)
    {
        throw nbad_index(Label(), i);
    }
    return Sales::operator[](i);
}

play.cpp

// 改自清单15.16
#include <iostream>
#include "sales.h"

int main()
{
    using namespace std;
    double vals1[12] =
            {
                    1220, 1100, 1122, 2212, 1232, 2334,
                    2884, 2393, 3302, 2922, 3002, 3544
            };
    double vals2[12] =
            {
                    12, 11, 22, 21, 32, 34,
                    28, 29, 33, 29, 32, 35
            };

    Sales sales1(2011, vals1, 12);
    LabeledSales sales2("Blogstar", 2012, vals2, 12);
    Sales::bad_index * S;
    LabeledSales::nbad_index * L;

    cout << "First try block:\n";
    try
    {
        int i;
        cout << "Year = " << sales1.Year() << endl;
        for (i = 0; i < 12; ++i)
        {
            cout << sales1[i] << ' ';
            if (i % 6 == 5)
                cout << endl;
        }
        cout << "Year = " << sales2.Year() << endl;
        cout << "Label = " << sales2.Label() << endl;
        for (i = 0; i <= 12; ++i)
        {
            cout << sales2[i] << ' ';
            if (i % 6 == 5)
                cout << endl;
        }
        cout << "End of try block 1.\n";
    }
    catch (logic_error & bad)
    {
        cout << bad.what();
        if (L = dynamic_cast<LabeledSales::nbad_index *>(&bad))
        {
            cout << "Company: " << L->label_val() << endl;
            cout << "Bad index: " << L->bi_val() << endl;
        }
        else if (S = dynamic_cast<Sales::bad_index *>(&bad))
        {
            cout << "Bad index: " << S->bi_val() << endl;
        }
    }
    cout << "\nNext try block:\n";
    try
    {
        sales2[2] = 37.5;
        sales1[20] = 23345;
        cout << "End of try block 2.\n";
    }
    catch (logic_error & bad)
    {
        cout << bad.what();
        if (L = dynamic_cast<LabeledSales::nbad_index *>(&bad))
        {
            cout << "Company: " << L->label_val() << endl;
            cout << "Bad index: " << L->bi_val() << endl;
        }
        else if (S = dynamic_cast<Sales::bad_index *>(&bad))
        {
            cout << "Bad index: " << S->bi_val() << endl;
        }
    }
    cout << "done\n";

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值