第十五章 编程练习

编程练习1

//tv.h
#ifndef TV_H_
#define TV_H_

#include <iostream>
class Tv
{
public:
    friend class Remote;
    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 ChangeReMode(Remote & r);
private:
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;
    int input;
};

class Remote
{
private:
    int mode;
    int ReMode;
public:
    friend class Tv;
    enum {Normal, Interactive};
    Remote(int m = Tv::TV, int n = Normal):mode(m), ReMode(n) {}
    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 ShowReMode() const { std::cout << "Remote now is at " << (ReMode == Normal? "Normal" : "Interactive") << std::endl;}
};

#endif
//tv.cpp
#include "tv.h"
#include <iostream>

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;
    }
}

void Tv::ChangeReMode(Remote & r)
{
    if (state == On)
        r.ReMode ^=1;
}
//use_tv.cpp
#include <iostream>
#include "tv.h"

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

    Remote grey;

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

    grey.ShowReMode();
    s42.ChangeReMode(grey);
    grey.ShowReMode();

    system("pause");
    return 0;
}

编程练习2

//exc_mean.h
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <iostream>
#include <stdexcept>
#include <string>

class bad_hmean : public std::logic_error
{
public:
    bad_hmean(const std::string &s = "hmean(), invalid argument") : logic_error(s) {}
};

class bad_gmean : public std::logic_error
{
public:
    bad_gmean(const std::string &s = "gmean(), auguments shouble be >= 0") : logic_error(s) {}
};
#endif
//use exc_mean.cpp
#include <iostream>
#include <cmath>
#include "exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;

    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 quite>: ";
        }
        catch (bad_hmean & bg)
        {
            cout << bg.what() << endl;;
            cout << "Try again.\n";
                continue;
        }
        catch(bad_gmean & hg)
        {
            cout << hg.what() << endl;;
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }
    }
    cout << "Bye!\n";
    system("pause");
    return 0;
}

double hmean(double a, double b)
{
    if (a == -b)
        throw bad_hmean();
    return 2 * a *b /(a+b);
}
double gmean(double a, double b)
{
    if (a<0 || b<0)
        throw bad_gmean();
    return std::sqrt(a * b);
}

编程练习3

//exc_mean.h
#ifndef EXC_MEAN_H_
#define EXC_MEAN_H_
#include <iostream>
#include <stdexcept>
#include <string>

class bad_hmean : public std::logic_error
{
private:
    double v1, v2;
public:
    bad_hmean(double x = 0, double y =  0) : std::logic_error("hmean") { v1 = x; v2 = y; }
    virtual void what() { std::cout << "hmean(), invalid argument. a = " << v1 << ", b = " << v2 << std::endl; }
};

class bad_gmean : public std::logic_error
{
private:
    double v1, v2;
public:
    bad_gmean(double x = 0, double y = 0) : logic_error("gmean") { v1 = x; v2 = y; }
    virtual void what() { std::cout << "gmean(), auguments shouble be >= 0. a = " << v1 << ", b = " << v2 << std::endl; }
};
#endif
//use exc_mean.cpp
#include <iostream>
#include <cmath>
#include "exc_mean.h"

double hmean(double a, double b);
double gmean(double a, double b);
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;

    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 quite>: ";
        }
        catch (bad_hmean & bg)
        {
            bg.what();
            cout << "Try again.\n";
                continue;
        }
        catch(bad_gmean & hg)
        {
            hg.what();
            cout << "Sorry, you don't get to play any more.\n";
            break;
        }
    }
    cout << "Bye!\n";
    system("pause");
    return 0;
}

double hmean(double a, double b)
{
    if (a == -b)
        throw bad_hmean(a, b);
    return 2 * a *b /(a+b);
}
double gmean(double a, double b)
{
    if (a<0 || b<0)
        throw bad_gmean(a, b);
    return std::sqrt(a * b);
}

编程练习4

主函数修改即可

#include <iostream>
#include "sales.h"

int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    using std::logic_error;
    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);

    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 & le)
    {
        cout << le.what();
        if(LabeledSales::nbad_index *ni = dynamic_cast<LabeledSales::nbad_index *>(&le))
        {
            cout << "Company: " << ni->label_val() << endl;
            cout << "bad index: " << ni->bi_val() << endl;
        }
        else if(Sales::bad_index *bi = dynamic_cast<Sales::bad_index *>(&le))
            cout << "bad index: " << bi->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 & le)
    {
        cout << le.what();
        if(LabeledSales::nbad_index *ni = dynamic_cast<LabeledSales::nbad_index *>(&le))
        {
            cout << "Company: " << ni->label_val() << endl;
            cout << "bad index: " << ni->bi_val() << endl;
        }
        else if(Sales::bad_index *bi = dynamic_cast<Sales::bad_index *>(&le))
            cout << "bad index: " << bi->bi_val() << endl;
    }
    cout << "done\n";
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值