分别用成员函数和友元函数重载运算符,使对整型的运算符=、+、-、*、/适用于分数运算。要求:(1)输出结果是最简分数(可以是带分数);(2)分母为1,只输出分子。
// g++ -Wall -std=c++17 main.cpp
#include <iostream>
#include <numeric>
class RationalNumber
{
public:
typedef int value_type;
constexpr RationalNumber(value_type a, value_type b) : _a(a), _b(b)
{
simplify();
}
constexpr RationalNumber &operator=(const RationalNumber &other)
{
_a = other._a;
_b = other._b;
return *this;
}
constexpr RationalNumber &operator+=(const RationalNumber &other)
{
auto a = _a * other._b + other._a * _b;
auto b = _b * other._b;
_a = a;
_b = b;
simplify();
return *this;
}
constexpr RationalNumber &operator-=(const RationalNumber &other)
{
auto a = _a * other._b - other._a * _b;
auto b = _b * other._b;
_a = a;
_b = b;
simplify();
return *this;
}
constexpr RationalNumber &operator*=(const RationalNumber &other)
{
_a *= other._a;
_b *= other._b;
simplify();
return *this;
}
constexpr RationalNumber &operator/=(const RationalNumber &other)
{
_a *= other._b;
_b *= other._a;
simplify();
return *this;
}
constexpr void simplify()
{
auto c = std::gcd(_a, _b);
if (c != 0)
{
_a /= c;
_b /= c;
}
}
private:
value_type _a;
value_type _b;
friend constexpr RationalNumber operator+(const RationalNumber &lhs, const RationalNumber &rhs);
friend constexpr RationalNumber operator-(const RationalNumber &lhs, const RationalNumber &rhs);
friend constexpr RationalNumber operator*(const RationalNumber &lhs, const RationalNumber &rhs);
friend constexpr RationalNumber operator/(const RationalNumber &lhs, const RationalNumber &rhs);
template <typename CharT, typename Traits>
friend std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const RationalNumber &number);
};
constexpr RationalNumber operator+(const RationalNumber &lhs, const RationalNumber &rhs)
{
RationalNumber tmp = lhs;
tmp += rhs;
return tmp;
}
constexpr RationalNumber operator-(const RationalNumber &lhs, const RationalNumber &rhs)
{
RationalNumber tmp = lhs;
tmp -= rhs;
return tmp;
}
constexpr RationalNumber operator*(const RationalNumber &lhs, const RationalNumber &rhs)
{
RationalNumber tmp = lhs;
tmp *= rhs;
return tmp;
}
constexpr RationalNumber operator/(const RationalNumber &lhs, const RationalNumber &rhs)
{
RationalNumber tmp = lhs;
tmp /= rhs;
return tmp;
}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const RationalNumber &r)
{
if (r._b == 0)
{
os << "INF";
}
else if (r._b == 1)
{
os << r._a;
}
else
{
auto c = r._a / r._b;
if (c == 0)
{
os << r._a << '/' << r._b;
}
else
{
auto a = r._a % r._b;
os << c << '+' << a << '/' << r._b;
}
}
return os;
}
int main()
{
char ch;
int a1, b1, a2, b2;
std::cout << "Input Fraction 1 (Format: a/b): ";
std::cin >> a1 >> ch >> b1;
std::cout << "Input Fraction 2 (Format: a/b): ";
std::cin >> a2 >> ch >> b2;
RationalNumber r1(a1, b1);
RationalNumber r2(a2, b2);
std::cout << r1 << " + " << r2 << " = " << r1 + r2 << '\n';
std::cout << r1 << " - " << r2 << " = " << r1 - r2 << '\n';
std::cout << r1 << " * " << r2 << " = " << r1 * r2 << '\n';
std::cout << r1 << " / " << r2 << " = " << r1 / r2 << '\n';
return 0;
}
#include <iostream>
using namespace std;
class fenshu {
public:
fenshu(int n = 0, int d = 0): num(n), den(d) {}//分子n,分母d
fenshu operator+(fenshu& c2);
fenshu operator-(fenshu& c2);
fenshu operator*(fenshu& c2);
fenshu operator/(fenshu& c2);
int gys(int a, int b);
void display()const {
if (den == 1)
cout << num << endl;
else
cout << num << "/" << den << endl;
}
private:
int num, den; //分子分母
};
int fenshu::gys(int a, int b) {
int c;
while (b != 0) {
c = a % b;
a = b;
b = c;
}
return a > 0 ? a : -a;
}
fenshu fenshu::operator+(fenshu& c2) //加法的重载
{
int d3, num3, g;
d3 = den * c2.den;
num3 = num * c2.den + c2.num * den;
g = gys(d3, num3);
d3 = d3 / g;
num3 = num3 / g;
return fenshu(num3, d3);
}
fenshu fenshu::operator-(fenshu& c2) //减法的重载
{
int d3, num3, g;
d3 = den * c2.den;
num3 = num * c2.den - c2.num * den;
g = gys(d3, num3);
d3 = d3 / g;
num3 = num3 / g;
return fenshu(num3, d3);
}
fenshu fenshu::operator*(fenshu& c2) //乘法的重载
{
int d3, num3, g;
d3 = den * c2.den;
num3 = num * c2.num;
g = gys(d3, num3);
d3 = d3 / g;
num3 = num3 / g;
return fenshu(num3, d3);
}
fenshu fenshu::operator/(fenshu& c2) //除法的重载
{
int d3, num3, g;
d3 = den * c2.num;
num3 = num * c2.den;
g = gys(d3, num3);
d3 = d3 / g;
num3 = num3 / g;
return fenshu(num3, d3);
}
int main()
{
int d1, n1, d2, n2;
char c;
cout << "Input x: ";
cin >> d1 >> c >> n1;
cout << "Input y: ";
cin >> d2 >> c >> n2;
fenshu c1(d1, n1), c2(d2, n2), c3;
c3 = c1 + c2;
cout << "x+y=";
c3.display();
c3 = c1 - c2;
cout << "x-y=";
c3.display();
c3 = c1 * c2;
cout << "x*y=";
c3.display();
c3 = c1 / c2;
cout << "x/y=";
c3.display();
system("pause");
return 0;
}
如果对你有帮助,可以给我个采纳吗,谢谢!!