端午的作业

#include <iostream>
 
using namespace std;
class RMB
{
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    RMB(){
        count++;
    }
    RMB(int yuan,int jiao,int fen):yuan(yuan),jiao(jiao),fen(fen){
        count++;
    }
    //加法
    const RMB operator+(const RMB &r)
    {
        RMB temp;
        int a=0,b=0;
        temp.fen=this->fen+r.fen;
        if(temp.fen>10)
        {
            temp.fen=temp.fen%10;
            a++;
        }
        if(a==1)
        {
            temp.jiao=this->jiao+r.jiao+1;
            a--;
        }else
        {
            temp.jiao=this->jiao+r.jiao;
        }
        if(temp.jiao>10)
        {
            temp.jiao=temp.jiao%10;
            b++;
        }
        if(b==1)
        {
            temp.yuan=this->yuan+r.yuan+1;
            b--;
        }else
        {
            temp.yuan=this->yuan+r.yuan;
        }
        return temp;
    }
    //减法
    const RMB operator-(const RMB &r)
    {
        RMB temp;
        int a=0,b=0;
        if(yuan<r.yuan)
        {
            cout << "账户余额不足" << endl;
        }else
        {
            //分减法
            if(fen<r.fen)
            {
                a++;
                temp.fen=fen+10-r.fen;
            }else
            {
                temp.fen=fen-r.fen;
            }
            //角减法
            if(jiao<r.jiao)
            {
                if(a==1)
                {
                    temp.jiao=10+jiao-1-r.jiao;
                    a--;
                    b++;
                }else
                {
                    temp.jiao=10+jiao-r.jiao;
                    b++;
                }
            }else
            {
                if(a==1)
                {
                    temp.jiao=jiao-1-r.jiao;
                    a--;
                    b++;
                }else
                {
                    temp.jiao=jiao-r.jiao;
                    b++;
                }
            }
            //元减法
            if(b==1)
            {
                temp.yuan=yuan-1-r.yuan;
                b--;
            }else
            {
                temp.yuan=yuan-r.yuan;
            }
        }
        return temp;
    }
    //大于
    bool operator>(const RMB &r) const
    {
        if(yuan>r.yuan)
        {
            return true;
        }else if(yuan==r.yuan)
        {
            if(jiao>r.jiao)
            {
                return true;
            }else if(jiao==r.jiao)
            {
                if(fen>r.fen)
                {
                    return true;
                }else if(fen==r.fen)
                {
                    cout << "余额相同" << endl;
                }else
                {
                    return false;
                }
            }else
            {
                return false;
            }
        }else
        {
            return false;
        }
    }
    //
    RMB &operator--()
    {
        --yuan;
        --jiao;
        --fen;
        return *this;
    }
    ~RMB()
    {
        count--;
    }
    void show()
    {
        cout << yuan << " " << jiao << " " << fen << endl;
        cout << "目前有" << count << "个账户在线" << endl;
    }
};
int RMB::count=0;
 
int main()
{
    RMB r1(14,9,6);
    RMB r2(15,5,3);
    RMB r3=r1+r2;
    r3.show();
    --r3;
    r3.show();
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值