6.7作业


#include <iostream>
 
using namespace std;
class Rmb
{
    //角分以十为单位
private:
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    Rmb(int yuan=0,int jiao=0,int fen=0):yuan(yuan),jiao(jiao),fen(fen)
    {
        int xjiao=0;
        if(this->fen>=10)
        {
            xjiao=(this->fen)/10;
            this->fen=(this->fen)%10;
 
        }
        this->jiao=(this->jiao)+xjiao;
        if(this->jiao>=10)
        {
            this->yuan=(this->yuan)+((this->jiao)/10);
            (this->jiao)=(this->jiao)%10;
        }
 
            count++;
            cout << "现在的Rmb对象有" << count << "个" << endl;
 
    }
    Rmb &operator=(const Rmb &other)
    {
        this->yuan=other.yuan;
        this->jiao=other.jiao;
        this->fen=other.fen;
//        int xjiao=0;
//        if(this->fen>=10)
//        {
//            xjiao=(this->fen)/10;
//            this->fen=(this->fen)%10;
 
//        }
//        this->jiao=(this->jiao)+xjiao;
//        if(this->jiao>=10)
//        {
//            this->yuan=(this->yuan)+((this->jiao)/10);
//            (this->jiao)=(this->jiao)%10;
//        }
        count++;
        return *this;
    }
    ~Rmb()
    {
 
            count--;
            cout << "死亡一个后现在的Rmb对象有" << count << "个" << endl;
 
    }
 
    const Rmb operator+(const Rmb &R)const
    {
        Rmb temp(0,0,0);
        temp.yuan=yuan+R.yuan;
        temp.jiao=jiao+R.jiao;
        temp.fen=fen+R.fen;
        int xjiao=0;
        if(temp.fen>=10)
        {
            xjiao=(temp.fen)/10;
            temp.fen=(temp.fen)%10;
 
        }
        temp.jiao=(temp.jiao)+xjiao;
        if(temp.jiao>=10)
        {
            temp.yuan=(temp.yuan)+((temp.jiao)/10);
            (temp.jiao)=(temp.jiao)%10;
        }
        return temp;
    }
    const Rmb operator-(const Rmb &R)const
    {
        Rmb temp;
        int yuan1=yuan;
        int jiao1=jiao;
        int fen1=fen;
        int yuan2=R.yuan;
        int jiao2=R.jiao;
        int fen2=R.fen;
        if(yuan1>yuan2)
        {
            if(fen1<fen2)
            {
                jiao1--;
                fen1=fen1+10;
                temp.fen=fen1-fen2;
                if(jiao1<jiao2)
                {
                    yuan1--;
                    jiao1=jiao1+10;
                    temp.jiao=jiao1-jiao2;
                    temp.yuan=yuan1-yuan2;
                    return temp;
                }
                else if(jiao1>=jiao2)
                {
                    temp.jiao=jiao1-jiao2;
                    temp.yuan=yuan1-yuan2;
                    return temp;
                }
            }
            else if(fen1>=fen2)
            {
                temp.fen=fen1-fen2;
                if(jiao1<jiao2)
                {
                    yuan1--;
                    jiao1=jiao1+10;
                    temp.jiao=jiao1-jiao2;
                    temp.yuan=yuan1-yuan2;
                    return temp;
                }
                else if(jiao1>=jiao2)
                {
                    temp.jiao=jiao1-jiao2;
                    temp.yuan=yuan1-yuan2;
                    return temp;
                }
            }
        }
        else if(yuan1==yuan2)
        {
            if(jiao1>jiao2)
            {
                if(fen1<fen2)
                {
                    jiao1--;
                    fen1=fen1+10;
                    temp.fen=fen1-fen2;
                    temp.jiao=jiao1-jiao2;
                    return temp;
                }
                else if(fen1>=fen2)
                {
                    temp.fen=fen1-fen2;
                    temp.jiao=jiao1-jiao2;
                    return temp;
                }
            }
            else if(jiao1==jiao2)
            {
                if(fen1>=fen2)
                {
                    temp.fen=fen1-fen2;
                }
                else if(fen1<fen2)
                {
                    cout << "减法不合法" << endl;
                    return temp;
                }
            }
            else if(jiao1<jiao2)
            {
                cout << "减法不合法" << endl;
                return temp;
            }
        }
        if(yuan1<yuan2)
        {
        cout << "减法不合法" << endl;
        return temp;
        }
        return temp;
    }
    bool operator>(const Rmb &R)const
    {
        if(yuan>R.yuan)
        {
            return 1;
        }
        else if(yuan==R.yuan)
        {
            if(jiao>R.jiao)
            {
                return 1;
            }
            else if(jiao==R.jiao)
            {
                if(fen>R.fen)
                {
                    return 1;
                }
            }
        }
        return 0;
    }
    Rmb &operator--()
    {
        fen--;
        if(fen<0)
        {
            fen=fen+10;
            jiao=jiao-2;
            if(jiao<0)
            {
                jiao=jiao+10;
                yuan=yuan-2;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                }
            }
            else if(jiao>=0)
            {
                yuan=yuan-1;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                }
            }
 
        }
        else if(fen>=0)
        {
            jiao--;
            if(jiao<0)
            {
                jiao=jiao+10;
                yuan=yuan-2;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                    cout << "自减不合法" << endl;
                }
            }
            else if(jiao>=0)
            {
                yuan--;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                    cout << "自减不合法" << endl;
                }
            }
        }
//        if(fen==0)
//        {
//            fen--;
//            fen=fen+10;
//            jiao--;
//        }
//        else
//        {
//            fen--;
//        }
//        if(jiao<=0)
//        {
//            jiao--;
//            jiao=jiao+10;
//            yuan--;
//        }
//        else
//        {
//            jiao--;
//        }
//        if(yuan<=0)
//        {
//            cout << "不合理" << endl;
//        }
//        else
//        {
//            yuan--;
//        }
        return *this;
    }
 
    const Rmb operator--(int)
    {
        Rmb temp=*this;
        fen--;
        if(fen<0)
        {
            fen=fen+10;
            jiao=jiao-2;
            if(jiao<0)
            {
                jiao=jiao+10;
                yuan=yuan-2;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                }
            }
            else if(jiao>=0)
            {
                yuan=yuan-1;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                }
            }
 
        }
        else if(fen>=0)
        {
            jiao--;
            if(jiao<0)
            {
                jiao=jiao+10;
                yuan=yuan-2;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                    cout << "自减不合法" << endl;
                }
            }
            else if(jiao>=0)
            {
                yuan--;
                if(yuan<0)
                {
                    yuan=0;
                    jiao=0;
                    fen=0;
                    cout << "自减不合法" << endl;
                }
            }
        }
        return temp;
    }
    void show()
    {
        cout << yuan << " " << jiao << " " << fen << endl;
    }
};
int Rmb::count=0;
int main()
{
    Rmb r1(0,5,0);
    r1.show();
    Rmb r2(99,6,7);
    r2.show();
    if(r1>r2)
    {
        cout << "r1>r2" << endl;
    }
    Rmb r3=r1+r2;
    r3.show();
    r3=r2-r1;
    r3.show();
    --r1;
    r1.show();
    r1--;
    r1.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值