Fork Copy #include using namespace std; int UCLN(int a, int b) { while (b) { int r = a % b; a = b; b = r; } return a; } int BCNN(int a, int b) { return a * b / UCLN(a, b); } class PhanSo { private: int tuso, mauso; public: PhanSo(int tuso, int mauso) : tuso(tuso), mauso(mauso) { } PhanSo() : PhanSo(0, 1) { } PhanSo rutgon() { int ucln = UCLN(tuso, mauso); tuso /= ucln; mauso /= ucln; return *this; } friend PhanSo operator+(const PhanSo& lps, const PhanSo& rps) { int mauchung = BCNN(lps.mauso, rps.mauso); int tuso = mauchung / lps.mauso * lps.tuso + mauchung / rps.mauso * rps.tuso; return PhanSo(tuso, mauchung).rutgon(); } friend istream& operator>>(istream& is, PhanSo& ps) { return is >> ps.tuso >> ps.mauso; } friend ostream& operator<<(ostream& os, const PhanSo& ps) { return os << ps.tuso << "/" << ps.mauso; } }; int main(int argc, char const* argv[]) { PhanSo ps1, ps2; cin >> ps1 >> ps2; cout << ps1 << " + " << ps2 << " = " << ps1 + ps2; return 0; }