#include<iostream>
#include<iomanip>
using namespace std;

class RMB {
private:
    int yuan, jiao, fen;

public:
    RMB(int yuan = 0, int jiao = 0, int fen = 0) : yuan(yuan), jiao(jiao), fen(fen) {}

    friend istream& operator>>(istream& in, RMB& r) {
        cout << "请分别输入元、角、分(整数之间用空格或回车间隔):" << endl;
        in >> r.yuan >> r.jiao >> r.fen;
        return in;
    }

	friend ostream& operator<<(ostream& out, const RMB& r) {
	    int totalFen = r.yuan * 100 + r.jiao * 10 + r.fen;
	    int yuan = totalFen / 100;
	    int jiao = (totalFen / 10) % 10;
	    int fen = totalFen % 10;
	    out << "￥ " << yuan << "." << jiao << fen;
	    return out;
	}

    RMB operator+(const RMB& r) const {
        RMB result;
        result.fen = fen + r.fen;
        result.jiao = jiao + r.jiao + result.fen / 100;
        result.fen %= 100;
        result.yuan = yuan + r.yuan + result.jiao / 10;
        result.jiao %= 10;
        return result;
    }

    RMB& operator+=(const RMB& r) {
        fen += r.fen;
        jiao += r.jiao + fen / 100;
        fen %= 100;
        yuan += r.yuan + jiao / 10;
        jiao %= 10;
        return *this;
    }

    RMB operator-(const RMB& r) const {
        RMB result;
        int totalFen = (yuan * 10 + jiao) * 10 + fen;
        int rTotalFen = (r.yuan * 10 + r.jiao) * 10 + r.fen;
        totalFen -= rTotalFen;
        if (totalFen < 0)
            totalFen = 0;
        result.fen = totalFen % 10;
        totalFen /= 10;
        result.jiao = totalFen % 10;
        totalFen /= 10;
        result.yuan = totalFen;
        return result;
    }

    RMB& operator++() {
        fen++;
        if (fen >= 100) {
            fen -= 100;
            jiao++;
        }
        if (jiao >= 10) {
            jiao -= 10;
            yuan++;
        }
        return *this;
    }
};


int main()
{
    RMB r1(14,  5  ,7),r2,r3;    //定义三个人民币的对象,  其中两个调用缺省的构造,  构造为0
    cout<<"请输入一个人民币数值:  \n";
    cin>>r2;    //调用重载的输入流输入对象的数值
    r3=r1+r2;    //调用重载的加法运算符
    cout<<r1<<"  +  "<<r2<<"  =  "<<r3<<endl;
    cout<<r3<<"  +  "<<r1<<"  =  ";  
    r3+=r1;  //调用重载运算符
    cout<<r3<<endl;
    cout<<r3<<"  -  "<<r1<<"  =  ";
    r3=r3-r1;  //调用重载运算符
    cout<<r3<<endl;
    cout<<r3<<"  +  "<<"￥    0.01  =  ";
    ++r3;  //调用重载运算符
    cout<<r3<<endl;
    return  0;
}
