Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- using namespace std;
- class Rational {
- public:
- Rational() {
- // Реализуйте конструктор по умолчанию
- n = 0; // числитель делимое numerator
- d = 1; // знаменатель делитель denominator
- com_d = 1; // общий макс делитель
- }
- Rational(int numerator, int denominator) {
- Initialize(numerator, denominator);
- }
- void Initialize(int numerator, int denominator) {
- // Реализуйте конструктор
- // Если числитель дроби равен нулю, то знаменатель должен быть равен 1.
- if (numerator == 0) {
- n = numerator;
- d = 1;
- com_d = 1;
- }
- // если оба числа отрицательные - то дробь положительная
- else if (numerator < 0 && denominator < 0) {
- n = abs(numerator);
- d = abs(denominator);
- }
- else if (numerator < 0 || denominator < 0)
- {
- n = abs(numerator) * (-1);
- d = abs(denominator);
- }
- else {
- n = numerator;
- d = denominator;
- }
- com_d = ComD();
- if (com_d > 1) {
- int a = n / com_d;
- int b = d / com_d;
- n = a;
- d = b;
- }
- }
- int ComD() const {
- int a = abs(n);
- int b = abs(d);
- while (a != 0 && b != 0) {
- if (a > b) {
- a = a % b;
- }
- else {
- b = b % a;
- }
- }
- return a + b;
- }
- int N() const {
- return n ;
- }
- int D() const {
- return d ;
- }
- int Numerator() const {
- // Реализуйте этот метод
- return n / com_d; // возвращает наименьшее делимое - сокращает дробь
- }
- int Denominator() const {
- // Реализуйте этот метод
- return d / com_d; // возвращает наименьший делитель - сокращает дробь
- }
- // Реализуйте для класса Rational операторы ==, + и -
- // оператор + бинарный следовательно a + b = a.operator+(b) значит передавать нужно один параметр правосторонний
- // this->value + a.value в нашем случае к первому слагаемому можно просто обращаться как n и d а второму x.n и x.d
- Rational operator+(const Rational &x) const {
- int a, b;
- if (x.d == d) {
- a = n + x.n;
- b = d;
- }
- else {
- a = (x.n * d) + (n * x.d);
- b = x.d * d;
- }
- Rational sum(a, b);
- return sum;
- }
- Rational operator-(const Rational &x) const {
- int a, b;
- if (x.d == d) {
- a = n - x.n;
- b = d;
- }
- else {
- a = (n * x.d) - (x.n * d);
- b = x.d * d;
- }
- Rational min(a, b);
- return min;
- }
- const bool operator == (const Rational &a) const
- {
- if ((a.n == n) && (a.d == d))
- return true;
- else return false;
- }
- // Реализуйте для класса Rational операторы * и /
- Rational operator * (const Rational &x) const {
- int a, b;
- a = n * x.n;
- b = d * x.d;
- Rational mult(a, b);
- return mult;
- }
- Rational operator / (const Rational &x) const {
- int a, b;
- a = n * x.d;
- b = d * x.n;
- Rational div(a, b);
- return div;
- }
- private:
- // Добавьте поля
- int n;
- int d;
- int com_d;
- };
- // Реализуйте для класса Rational операторы << и >>
- // оператор вывода в поток
- ostream& operator << (ostream &stream, const Rational &rational){
- stream << rational.N() << '/' << rational.D();
- return stream;
- }
- // оператор ввода из потока
- istream& operator >> (istream &stream, Rational &rational){
- if ( !stream.eof() ) {
- int a = 0;
- int b = 0 ;
- stream >> a;
- stream.ignore(1); // пропускаем разделитель
- stream >> b;
- rational.Initialize(a, b);
- }
- return stream;
- }
- int main() {
- {
- ostringstream output;
- output << Rational(-6, 8);
- if (output.str() != "-3/4") {
- cout << "Rational(-6, 8) should be written as "-3/4"" << endl;
- return 1;
- }
- }
- {
- istringstream input("5/7");
- Rational r;
- input >> r;
- bool equal = r == Rational(5, 7);
- if (!equal) {
- cout << "5/7 is incorrectly read as " << r << endl;
- return 2;
- }
- }
- {
- istringstream input("5/7 10/8");
- Rational r1, r2;
- input >> r1 >> r2;
- bool correct = r1 == Rational(5, 7) && r2 == Rational(5, 4);
- if (!correct) {
- cout << "Multiple values are read incorrectly: " << r1 << " " << r2 << endl;
- return 3;
- }
- input >> r1;
- input >> r2;
- correct = r1 == Rational(5, 7) && r2 == Rational(5, 4);
- if (!correct) {
- cout << "Read from empty stream shouldn't change arguments: " << r1 << " " << r2 << endl;
- return 4;
- }
- }
- cout << "OK" << endl;
- return 0;
- }
- {
- istringstream input("abc");
- Rational r;
- input >> r;
- bool equal = r == Rational(0, 1);
- if (!equal) {
- cout << "Read from stream works incorrectly " << r << endl;
- return 5;
- }
- }
- {
- istringstream input("");
- Rational r;
- input >> r;
- bool equal = r == Rational(0, 1);
- if (!equal) {
- cout << "Read from empty stream works incorrectly " << r << endl;
- return 6;
- }
- }
- return 3;
- }
- input >> r1;
- input >> r2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement