C++学习-Day-31

C++学习-Day-31

一、编程练习

  1. 15_1
#include<iostream>
class Remote;
class Tv
{
    friend class Remote;
    int state;
    int volume;
    int maxchannel;
    int channel;
    int mode;//broadcast or cable
    int 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){}
    void onoff(){state=~state;}
    bool ison() const {return state==On;}
    bool volup();
    bool voldown();
    void chanup();
    void chandown();
    void set_mode(){mode=~mode;}
    void set_input(){input=~input;}
    void set_state(Remote & r);
    void settings() 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();}
    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() const;//display mode and state of remote
};

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;
}
inline void 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;
}
int main()
{
    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();
}

  1. 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"){}
};

double hmean(double a,double b)
{
    if(a==-b)
        throw bad_hmean();
    return 2.0*a*b/(a+b);
}
double gmean(double a,double b)
{
    if(a<0||b<0)
        throw bad_gmean();
    return sqrt(a*b);
}
int main()
{
    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";
    return 0;
}

  1. 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){}
    double re_a() const{return a;};
    double re_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"){}
};

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

int main()
{
    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";
    return 0;
}

  1. 15_4
#include<stdexcept>
#include<string>
#include<iostream>
class Sales
{
public:
    enum{MONTHS=12};
private:
    double gross[MONTHS];
    int year;
public:
    class bad_index:public std::logic_error
    {
        int bi;
    public:
        explicit bad_index(int ix,const std::string & s="Index error in Sales object\n"):std::logic_error(s),bi(ix){}
        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);
};

class LabeledSales:public Sales
{
    std::string label;
public:
    class nbad_index:public Sales::bad_index
    {
        std::string lbl;
    public:
        nbad_index(const std::string &lb,int ix,const std::string &s="Index error in LabeledSales object\n"):Sales::bad_index(ix,s),lbl(lb){}
        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);
};

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::LabeledSales(const std::string & lb,int yy):Sales(yy)
{
    label=lb;
}
LabeledSales::LabeledSales(const std::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);
}
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    double vals1[12]={1220,1100,1122,2212,1232,4354,4324,7675,6546,3256,9800,5478};
    double vals2[12]={32,56,32,76,32,87,42,97,28,76,42,86};
    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(Sales::bad_index & bad)
    {
        cout<<bad.what();
        if(LabeledSales::nbad_index * ps=dynamic_cast< LabeledSales::nbad_index *>(&bad))
        cout<<"Company: "<<ps->label_val()<<endl;
        cout<<"bad index: "<<bad.bi_val()<<endl;
    }
    cout<<"Next try block:\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 * ps=dynamic_cast< LabeledSales::nbad_index *>(&bad))
        cout<<"Company: "<<ps->label_val()<<endl;
        cout<<"bad index: "<<bad.bi_val()<<endl;
    }
    cout<<"done\n";
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值