Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Rational {
  5. public:
  6. Rational() {
  7. // Реализуйте конструктор по умолчанию
  8. numer = 0;
  9. denom = 1;
  10. }
  11.  
  12. Rational(int a, int b) {
  13. // Реализуйте конструктор
  14. int numerator, denominator, nod;
  15. numerator = a;
  16. denominator = b;
  17. a = abs(numerator);
  18. b = abs(denominator);
  19.  
  20. while (a > 0 && b > 0) {
  21. if (a > b) {
  22. a %= b;
  23. }
  24. else {
  25. b %= a;
  26. }
  27. }
  28. nod = a + b;
  29.  
  30. numer = numerator / nod;
  31. denom = denominator / nod;
  32.  
  33. if (numer*denom < 0) {
  34. if (denom < 0)
  35. denom *= (-1);
  36. if (numer >= 0) {
  37. numer *= (-1);
  38. }
  39.  
  40. }
  41. else {
  42. if (numer < 0) {
  43. numer *= (-1);
  44. }
  45. if (denom < 0) {
  46. denom *= (-1);
  47. }
  48. }
  49.  
  50.  
  51. }
  52.  
  53. int Numerator() const {
  54.  
  55. return numer;
  56.  
  57.  
  58. }
  59.  
  60. int Denominator() const {
  61.  
  62. return denom;
  63. }
  64.  
  65. private:
  66. // Добавьте поля
  67. int numer;
  68. int denom;
  69. };
  70. Rational operator+ ( Rational & left, Rational& right) {
  71. int denom_1, numer_1;
  72. denom_1 = left.Denominator()*right.Denominator();
  73. numer_1 = right.Numerator()*left.Denominator()+ right.Denominator()*left.Numerator();
  74. Rational answer(numer_1, denom_1);
  75. return answer;
  76. }
  77.  
  78. Rational operator- (Rational & left, Rational& right) {
  79. int denom_1, numer_1;
  80. denom_1 = left.Denominator()*right.Denominator();
  81. numer_1 = left.Numerator()*right.Denominator() -right.Numerator()*left.Denominator();
  82. Rational answer(numer_1, denom_1);
  83. return answer;
  84. }
  85.  
  86. bool operator==( const Rational & left,const Rational& right) {//left<right
  87. if ((left.Numerator() == right.Numerator()) && (left.Denominator() == right.Denominator())) {
  88. return true;
  89. }
  90. return false;
  91. }
  92.  
  93. void printrational(const Rational & rational) {
  94. cout << rational.Numerator() << "/" << rational.Denominator()<<endl;
  95. }
  96. // Реализуйте для класса Rational операторы ==, + и -
  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.  
  132. cout << "OK" << endl;
  133. //system("pause");
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement