Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class Rational {
  9. public:
  10. Rational() {
  11. // Реализуйте конструктор по умолчанию
  12. n = 0; // числитель делимое numerator
  13. d = 1; // знаменатель делитель denominator
  14. com_d = 1; // общий макс делитель
  15. }
  16. Rational(int numerator, int denominator) {
  17. Initialize(numerator, denominator);
  18. }
  19.  
  20. void Initialize(int numerator, int denominator) {
  21. // Реализуйте конструктор
  22. // Если числитель дроби равен нулю, то знаменатель должен быть равен 1.
  23. if (numerator == 0) {
  24. n = numerator;
  25. d = 1;
  26. com_d = 1;
  27. }
  28. // если оба числа отрицательные - то дробь положительная
  29. else if (numerator < 0 && denominator < 0) {
  30. n = abs(numerator);
  31. d = abs(denominator);
  32. }
  33. else if (numerator < 0 || denominator < 0)
  34. {
  35. n = abs(numerator) * (-1);
  36. d = abs(denominator);
  37. }
  38. else {
  39. n = numerator;
  40. d = denominator;
  41.  
  42. }
  43. com_d = ComD();
  44.  
  45. if (com_d > 1) {
  46. int a = n / com_d;
  47. int b = d / com_d;
  48. n = a;
  49. d = b;
  50. }
  51. }
  52. int ComD() const {
  53. int a = abs(n);
  54. int b = abs(d);
  55. while (a != 0 && b != 0) {
  56. if (a > b) {
  57. a = a % b;
  58. }
  59. else {
  60. b = b % a;
  61. }
  62. }
  63. return a + b;
  64. }
  65.  
  66. int N() const {
  67. return n ;
  68. }
  69.  
  70. int D() const {
  71. return d ;
  72. }
  73.  
  74.  
  75. int Numerator() const {
  76. // Реализуйте этот метод
  77. return n / com_d; // возвращает наименьшее делимое - сокращает дробь
  78. }
  79.  
  80. int Denominator() const {
  81. // Реализуйте этот метод
  82. return d / com_d; // возвращает наименьший делитель - сокращает дробь
  83. }
  84.  
  85. // Реализуйте для класса Rational операторы ==, + и -
  86.  
  87.  
  88. // оператор + бинарный следовательно a + b = a.operator+(b) значит передавать нужно один параметр правосторонний
  89. // this->value + a.value в нашем случае к первому слагаемому можно просто обращаться как n и d а второму x.n и x.d
  90. Rational operator+(const Rational &x) const {
  91. int a, b;
  92. if (x.d == d) {
  93. a = n + x.n;
  94. b = d;
  95. }
  96. else {
  97. a = (x.n * d) + (n * x.d);
  98. b = x.d * d;
  99. }
  100. Rational sum(a, b);
  101. return sum;
  102. }
  103.  
  104. Rational operator-(const Rational &x) const {
  105. int a, b;
  106. if (x.d == d) {
  107. a = n - x.n;
  108. b = d;
  109. }
  110. else {
  111. a = (n * x.d) - (x.n * d);
  112. b = x.d * d;
  113. }
  114. Rational min(a, b);
  115. return min;
  116. }
  117.  
  118.  
  119. const bool operator == (const Rational &a) const
  120. {
  121. if ((a.n == n) && (a.d == d))
  122. return true;
  123. else return false;
  124. }
  125.  
  126. // Реализуйте для класса Rational операторы * и /
  127. Rational operator * (const Rational &x) const {
  128. int a, b;
  129. a = n * x.n;
  130. b = d * x.d;
  131. Rational mult(a, b);
  132. return mult;
  133. }
  134.  
  135. Rational operator / (const Rational &x) const {
  136. int a, b;
  137. a = n * x.d;
  138. b = d * x.n;
  139. Rational div(a, b);
  140. return div;
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. private:
  148. // Добавьте поля
  149. int n;
  150. int d;
  151. int com_d;
  152. };
  153.  
  154. // Реализуйте для класса Rational операторы << и >>
  155. // оператор вывода в поток
  156. ostream& operator << (ostream &stream, const Rational &rational){
  157. stream << rational.N() << '/' << rational.D();
  158. return stream;
  159. }
  160.  
  161. // оператор ввода из потока
  162. istream& operator >> (istream &stream, Rational &rational){
  163. if ( !stream.eof() ) {
  164. int a = 0;
  165. int b = 0 ;
  166. stream >> a;
  167. stream.ignore(1); // пропускаем разделитель
  168. stream >> b;
  169. rational.Initialize(a, b);
  170. }
  171.  
  172. return stream;
  173. }
  174.  
  175.  
  176. int main() {
  177. {
  178. ostringstream output;
  179. output << Rational(-6, 8);
  180. if (output.str() != "-3/4") {
  181. cout << "Rational(-6, 8) should be written as "-3/4"" << endl;
  182. return 1;
  183. }
  184. }
  185.  
  186. {
  187. istringstream input("5/7");
  188. Rational r;
  189. input >> r;
  190. bool equal = r == Rational(5, 7);
  191. if (!equal) {
  192. cout << "5/7 is incorrectly read as " << r << endl;
  193. return 2;
  194. }
  195. }
  196.  
  197. {
  198. istringstream input("5/7 10/8");
  199. Rational r1, r2;
  200. input >> r1 >> r2;
  201. bool correct = r1 == Rational(5, 7) && r2 == Rational(5, 4);
  202. if (!correct) {
  203. cout << "Multiple values are read incorrectly: " << r1 << " " << r2 << endl;
  204. return 3;
  205. }
  206.  
  207. input >> r1;
  208. input >> r2;
  209. correct = r1 == Rational(5, 7) && r2 == Rational(5, 4);
  210. if (!correct) {
  211. cout << "Read from empty stream shouldn't change arguments: " << r1 << " " << r2 << endl;
  212. return 4;
  213. }
  214. }
  215.  
  216. cout << "OK" << endl;
  217. return 0;
  218. }
  219.  
  220. {
  221. istringstream input("abc");
  222. Rational r;
  223. input >> r;
  224. bool equal = r == Rational(0, 1);
  225. if (!equal) {
  226. cout << "Read from stream works incorrectly " << r << endl;
  227. return 5;
  228. }
  229. }
  230.  
  231. {
  232. istringstream input("");
  233. Rational r;
  234. input >> r;
  235. bool equal = r == Rational(0, 1);
  236. if (!equal) {
  237. cout << "Read from empty stream works incorrectly " << r << endl;
  238. return 6;
  239. }
  240. }
  241.  
  242. return 3;
  243. }
  244.  
  245. input >> r1;
  246. input >> r2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement