Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. class Rational {
  6. public:
  7. Rational() {
  8. // Реализуйте конструктор по умолчанию
  9. p=0;
  10. q=1;
  11. //для отладки
  12. //cout << "Hi, I am a Default Counstructor" << endl;
  13. }
  14.  
  15. Rational(int numerator, int denominator) {
  16. // Реализуйте конструктор
  17. if (numerator==0) {
  18. p=0;
  19. q=1;
  20. } else {
  21. int s = FindSign(numerator, denominator);
  22. int nod=FindNod(numerator,denominator);
  23. numerator=abs(numerator);
  24. denominator=abs(denominator);
  25. p=numerator/nod;
  26. q=denominator/nod;
  27.  
  28. p=p*s;
  29. }
  30. //для отладки
  31. //cout << p << "/" << q << endl;
  32. }
  33.  
  34. int Numerator() const {
  35. // Реализуйте этот метод
  36. return p;
  37. }
  38.  
  39. int Denominator() const {
  40. // Реализуйте этот метод
  41. return q;
  42. }
  43.  
  44. private:
  45. // Добавьте поля
  46. int p, q;
  47.  
  48. int FindNod (int a, int b) {
  49. int nod=1;
  50. int i = 1;
  51. while(i!=0) {
  52. i=(a % b);
  53. nod=b;
  54. a=b;
  55. b=i;
  56. }
  57. return nod;
  58. }
  59.  
  60. int FindSign (int c, int d) {
  61. int mult = c*d;
  62. if (mult >=0) {
  63. return 1;
  64. } else {
  65. return -1;
  66. }
  67. }
  68.  
  69. };
  70.  
  71. bool operator == (const Rational r1, const Rational r2) {
  72. if (r1.Numerator()==r2.Numerator ()&& r1.Denominator()==r2.Denominator()) {
  73. return 1;
  74. } else {
  75. return 0;
  76. }
  77. }
  78.  
  79. Rational operator + (const Rational r1, const Rational r2) {
  80. if (r1.Denominator()==r2.Denominator()) {
  81. return Rational(r1.Numerator()+r2.Numerator(), r1.Denominator());
  82. } else {
  83. return Rational(r1.Numerator()*r2.Denominator() + r2.Numerator()*r1.Denominator(), r1.Denominator()*r2.Denominator());
  84.  
  85. }
  86. }
  87.  
  88. Rational operator - (const Rational r1, const Rational r2) {
  89. if (r1.Denominator()==r2.Denominator()) {
  90. return Rational (r1.Numerator()-r2.Numerator(), r1.Denominator());
  91. } else {
  92. return Rational (r1.Numerator()*r2.Denominator()-r2.Numerator()*r1.Denominator(), r1.Denominator()*r2.Denominator());
  93. }
  94. }
  95.  
  96.  
  97.  
  98. int main() {
  99. {
  100. Rational r1(4, 6);
  101. Rational r2(2, 3);
  102. bool equal = r1 == r2;
  103. if (!equal) {
  104. cout << "4/6 != 2/3" << endl;
  105. return 1;
  106. }
  107. }
  108.  
  109. {
  110. Rational a(2, 3);
  111. Rational b(4, 3);
  112. Rational c = a + b;
  113. bool equal = c == Rational(2, 1);
  114. if (!equal) {
  115. cout << "2/3 + 4/3 != 2" << endl;
  116. return 2;
  117. }
  118. }
  119.  
  120. {
  121. Rational a(5, 7);
  122. Rational b(2, 9);
  123. Rational c = a - b;
  124. bool equal = c == Rational(31, 63);
  125. if (!equal) {
  126. cout << "5/7 - 2/9 != 31/63" << endl;
  127. return 3;
  128. }
  129. }
  130.  
  131. cout << "OK" << endl;
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement