C++day5

作业:

#include <iostream>

using namespace std;

class RMB
{
    friend const RMB operator+(RMB &L,RMB &R);
    friend const RMB operator-(RMB &L,RMB &R);
    friend bool operator>(RMB &L,RMB &R);
    friend RMB &operator--(RMB &R);
    friend const RMB operator--(RMB &L,int);
private:
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    static int RMBadd()
    {
        return ++count;
    }
    static int RMBdel()
    {
        return --count;
    }
    RMB()
    {
        RMBadd();
    }
    RMB(int y,int j,int f):yuan(y),jiao(j),fen(f)
    {
        RMBadd();
    }
    ~RMB()
    {
        RMBdel();
    }
    void show()
    {
        cout << "total money:" << yuan << "yuan" << jiao << "jiao" << fen << "fen" << endl;
    }
    static void countshow()
    {
        cout << "total count:" << count << endl;
    }
};

int RMB::count=0;

const RMB operator+(RMB &L,RMB &R)
{
    RMB temp;
    temp.yuan=L.yuan+R.yuan;
    temp.jiao=L.jiao+R.jiao;
    if(temp.jiao>=10)
    {
        temp.yuan+=temp.jiao/10;
        temp.jiao%=10;
    }
    temp.fen=L.fen+R.fen;
    if(temp.fen>=10)
    {
        temp.jiao+=temp.fen/10;
        temp.fen%=10;
    }
    return temp;
}

const RMB operator-(RMB &L,RMB &R)
{
    RMB temp;
    temp.yuan=L.yuan-R.yuan;
    temp.jiao=L.jiao-R.jiao;
    if(temp.jiao<0)
    {
        temp.yuan--;
        temp.jiao+=10;
    }
    temp.fen=L.fen-R.fen;
    if(temp.fen<0)
    {
        temp.jiao--;
        temp.fen+=10;
    }
    return temp;
}

bool operator>(RMB &L,RMB &R)
{
    if(L.yuan>R.yuan && L.jiao>R.jiao && L.fen>R.fen)
    {
        return true;
    }
    else
    {
        return false;
    }
}

RMB &operator--(RMB &R)
{
    --R.yuan;
    --R.jiao;
    --R.fen;
    return R;
}

const RMB operator--(RMB &L,int)
{
    RMB temp;
    temp.yuan=L.yuan--;
    temp.jiao=L.jiao--;
    temp.fen=L.fen--;
    return temp;
}
int main()
{
    RMB r;
    r.countshow();
    RMB r1(9,9,9);
    r1.countshow();
    RMB *r2=new RMB;
    r2->countshow();
    delete r2;
    r2=nullptr;
    r1.countshow();

    RMB r3(8,8,8);
    r=r1+r3;
    r.show();

    RMB r4=r-r1;
    r4.show();

    --r4;
    r4.show();
    RMB r5=r4--;
    r5.show();
    r4.show();

    return 0;
}

结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值