钱能C++教材习题答案十五

   /2005-05-08

 

第18章

//**********************
//***18.21**************
//*Daiyuebing***********
//*采用了默认的赋值运算*
//**********************
# include <iostream.h>
class Complex
{
public:
        Complex()
        {
                seal=0;
                real=0;
        }
        Complex(double f)
        {
                seal=f;
                real=0;
        }
        Complex(double a,double b)
        {
                seal=a;
                real=b;
        }

        double & getseal()
        {
                return seal;
        }
        double & getreal()
        {
                return real;
        }
        void display()
        {
                cout<<seal<<"    "<<real<<endl;
        }

private:
        double seal;
        double real;
};

Complex operator +(Complex c,Complex d)
{
        Complex e;
        e.getseal()=c.getseal()+d.getseal();
        e.getreal()=c.getreal()+d.getreal();
        return e;
}

void main()
{
        Complex a(2,5),b(7,8),c(0,0);
        a.display();
        b.display();
        c.display();
        cout<<endl;
        c=a+b;
        a.display();
        b.display();
        c.display();
        cout<<endl;
        c=4.1+a;
        a.display();
        b.display();
        c.display();
        cout<<endl;
        c=b+5.6;
        a.display();
        b.display();
        c.display();
        cout<<endl;
}

//**********************
//********18.2**********
//******Daiyuebing******
//*采用了默认的赋值运算*
//**********************
# include <iostream.h>
class time
{
public:
        time()
        {
                h=0;
                m=0;
                s=0;
        }
        time(int a,int b,int c)
        {
                h=a;
                m=b;
                s=c;
        }
        void display()
        {
                cout<<h<<":"<<m<<":"<<s<<endl;
        }
        int& geth()
        {
                return h;
        }
        int& gets()
        {
                return s;
        }
        int& getm()
        {
                return m;
        }
        friend time operator +(time d,time e);
        friend time operator -(time d,time e);
protected:
        int h;
        int m;
        int s;
};
        time operator +(time d,time e)
        {
                time f;
                f.s=d.s+e.s;
                while (f.s>=60)
                {
                        f.s=f.s-60;
                        f.m=f.m+1;
                }
                f.m=f.m+d.m+e.m;
                while (f.m>=60)
                {
                        f.m=f.m-60;
                        f.h=f.h+1;
                }
                f.h=f.h+d.h+e.h;
                while (f.h>=24)
                {
                        f.h=f.h-24;
                }
                return f;
        }
        time operator -(time d,time e)
        {
                time f;
                f.s=d.s-e.s;
                while (f.s<0)
                {
                        f.s=f.s+60;
                        f.m=f.m-1;
                }
                f.m=f.m+d.m-e.m;
                while (f.m<0)
                {
                        f.m=f.m+60;
                        f.h=f.h-1;
                }
                f.h=f.h+d.h-e.h;
                while (f.h<0)
                {
                        f.h=f.h+24;
                }
                return f;
        }
       
void main()
{
        time x(12,23,45),y(3,45,23),z(1,23,34);
        x.display();
        y.display();
        z.display();
        cout<<endl;
        z=x+y;
        x.display();
        y.display();
        z.display();
        cout<<endl;
        z=x-y;
        x.display();
        y.display();
        z.display();
        cout<<endl;
}

//**********************
//***18.3***************
//******Daiyuebing******
//**********************
money::money()
{
        yuan=0;
        jf=0;
}
friend money operator * (const money & a,double b)
{
        money temp;
        temp.yuan=int(a.yuan*b);
        temp.jf=(a.yuan*b-int(a.yuan*b))*100+a.jf*b;
        if(temp.jf>=100)
        {
                temp.jf-=100;
                jf.yuan++;
        }
        return temp;
}

friend money operator * (double b,const money & a)
{
        return a*b;
}

//**********************
//***18.4***************
//******Daiyuebing******
//**********************
money & money:perator +=(const money & a)
{
        yuan=yuan+a.yuan;
        jf=jf+a.jf;
        if (jf>=100)
        {
                jf-=100;
                yuan++;
        }
        return * this;
}

money & money:perator+=(double a)
{
        yuan=yuan+int(a);
        jf=jf+(a-int(a));
        if (jf>=100)
        {
                jf-=100;
                yuan++;
        }
        return * this;
}

money & money:perator-=(const money a)
{
        while(jf<a.jf)
        {
                jf+=100;
                yuan--;
        }
        jf=jf-a.jf;
        yuan=yuan-a.yuan;
        return * this;
}


相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19