Description
Define RMB RMB class, the data members of this class include int yuan (yuan), int jiao (corner), int fen (fen), bool mark (sign, indicating positive and negative numbers). The constructor of the RMB class includes at least two types, that is, using (element, angle, cent, sign) or using double type data to construct the RMB class.
The following is the code of the main function, please supplement the definition of the RMB class according to the needs of the main function.
int main()
{
RMB a, b;
double c;
cout.setf(cout.showpoint);
cout.precision(2);
cout.setf(ios::fixed);
cin >> a;
cin >> b;
cin >> c;
cout << RMB(a + c) << endl;
cout << RMB(b - a) << endl;
cout << RMB(2.18 + a) << endl;
return 0;
}
Input
First input the elements, angles, and minutes of a and b respectively, and then input a double type data as the input of c:
Output
According to the main function code, give the result of RMB class addition and subtraction;
The result is output as a floating point number with two decimal places;
Sample Input
1
2
3
1
5
1
3.23
Sample Output
4.46
0.28
3.41
analyze
- It is necessary to overload operators
operator >>
, etc. These two overloads should be overloaded as global functions ,operator <<
and then declared as friend functions, because the usage iscin >> a
rathercout << RMB(a + c) << endl
thana >>
.a <<
And there is a pitfall here,RMB(a + c)
the return is a prvalue, so the overloaded function parameter table also needs to be written as an rvalue reference, constant reference or value copy, and it cannot be called when it is written as a reference. - Since the title says that there will be variables indicating positive and negative
bool mark
, it can be determined thatint yuan, jiao, fen
both are positive numbers. - Due to the existence of positive and negative,
operator +
andoperator -
needs to consider the positive and negative ± positive and negative, which is too troublesome. It can be converted to the calculationdouble
first , and the calculation result is the samedouble
, so it needs to bedouble
constructedRMB
. The title says that two constructors need to be explicitly defined, one of which isdouble
constructed , and this is the reason. In addition,operator +
andoperator -
must be overloaded as a global function, and then declared as a friend function, because the left and right operands are a mixdouble
of and .RMB
- Floating-point numbers have precision issues, and both
float
ordouble
must consider precision issues. Simply put, you only need
Then you can usedouble value_f = 0; cin >> value_f; value_f *= 100; value_f += 0.5; int value = floor(value_f);
value
to cut out the rounded corners, pay attention tofloor()
the needs#include <cmath>
.
Summit
#include <cmath>
#include <iostream>
using namespace std;
class RMB {
friend ostream& operator<<(ostream&, RMB&&);
friend istream& operator>>(istream&, RMB&);
friend RMB operator+(RMB&, double);
friend RMB operator+(double, RMB&);
friend RMB operator-(RMB&, RMB&);
public:
RMB(int _y = 0, int _j = 0, int _f = 0, bool _m = true)
: yuan(_y)
, jiao(_j)
, fen(_f)
, mark(_m)
{
}
explicit RMB(double value_f)
{
value_f *= 100;
value_f += 0.5;
int value = floor(value_f);
mark = (value >= 0) ? true : false;
value = abs(value);
yuan = value / 100;
value -= yuan * 100;
jiao = value / 10;
value -= jiao * 10;
fen = value;
}
double to_double()
{
double ans = 100 * yuan + 10 * jiao + fen;
ans /= 100;
if (!mark)
return -1 * ans;
return ans;
}
private:
int yuan, jiao, fen;
bool mark;
};
ostream& operator<<(ostream& out, RMB&& rmb)
{
cout << rmb.to_double();
return out;
}
istream& operator>>(istream& in, RMB& rmb)
{
int yuan = 0, jiao = 0, fen = 0;
cin >> yuan >> jiao >> fen;
rmb.yuan = yuan, rmb.jiao = jiao, rmb.fen = fen;
return in;
}
RMB operator+(RMB& lhs, double rhs)
{
double l_value = lhs.to_double();
return RMB(l_value + rhs);
}
RMB operator+(double lhs, RMB& rhs)
{
double r_value = rhs.to_double();
return RMB(lhs + r_value);
}
RMB operator-(RMB& lhs, RMB& rhs)
{
double l_value = 0, r_value = 0;
l_value = lhs.to_double();
r_value = rhs.to_double();
return RMB(l_value - r_value);
}
int main()
{
RMB a, b;
double c;
cout.setf(cout.showpoint);
cout.precision(2);
cout.setf(ios::fixed);
cin >> a;
cin >> b;
cin >> c;
cout << RMB(a + c) << endl;
cout << RMB(b - a) << endl;
cout << RMB(2.18 + a) << endl;
return 0;
}