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(){
  12. }
  13. PhanSo(ll tu, ll mau);
  14. friend istream& operator >> (istream& in, PhanSo& x);
  15. friend ostream& operator << (ostream& out, PhanSo x);
  16. friend PhanSo operator + (PhanSo a, PhanSo b);
  17. };
  18.  
  19. PhanSo::PhanSo(ll tu, ll mau){
  20. this->tu = tu;
  21. this->mau = mau;
  22. }
  23.  
  24. istream& operator >> (istream& in, PhanSo& x){
  25. in >> x.tu >> x.mau;
  26. return in;
  27. }
  28.  
  29. ostream& operator << (ostream& out, PhanSo x){
  30. out << x.tu << "/" << x.mau;
  31. return out;
  32. }
  33.  
  34. PhanSo operator + (PhanSo a, PhanSo b){
  35. PhanSo t;
  36. ll mau_chung = a.mau * b.mau;
  37. ll tu_chung = a.tu * b.mau + a.mau * b.tu;
  38. ll gcd = __gcd(mau_chung, tu_chung);
  39. t.tu = tu_chung / gcd;
  40. t.mau = mau_chung / gcd;
  41. return t;
  42. }
  43.  
  44. int main() {
  45. PhanSo p(1,1), q(1,1);
  46. cin >> p >> q;
  47. cout << p + q;
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2/1