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



文件信息
文件来源  
文件作者  
更新时间 2005-5-8 10:53:16 
添加编辑 viewsnake 

辅助信息
打印功能 打印本文
背景颜色 杏黄 秋褐 胭红 芥绿 天蓝 雪青 炭灰 奶白
字体大小 特大号字 大号字 中号字 小号字
免责声明 本网站所有文章均来自网络,仅提供预览形式,不提供纸张形式,若涉及到版权的文章,请购买正版,毕竟在电脑上看也不舒服啊,呵呵,这是viewsnake个人网站,纯粹交流学习资料的地方。无商业行为。
选择更多免费考研资料:
阅读正文内容
 

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



<<<返回上一页 <<<返回网站首页
<<<您的位置:首页>考研经验>考研笔记>计算机工程笔记>正文