fork download
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. typedef long long ll;
  5. const int mod = 1e9 + 7;
  6.  
  7. class PhanSo{
  8. private:
  9. ll tu, mau;
  10. public:
  11. PhanSo(ll tu, ll mau);
  12. friend istream& operator >> (istream& in, PhanSo& x);
  13. friend ostream& operator << (ostream& out, PhanSo x);
  14. friend PhanSo operator + (PhanSo a, PhanSo b);
  15. };
  16.  
  17. PhanSo::PhanSo(ll tu, ll mau){
  18. this->tu = tu;
  19. this->mau = mau;
  20. }
  21.  
  22. istream& operator >> (istream& in, PhanSo& x){
  23. in >> x.tu >> x.mau;
  24. return in;
  25. }
  26.  
  27. ostream& operator << (ostream& out, PhanSo x){
  28. out << x.tu << "/" << x.mau;
  29. return out;
  30. }
  31.  
  32. PhanSo operator + (PhanSo a, PhanSo b){
  33. PhanSo t(1,1);
  34. ll mau_chung = a.mau * b.mau;
  35. ll tu_chung = a.tu * b.mau + a.mau * b.tu;
  36. ll gcd = __gcd(mau_chung, tu_chung);
  37. t.tu = tu_chung / gcd;
  38. t.mau = mau_chung / gcd;
  39. return t;
  40. }
  41.  
  42. int main() {
  43. PhanSo p(1,1), q(1,1);
  44. cin >> p >> q;
  45. cout << p + q;
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
2/1