fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Fraction {
  4. protected:
  5. int n, d;
  6. int gcd(int N, int D) { return (N == 0? D: gcd(D%N, N)); }
  7. void restrict() {int t = gcd(this->n, this->d); this->n =this->n / t; this->d /= t;}
  8. public:
  9.  
  10. // Fraction(int N = 1, int D = 1) {int t = gcd(N,D); n = N/t; d = D/t;}
  11. Fraction(int n=1, int d=1 ): n(n), d(d) {restrict();}
  12. int getD() {return d;}
  13. int getN() {return n;}
  14.  
  15. // x * 2 - працює
  16. // 2 * x - не працює,якщо операцыя оголошена як метод класу, а не friend функція
  17. // const Fraction operator*(const Fraction & y) { return Fraction(n * y.n, d * y.d); }
  18. friend Fraction operator*(const Fraction &, const Fraction &);
  19. const Fraction operator/(const Fraction & y) { return Fraction(n * y.d, d * y.n); }
  20.  
  21.  
  22.  
  23. // const Fraction operator+(const Fraction & rhs) {
  24. //return Fraction(this->n * rhs.d + this->d * rhs.n, this->d * rhs.d); }
  25.  
  26.  
  27. const Fraction operator-(const Fraction & y) { return Fraction(n * y.d - d * y.n, d * y.d); }
  28. const Fraction operator+(const Fraction & y) { return Fraction(n * y.d + d * y.n, d * y.d); }
  29. const Fraction operator*(const Fraction & y) { return Fraction(n * y.n, d * y.d); }
  30.  
  31.  
  32. const Fraction operator-() { return Fraction(-n, d); }
  33. Fraction operator*=(const Fraction & y) { n *= y.n; d *= y.d; restrict(); return *this;}
  34. Fraction operator++() { n += d; return *this;}
  35.  
  36. const Fraction operator++(int) {n += d; return Fraction(n-d, d); }
  37.  
  38.  
  39. friend std::ostream & operator<<(std::ostream &, const Fraction);
  40.  
  41.  
  42.  
  43.  
  44.  
  45. friend std::istream & operator>> (std::istream &, Fraction &);
  46.  
  47. int sum(int b){
  48. return this->n+b;
  49. }
  50.  
  51.  
  52. };
  53.  
  54. class T:public Fraction{
  55. private:
  56. int w;
  57. public:
  58. T(int c,int n,int d):Fraction(c*d+n,d){
  59.  
  60. }
  61. };
  62. Fraction operator*(const Fraction & x, const Fraction & y) { return Fraction(x.n * y.n, x.d * y.d); }
  63.  
  64. std::ostream & operator<< (std::ostream & out, const Fraction x) {
  65. out << x.n << "/" << x.d;
  66. //out<<"Hello";
  67. return out;
  68. }
  69.  
  70. std::istream & operator>> (std::istream & in, Fraction & x) {
  71. int N, D;
  72. in >> N >> D;
  73. x = Fraction(N, D);
  74. return in;
  75. }
  76.  
  77.  
  78. int main() {
  79. Fraction x(1,3),y(1,2);
  80. cout<<y+x<<endl;
  81. cout<<-x<<endl;
  82. cout<<y;
  83. // cout<<x+y<<"123";
  84. // cout<<y<<endl;
  85. //cout<<endl;
  86. //cout<<x.sum(5);
  87. // cout << 2 * y << '\n'; //чому тут немає помилки, як працює?
  88. // cin >> z;
  89. // cout<<z<<endl;
  90. // std::cout << x + y << '\n';
  91. // z *= 2;
  92. // std::cout << z << '\n';
  93. // std::cout << ++z << '\n';
  94. // std::cout << z++ << '\n';
  95. // std::cout << z << '\n';
  96. // T t(1,2,3);
  97. // std::cout<<t;
  98. }
  99.  
  100. /*
  101. План:
  102. +.-,*./, %. +
  103.  
  104. 1. мінус
  105. 2. Унарнний мінус
  106. 3. x *= 2
  107. 4. ++x, x++
  108. 5. 2 + x
  109. */
Success #stdin #stdout 0s 5276KB
stdin
2   3
stdout
5/6
1/-3
1/2