20240903 作业

#include <iostream>

using namespace std;

class Rmb
{
friend bool operator>(const Rmb &L, const Rmb &R);
friend const Rmb operator+(const Rmb &L, const Rmb &R);
friend const Rmb operator-(const Rmb &L, const Rmb &R);
friend const Rmb operator--(Rmb &O, int);
friend Rmb operator--(Rmb &O);
private:
    int yuan;
    int jiao;
    int fen;
    static int count;//记录当前已创建的RMB对象的数量

public:
    Rmb() //无参构造,每调用一次  count加1
    {
        count++;
        cout << "count =" << count << endl;
    }
    Rmb(int x,int y,int z):yuan(x),jiao(y),fen(z)//有参构造,每调用一次  count加1
    {
        count++;
        cout << "count =" << count << endl;
    }
    Rmb(const Rmb &other):yuan(other.yuan),jiao(other.jiao),fen(other.fen)//拷贝构造,每调用一次  count加1
    {
        count++;
        cout << "count =" << count << endl;
    }


    Rmb &operator=(const Rmb &other) // 拷贝赋值函数,  已经调用过无参构造,count不用加1
    {
        if (this != &other)
        {
            yuan = other.yuan;
            jiao = other.jiao;
            fen = other.fen;
        }
        return *this;
    }

    ~Rmb()//析构,每调用一次  count-1
    {
        count--;
        cout << "count =" << count << endl;
    }
    void show_rmb()
    {
        cout << yuan << "," << jiao << "," << fen << endl;
    }
};
//全局函数后置--重载
const Rmb operator--(Rmb &O, int)
{
    Rmb temp;
    int money = O.yuan*100+O.jiao*10+O.fen;
    if (money >= 111)
    {
        money -= 111;
        temp.yuan = money/100;
        temp.jiao = (money%100)/10;
        temp.fen = money%10;
    }
    else if(money < 111 && money >= 11)
    {
        money -= 11;
        temp.yuan = 0;
        temp.jiao = (money%100)/10;
        temp.fen = money%10;
    }
    else
    {
        money -= 1;
        temp.yuan = 0;
        temp.jiao = 0;
        temp.fen = money%10;
    }
    return temp;
}
//全局函数前置--重载
Rmb operator--(Rmb &O)
{
    int money = O.yuan*100+O.jiao*10+O.fen;
    if (money >= 111)
    {
        money -= 111;
        O.yuan = money/100;
        O.jiao = (money%100)/10;
        O.fen = money%10;
    }
    else if(money < 111 && money >= 11)
    {
        money -= 11;
        O.yuan = 0;
        O.jiao = (money%100)/10;
        O.fen = money%10;
    }
    else
    {
        money -= 1;
        O.yuan = 0;
        O.jiao = 0;
        O.fen = money%10;

    }
        return O;
}
//全局函数-号重载
const Rmb operator-(const Rmb &L, const Rmb &R)
{
    Rmb temp;
    int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
    int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
    int add = Left - Right;//相-
    temp.yuan = add/100;//计算相-后的yuan
    temp.jiao = (add%100)/10;//计算相-后的角
    temp.fen = add%10;//计算相-后的分
    return temp;
}
//全局函数+号重载
const Rmb operator+(const Rmb &L, const Rmb &R)
{
    Rmb temp;
    int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
    int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
    int add = Left + Right;//相加
    temp.yuan = add/100;//计算相加后的yuan
    temp.jiao = (add%100)/10;//计算相加后的角
    temp.fen = add%10;//计算相加后的分
    return temp;
}
//全局函数>号重载
bool operator>(const Rmb &L, const Rmb &R)
{
    int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
    int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
    if(Left > Right)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int Rmb::count = 0;

int main()
{
    Rmb R1(3,4,5);
    Rmb R2(1,2,3);
    Rmb R3;
    if(R1 > R2)
    {
        cout << "R1 > R2" << endl;
        cout << "======================" << endl;
        R3 = R1 - R2;
        cout << "R3::";
        R3.show_rmb();
        cout << "======================" << endl;
    }

    R3 = R1 + R2;
    R3.show_rmb();
    cout << "======================" << endl;
    R3 = R1--;
    R1.show_rmb();
    R3.show_rmb();
    cout << "======================" << endl;
    R3 = --R1;
    R1.show_rmb();
    R3.show_rmb();
    cout << "======================" << endl;


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值