#include<iostream>
class Remote;
class Tv
{
friend class Remote;int state;int volume;int maxchannel;int channel;int mode;//broadcast or cableint input;//TV or DVD
public:enum{Off,On};enum{MinVal,MaxVal=20};enum{Antenna,Cable};enum{TV,DVD};enum{Normal,Interaction};Tv(int s=Off,int mc=125):state(s),volume(5),maxchannel(mc),channel(1),mode(Cable),input(TV){}voidonoff(){state=~state;}
bool ison()const{return state==On;}
bool volup();
bool voldown();voidchanup();voidchandown();voidset_mode(){mode=~mode;}voidset_input(){input=~input;}voidset_state(Remote & r);voidsettings()const;//display all settings};
class Remote
{
friend class Tv;int mode;int state;
public:Remote(int m=Tv::TV,int s=Tv::Normal):mode(m),state(s){}
bool volup(Tv & t){return t.volup();}
bool voldown(Tv & t){return t.voldown();}voidonoff(Tv & t){t.onoff();}voidchanup(Tv & t){t.chanup();}voidchandown(Tv & t){t.chandown();}voidset_chan(Tv & t,int c){t.channel=c;}voidset_mode(Tv & t){t.set_mode();}voidset_input(Tv & t){t.set_input();}voidshow()const;//display mode and state of remote};
bool Tv::volup(){if(volume<MaxVal){
volume++;return true;}elsereturn false;}
bool Tv::voldown(){if(volume>MinVal){
volume--;return true;}elsereturn false;}void Tv::chanup(){if(channel<maxchannel)
channel++;else
channel=1;}void Tv::chandown(){if(channel>1)
channel--;else
channel=maxchannel;}inlinevoid Tv::set_state(Remote & r){if(state)
r.state=~r.state;}void Tv::settings()const{
std::cout<<"TV is "<<(state==Off?"Off":"On")<<std::endl;if(state){
std::cout<<"Volume setting="<<volume<<std::endl;
std::cout<<"Channel setting="<<channel<<std::endl;
std::cout<<"Mode="<<(mode==Antenna?"Antenna":"Cable")<<std::endl;
std::cout<<"Input="<<(input==TV?"TV":"DVD")<<std::endl;}}void Remote::show()const{
std::cout<<"Remote mode="<<(mode==Tv::TV?"TV":"DVD")<<std::endl;
std::cout<<"Remote state="<<(state==Tv::Normal?"Normal":"Interaction")<<std::endl;}intmain(){
using std::cout;
Tv s1;
cout<<"Initial settings for s1 TV:\n";
s1.settings();
s1.onoff();
s1.chanup();
cout<<"boosting the s1 TV and channel up for it\n";
s1.settings();
Remote r;
cout<<"Initial settings for r Remote:\n";
r.show();
r.set_chan(s1,15);
r.volup(s1);
r.set_input(s1);
cout<<"using remote to set channel 15 and volume up and set input\n";
s1.settings();
cout<<"CD to Interactive mode\n";
s1.set_state(r);
r.show();}
15_2
#include<iostream>#include<stdexcept>#include<cmath>
class bad_hmean:public std::logic_error
{
public:bad_hmean():logic_error("valid arguments to hmean"){}};
class bad_gmean:public std::logic_error
{
public:bad_gmean():logic_error("valid arguments to gmean"){}};doublehmean(double a,double b){if(a==-b)
throw bad_hmean();return2.0*a*b/(a+b);}doublegmean(double a,double b){if(a<0||b<0)
throw bad_gmean();returnsqrt(a*b);}intmain(){
using std::cin;
using std::cout;
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(std::logic_error &le){
cout<<le.what()<<endl;
cout<<"Try again.\n";continue;}}
cout<<"Bye.\n";return0;}
15_3
#include<iostream>#include<stdexcept>#include<cmath>#include<string>
class base_error:public std::logic_error
{double a;double b;
public:base_error(double v1,double v2,const std::string &s):a(v1),b(v2),logic_error(s){}doublere_a()const{return a;};doublere_b()const{return b;};};
class bad_hmean:public base_error
{
public:bad_hmean(double v1,double v2):base_error::base_error(v1,v2,"are valid arguments for hmean"){}};
class bad_gmean:public base_error
{
public:bad_gmean(double v1,double v2):base_error::base_error(v1,v2,"are valid arguments for gmean"){}};doublehmean(double a,double b){if(a==-b)
throw bad_hmean(a,b);return2.0*a*b/(a+b);}doublegmean(double a,double b){if(a<0||b<0)
throw bad_gmean(a,b);returnsqrt(a*b);}intmain(){
using std::cin;
using std::cout;
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(base_error &be){
cout<<be.re_a()<<" and "<<be.re_b()<<" is "<<be.what()<<endl;
cout<<"Sorry,you don't play any more.\n";break;}}
cout<<"Bye.\n";return0;}