Добавил:
TaskeFox
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <iostream>
using namespace std;
class Pair abstract
{
public:
double first;
double second;
Pair() {}
Pair(double first, double second) : first(first), second(second) {}
virtual Pair* plus(Pair *obj) abstract;
virtual Pair* sub(Pair *obj) abstract;
virtual Pair* mult(Pair *obj) abstract;
virtual Pair* div(Pair *obj) abstract;
};
class Rational : public Pair
{
public:
Rational(double a, double b) : Pair(a, b) { }
Pair* plus(Pair* obj) override
{
if (this->second == obj->second)
{
return new Rational(this->first + obj->first, this->second);
}
else
{
return new Rational(this->first * obj->second + obj->first*this->second, this->second*obj->second);
}
}
Pair* sub(Pair* obj) override
{
if (this->second == obj->second)
{
return new Rational(this->first - obj->first, this->second);
}
else
{
return new Rational(this->first * obj->second - obj->first*this->second, this->second*obj->second);
}
}
Pair* mult(Pair* obj) override
{
return new Rational(this->first * obj->first, this->second*obj->second);
}
Pair* div(Pair* obj) override
{
return new Rational(this->first*obj->second, this->second*obj->first);
}
void Print()const
{
cout << first << " / " << second << endl;
}
};
class Complex : public Pair
{
public:
Complex(double a, double b) : Pair(a, b) { }
Pair* plus(Pair* obj) override
{
return new Complex(this->first + obj->first, this->second + obj->second);
}
Pair* sub(Pair* obj) override
{
return new Complex(this->first - obj->first, this->second - obj->second);
}
Pair* mult(Pair* obj) override
{
return new Complex(this->first * obj->first - this->second * obj->second, this->first*obj->second + this->second*obj->first);
}
Pair* div(Pair* obj) override
{
return new Complex((this->first * this->second + obj->first * obj->second)/(obj->first*obj->first + obj->second*obj->second), (this->second*obj->first - this->first*obj->second)/(obj->first*obj->first + obj->second*obj->second));
}
void Print()const
{
cout << this->first << " + " << this->second << "i" << endl;
}
};
int main()
{
cout << "RATIONAL " << endl;
Rational a(1.0, 2.0);
Rational b(4.0, 2.0);
cout << "a : ";
a.Print();
cout << "b : ";
b.Print();
cout << endl;
cout << "a + b : ";
dynamic_cast<Rational*>(a.plus(&b))->Print();
cout << endl;
cout << "a - b : ";
dynamic_cast<Rational*>(a.sub(&b))->Print();
cout << endl;
cout << "a * b : ";
dynamic_cast<Rational*>(a.mult(&b))->Print();
cout << endl;
cout << "a / b : ";
dynamic_cast<Rational*>(a.div(&b))->Print();
cout << endl;
cout << endl;
cout << endl;
cout << "COMPLEX " << endl;
Complex ac(5,-6);
Complex bc(-3.0, 2.0);
cout << "a : ";
ac.Print();
cout << "b : ";
bc.Print();
cout << endl;
cout << "a + b : ";
dynamic_cast<Complex*>(ac.plus(&bc))->Print();
cout << endl;
cout << "a - b : ";
dynamic_cast<Complex*>(ac.sub(&bc))->Print();
cout << endl;
cout << "a * b : ";
dynamic_cast<Complex*>(ac.mult(&bc))->Print();
cout << endl;
cout << "a / b : ";
dynamic_cast<Complex*>(ac.div(&bc))->Print();
cout << endl;
system("pause");
return 0;
}