MyTetra Share Делитесь знаниями!
 Класс Rational с перегрузкой (часть 4)
Время создания: 14.08.2017 23:14
Раздел: Computer - Programming - C++ - Coursera White_Belt

#include <iostream>

#include <sstream>

#include <vector>

#include <set>

#include <map>

using namespace std;

int Divider(const int& n, const int& d) {

if(n == d)

{

return n;

}

else if(n == 0)

{

return n;

}

else

{

int dividend_t = 0;

int divider_t = 0;

int reminder = 0;

if(n > d)

{

dividend_t = n;

divider_t = d;

}

else

{

dividend_t = d;

divider_t = n;

}

reminder = dividend_t % divider_t;

while(reminder != 0)

{

dividend_t = divider_t;

divider_t = reminder;

reminder = dividend_t % divider_t;

}

if(divider_t < 0)

return -divider_t;

else return divider_t;

}

}

class Rational {

public:

Rational() {

p = 0;

q = 1;

}

Rational(int numerator, int denominator) {

Initialize(numerator, denominator);

}

void Initialize(const int& n, const int& d)

{

p = n;

q = d;

SignedFactional();

}

void SignedFactional()

{

if(p == 0)

{

q = 1;

}

else if(q < 0)

{

p = -p;

q = -q;

}

int divider = Divider(p, q);

p /= divider;

q /= divider;

}

int Numerator() const {

return p;

}

int Denominator() const {

return q;

}

const bool operator == (const Rational& a) const

{

if((a.p == p))

return true;

else return false;

}

const bool operator > (const Rational& a) const

{

if((p * a.q) > (q * a.p))

return true;

else return false;

}

const bool operator < (const Rational& a) const

{

if((p * a.q) < (q * a.p))

return true;

else return false;

}

Rational operator + (const Rational& x)

{

Rational result;

if(x.q == q)

{

result.p = x.p + p;

result.q = x.q;

}

else

{

result.p = (x.p * q) + (p * x.q);

result.q = x.q * q;

}

result.SignedFactional();

return result;

}

Rational operator - (const Rational& x)

{

Rational result;

if(x.q == q)

{

result.p = p - x.p;

result.q = x.q;

}

else

{

result.p = (p * x.q) - (x.p * q);

result.q = x.q * q;

}

result.SignedFactional();

return result;

}

Rational operator * (const Rational& x)

{

Rational result;

result.p = x.p * p;

result.q = x.q * q;

result.SignedFactional();

return result;

}

Rational operator / (const Rational& x)

{

Rational result;

result.p = x.q * p;

result.q = x.p * q;

result.SignedFactional();

return result;

}

private:

int p;

int q;

};

istream& operator >> (istream& stream, Rational& r)

{

int p_t, q_t;

stream >> p_t;

stream.ignore(1); //ignore '/'

stream >> q_t;

r.Initialize(p_t, q_t);

return stream;

}

ostream& operator << (ostream& stream, const Rational& r)

{

stream << r.Numerator() << '/' << r.Denominator();

return stream;

}

int main() {

{

const set<Rational> rs = {{1, 2}, {1, 25}, {9, 12}, {3, 4}, {1, 2}};

// for(const Rational& i : rs)

// cout << i << " ";

// cout << rs.size() << endl;

if (rs.size() != 3) {

cout << "Wrong amount of items in the set" << endl;

return 1;

}

vector<Rational> v;

for (auto x : rs) {

v.push_back(x);

}

if (v != vector<Rational>{{1, 25}, {1, 2}, {3, 4}}) {

cout << "Rationals comparison works incorrectly" << endl;

return 2;

}

}

{

map<Rational, int> count;

++count[{1, 2}];

++count[{1, 2}];

++count[{2, 3}];

// cout << count.size() << endl;

if (count.size() != 2) {

cout << "Wrong amount of items in the map" << endl;

return 3;

}

}

cout << "OK" << endl;

getchar();

return 0;

}

 MyTetra Share v.0.53