【定义一个人民币的类(重载+、-、+=、++、输入输出流)】

该代码示例定义了一个C++类RMB,包含元、角、分成员,实现了输入输出流的重载以及加减运算符的重载,支持人民币数值的运算和进位处理。主函数中展示了类的使用,包括加法、减法和自增操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题描述】定义一个人民币的类,其中成员数据包括元、角、分,成员函数包括构造及输出函数。要求增加适当的成员函数,重载+、-、+=、++、输入输出流,来实现人民币的直接运算。注意分、角、元的进位。键盘输入一个人民币数值,输出完成运算后的相应结果。

【输入形式】输入元、角、分(整数之间用空格间隔)。

【输出形式】完成运算后的相应结果。

【样例输入】

请输入一个人民币数值:

请分别输入元、角、分(整数之间用空格间隔):

10 10 10

【样例输出】

¥ 14.57 + ¥ 11.10 = ¥ 25.67

¥ 25.67 + ¥ 14.57 = ¥ 40.24

¥ 40.24 - ¥ 14.57 = ¥ 25.67

¥ 25.67 + ¥ 0.01 = ¥ 25.68

【样例说明】
【评分标准】 3个评分点

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<istream>
#include<iomanip>
#include<ostream>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<fstream>
#include<stack>
#include<ctime>
#include<deque>
#include<queue>
#include <sstream>
#include <numeric>
#pragma warning (disable:4996)

using namespace std;

class RMB {
private:
    int yuan;  // 元
    int jiao;  // 角
    int fen;   // 分

public:
    RMB() : yuan(0), jiao(0), fen(0) {}  // 缺省构造函数,初始化为零
    RMB(int y, int j = 0, int f = 0) : yuan(y), jiao(j), fen(f) {}  // 带参构造函数
    friend ostream& operator<<(ostream& out, const RMB& r);  // 友元函数,重载输出流
    friend istream& operator>>(istream& in, RMB& r);  // 友元函数,重载输入流

    RMB operator+(const RMB& r) const;  // 重载加法运算符
    RMB operator-(const RMB& r) const;  // 重载减法运算符
    RMB& operator+=(const RMB& r);  // 重载累加运算符
    RMB& operator++();  // 重载前置自增运算符
};

// 重载输出流
ostream& operator<<(ostream& out, const RMB& r) {
    RMB temp = r;
    if (temp.fen >= 10)
    {
        temp.jiao = temp.jiao + (temp.fen / 10);
        temp.fen %= 10;
    }

    if (temp.jiao >= 10)
    {
        temp.yuan = temp.yuan + (temp.jiao / 10);
        temp.jiao %= 10;
    }
    out << "¥" << setw(3)<<temp.yuan << '.'<< temp.jiao << temp.fen;
    return out;
}

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

// 重载加法运算符
RMB RMB::operator+(const RMB& r) const {
    RMB result(this->yuan + r.yuan, this->jiao + r.jiao, this->fen + r.fen);
    if (result.fen >= 10) {  // 分进位
        result.jiao += result.fen / 10;
        result.fen %= 10;
    }
    if (result.jiao >= 10) {  // 角进位
        result.yuan += result.jiao / 10;
        result.jiao %= 10;
    }
    return result;
}

// 重载减法运算符
RMB RMB::operator-(const RMB& r) const {
    RMB result(this->yuan - r.yuan, this->jiao - r.jiao, this->fen - r.fen);
    if (result.fen < 0) {  // 分借位
        result.fen += 10;
        result.jiao -= 1;
    }
    if (result.jiao < 0) {  // 角借位
        result.jiao += 10;
        result.yuan -= 1;
    }
    return result;
}

// 重载累加运算符
RMB& RMB::operator+=(const RMB& r) {
    *this = *this + r;
    return *this;
}

// 重载前置自增运算符
RMB& RMB::operator++() {
    this->fen += 1;
    if (this->fen >= 10) {  // 分进位
        this->jiao += this->fen / 10;
        this->fen %= 10;
    }
    if (this->jiao >= 10) {  // 角进位
        this->yuan += this->jiao / 10;
        this->jiao %= 10;
    }
    return *this;
}

int main() {
    RMB r1(14, 5, 7), r2, r3;
    cout << "请输入一个人民币数值:" << endl;
    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wdwc2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值