分别用成员函数和友元函数重载运算符,使对整型的运算符=、+、-、*、/适用于分数运算。要求:(1)输出结果是最简分数(可以是带分数);(2)分母为1,只输出分子。
2条回答 默认 最新
- _GX_ 2021-12-14 06:02关注
// 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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用