C++ Primer Plus 第十五章 课后编程练习题1-4

//1题
//类定义
#ifndef tv_remote_hpp
#define tv_remote_hpp
class Remote;
class Tv
{
public:
friend class Remote;
enum State{Off, On}; //开关
enum {MinVal, MaxVal = 20}; //频道
enum {Antenna, Cable}; //有线, 无线
enum {TV, DVD}; //电视, DVD
enum {Routine, Interaction}; //遥控器 常规状态, 互动状态
Tv(int s = Off, int mc = 125) : state(s), volume(5),
maxchannel(mc), channel(2), mode(Cable), input(TV) {}
void onoff() {state ^= 1;} //切换开关
bool ison() const {return state == On;}; //返回开关
bool volup(); //增加音量
bool voldown(); //减小音量
void chanup(); //频道+1
void chandown(); //频道-1
void set_mode() {mode ^= 1;} //切换有线,无线
void set_input() {input ^= 1;} //切换TV, DVD
void settings() const; //显示当前设置
void set_pattern(Remote & r); //遥控器常规状态, 互动状态切换
private:
int state; //开关
int volume; //音量
int maxchannel; //最大频道数
int channel; //当前频道
int mode; //有线, 无线
int input; //电视, DVD
};
class Remote
{
public:
friend class Tv;
Remote(int m = Tv::TV, int p = Tv::Routine) :mode(m), pattern§ {}
bool volup(Tv & t) {return t.volup();} //音量+1
bool voldown(Tv & t) {return t.voldown();} //音量-1
void onoff(Tv & t) {t.onoff();} //开关
void chanup(Tv & t) {t.chanup();} //频道+1
void chandown(Tv & t) {t.chandown();} //频道-1
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();} //切换TV,DVD
void set_pattern() ; //切换 状态
void show_pattern(); //显示
private:
int mode; //TV,DVD
int pattern; //常规模式, 互动模式
};

#endif /* tv_remote_hpp */

//类函数定义
#include “tv_remote.hpp”
#include
using std::cout;
using std::endl;
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::set_pattern(Remote & r)
{
if (input == TV)
r.set_pattern();
}

void Tv::settings()const
{

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 Remote::set_pattern()
{
if (mode == Tv::TV)
pattern ^= 1;
}

void Remote::show_pattern()
{

if (mode == Tv::TV)
    cout <<"Remote Pattern = "<< (pattern == 0? "Routine" :"Interaction")
    << endl;

}
//测试小程序
#include
#include “tv_remote.hpp”
int main()
{
using std::cout;
Tv s42;
cout <<“Initial settings for 42” TV:\n";
s42.settings();
s42.onoff();
s42.chanup();
cout <<“Adjusted 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";
grey.show_pattern();
s42.settings();

Tv s58(Tv::On);
s58.set_pattern(grey);
s58.set_mode();
grey.set_chan(s58, 28);
cout <<"\n58\" settings:\n";
grey.show_pattern();
s58.settings();
return 0;

}

//2题
//类定义
#ifndef exc_mean_hpp
#define exc_mean_hpp
#include
#include
class bad_hmean :std::logic_error
{
public:
bad_hmean(const std::string & st) : std::logic_error(st) {}
bad_hmean(const char * st) : std::logic_error(st) {}
virtual void what();
};

class bad_gmean :std::logic_error
{
public:
bad_gmean(const std::string & st) : std::logic_error(st) {}
bad_gmean(const char * st) : std::logic_error(st) {}
virtual void what();
};
#endif /* exc_mean_hpp */

//类函数定义
#include “exc_mean.hpp”
#include
void bad_hmean::what()
{
std::cout << std::logic_error::what();
std::cout <<" invalid arguments: a = -b\n";
}

void bad_gmean::what()
{
std::cout << std::logic_error::what();
std::cout <<" arguments should be >= 0\n";
}

//测试程序
#include
#include “exc_mean.hpp”
#include
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 quit>: ";
    }
    catch (bad_hmean & bg)
    {
        bg.what();
        cout <<"Try again.\n";
        continue;
    }
    catch (bad_gmean & pg)
    {
        pg.what();
        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()”);
return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
if (a < 0 || b < 0)
throw bad_gmean(“gmean()”);
return std::sqrt(a * b);
}

//3题
//类定义
#ifndef h_g_mean_hpp
#define h_g_mean_hpp
#include
#include
class bad_1 : std::logic_error
{
private:
double bad1;
double bad2;
public:
bad_1(double a, double b,const std::string & st)
:bad1(a), bad2(b), std::logic_error(st) {}
bad_1(double a, double b, const char * st)
:bad1(a), bad2(b), std::logic_error(st) {}
virtual void show()const;
};

class bad_hmean : bad_1
{
public:
bad_hmean(double a, double b, const std::string & st)
: bad_1(a, b, st) {}
bad_hmean(double a, double b, const char * st)
: bad_1(a, b, st) {}
void show() const;
};

class bad_gmean : bad_1
{
public:
bad_gmean(double a, double b, const std::string & st)
: bad_1(a, b, st) {}
bad_gmean(double a, double b, const char * st)
: bad_1(a, b, st) {}
void show() const;
};
#endif /* h_g_mean_hpp */

//类函数定义
#include
#include “h_g_mean.hpp”
using std::cout;
using std::endl;
void bad_1::show() const
{
cout << std::logic_error::what();
cout <<"(" << bad1 <<", " << bad2 <<"): ";
}

void bad_hmean::show() const
{
bad_1::show();
cout <<“invalid arguments: a = -b!\n”;
}

void bad_gmean::show() const
{
bad_1::show();
cout <<“arguments should be >= 0!\n”;
}

//测试程序
#include
#include
#include “h_g_mean.hpp”

double hmean(double a, double b);
double gmean(double a, double b);

int main()
{
using std::endl;
using std::cout;
using std::cin;

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)
    {
        bg.show();
        cout <<"Sorry you don't get to play any more.\n";
        break;
    }
    catch(bad_gmean & pg)
    {
        pg.show();
        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(a, b,“hmean()”);
return 2.0 * a * b / (a + b);
}

double gmean(double a, double b)
{
if (a < 0 || b < 0)
throw bad_gmean(a, b, “gmean()”);
return std::sqrt(a * b);
}

//4题
//只修改了main()函数
#include
#include “sales.hpp”
int main()
{
using std::cout;
using std::cin;
using std::endl;
double vals1[12] =
{
1220, 1100, 1122, 2212, 1232, 2334,
2883, 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 bock 1.\n”;
}
//基类指针引用可以指向派生类,接受引用后,转换为派生类,如果为空证明原本不是派生类的。
catch (Sales::bad_index & bad)
{
cout << bad.what();
if (LabeledSales::nbad_index * p = dynamic_cast<LabeledSales::nbad_index *> (&bad))
cout << “Company: " << p->label_val() << endl;
cout << “bad index: " << bad.bi_val() << endl;
}
cout <<”\nNext try bloock:\n”;
try
{
sales2[2] = 37.5;
sales1[20] = 23345;
cout <<“End of try block 2.\n”;
}
catch (Sales::bad_index & bad)
{
cout << bad.what();
if (LabeledSales::nbad_index * p = dynamic_cast<LabeledSales::nbad_index *> (&bad))
cout << "Company: " << p->label_val() << endl;
cout << "bad index: " << bad.bi_val() << endl;
}
cout <<“done\n”;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值