|  | @@ -0,0 +1,361 @@
 | 
	
		
			
				|  |  | +#include "Rational.h"
 | 
	
		
			
				|  |  | +#include <iostream>
 | 
	
		
			
				|  |  | +#include <cmath>
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +using namespace std;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +int max(int a, int b) {
 | 
	
		
			
				|  |  | +	if (a >= b)
 | 
	
		
			
				|  |  | +		return a;
 | 
	
		
			
				|  |  | +	return b;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +int min(int a, int b) {
 | 
	
		
			
				|  |  | +	if (a <= b)
 | 
	
		
			
				|  |  | +		return a;
 | 
	
		
			
				|  |  | +	return b;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +Rational::Rational() {
 | 
	
		
			
				|  |  | +	Chisl = 0;
 | 
	
		
			
				|  |  | +	Znam = 1;
 | 
	
		
			
				|  |  | +	sign = 0;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +Rational::Rational(int _x) {
 | 
	
		
			
				|  |  | +	Chisl = _x;
 | 
	
		
			
				|  |  | +	Znam = 1;
 | 
	
		
			
				|  |  | +	sign = _x < 0;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +Rational::Rational(int _x, int _y) {
 | 
	
		
			
				|  |  | +	sign = !((_x >=0 && _y >= 0) || (_x<0 && _y<0));
 | 
	
		
			
				|  |  | +	Chisl = _x>=0 ? _x:-_x;
 | 
	
		
			
				|  |  | +	Znam = _y>=0 ?_y:-_y;
 | 
	
		
			
				|  |  | +	;
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +Rational::Rational(double _x) {
 | 
	
		
			
				|  |  | +	sign = _x < 0;
 | 
	
		
			
				|  |  | +	int dec = 1000000;
 | 
	
		
			
				|  |  | +	Chisl = dec * _x;
 | 
	
		
			
				|  |  | +	Znam = dec;
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//ñîêðàùåíèå
 | 
	
		
			
				|  |  | +void
 | 
	
		
			
				|  |  | +Rational::socr() {
 | 
	
		
			
				|  |  | +	sign = (!((Chisl >= 0 && Znam >= 0) || (Chisl < 0 && Znam < 0)))||sign;
 | 
	
		
			
				|  |  | +	Chisl = Chisl >= 0 ? Chisl : -Chisl;
 | 
	
		
			
				|  |  | +	Znam = Znam >= 0 ? Znam : -Znam;
 | 
	
		
			
				|  |  | +	int a, b;
 | 
	
		
			
				|  |  | +	a = Chisl;
 | 
	
		
			
				|  |  | +	b = Znam;
 | 
	
		
			
				|  |  | +	int end = 0;
 | 
	
		
			
				|  |  | +	while (a != 0 && b != 0 && end!=100000) {
 | 
	
		
			
				|  |  | +		end++;
 | 
	
		
			
				|  |  | +		if (a > b) {
 | 
	
		
			
				|  |  | +			a %= b;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		else
 | 
	
		
			
				|  |  | +			b %= a;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	if (end != 100000) {
 | 
	
		
			
				|  |  | +		Chisl /= (a + b);
 | 
	
		
			
				|  |  | +		Znam /= (a + b);
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//êîðåíü
 | 
	
		
			
				|  |  | +const double
 | 
	
		
			
				|  |  | +Rational::root() {
 | 
	
		
			
				|  |  | +	return sqrt(Chisl) / sqrt(Znam);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//îïåðàòîðû ôîðìàòà *=
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator += (const Rational& r) {
 | 
	
		
			
				|  |  | +	int obsh_znam = Znam * r.Znam;
 | 
	
		
			
				|  |  | +	int mod1, mod2;
 | 
	
		
			
				|  |  | +	mod1 = sign ? -1 : 1;
 | 
	
		
			
				|  |  | +	mod2 = r.sign ? -1 : 1;
 | 
	
		
			
				|  |  | +	Chisl = Chisl * r.Znam * mod1 + r.Chisl * Znam * mod2;
 | 
	
		
			
				|  |  | +	Znam = obsh_znam;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	sign = Chisl < 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator -= (const Rational& r) {
 | 
	
		
			
				|  |  | +	int obsh_znam = Znam * r.Znam;
 | 
	
		
			
				|  |  | +	int mod1, mod2;
 | 
	
		
			
				|  |  | +	mod1 = sign ? -1 : 1;
 | 
	
		
			
				|  |  | +	mod2 = r.sign ? -1 : 1;
 | 
	
		
			
				|  |  | +	Chisl = Chisl * r.Znam * mod1 - r.Chisl * Znam * mod2;
 | 
	
		
			
				|  |  | +	Znam = obsh_znam;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	sign = (Chisl < 0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	Chisl = Chisl >= 0 ? Chisl : -Chisl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator*=(const Rational& r) {
 | 
	
		
			
				|  |  | +	Chisl = Chisl * r.Chisl;
 | 
	
		
			
				|  |  | +	Znam = Znam * r.Znam;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	sign = !(sign == r.sign);
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator/=(const Rational& r) {
 | 
	
		
			
				|  |  | +	Chisl = Chisl * r.Znam;
 | 
	
		
			
				|  |  | +	Znam = Znam * r.Chisl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	sign = !(sign == r.sign);
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//ñðàâíåíèå
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator < (const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +	r1.Chisl *= r2.Znam;
 | 
	
		
			
				|  |  | +	r2.Chisl *= r1.Znam;
 | 
	
		
			
				|  |  | +	return r1.Chisl < r2.Chisl;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator > (const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +	r1.Chisl *= r2.Znam;
 | 
	
		
			
				|  |  | +	r2.Chisl *= r1.Znam;
 | 
	
		
			
				|  |  | +	return r1.Chisl > r2.Chisl;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator >= (const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return !(r1 < r2);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator<=(const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return !(r1 > r2);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator==(const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +	r1.Chisl *= r2.Znam;
 | 
	
		
			
				|  |  | +	r2.Chisl *= r1.Znam;
 | 
	
		
			
				|  |  | +	return r1.Chisl == r2.Chisl;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const bool&
 | 
	
		
			
				|  |  | +Rational::operator!=(const Rational& r) {
 | 
	
		
			
				|  |  | +	Rational r1 = *this;
 | 
	
		
			
				|  |  | +	Rational r2 = r;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return !(r1 == r2);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//ñëîæåíèå óìíîæåíèå
 | 
	
		
			
				|  |  | +const Rational& 
 | 
	
		
			
				|  |  | +Rational::operator + (const Rational& r1) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res += r1;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator - (const Rational& r1) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res -= r1;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator * (const Rational& r1) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res *= r1;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator / (const Rational& r1) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res /= r1;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//ñ äðóãèì òèïîì ïåðåìåííûõ
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator *= (const int& r) {
 | 
	
		
			
				|  |  | +	Chisl *= r;
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator/=(const int& r) {
 | 
	
		
			
				|  |  | +	Znam *= r;
 | 
	
		
			
				|  |  | +	socr();
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator+=(const int& r) {
 | 
	
		
			
				|  |  | +	Rational  r_t = Rational(r);
 | 
	
		
			
				|  |  | +	*this += r_t;
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator-=(const int& r) {
 | 
	
		
			
				|  |  | +	Rational  r_t = Rational(r);
 | 
	
		
			
				|  |  | +	*this -= r_t;
 | 
	
		
			
				|  |  | +	return *this;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator * ( const int& i) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res *= i;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator + (const int& i) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res += i;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator - (const int& i) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res -= i;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const Rational&
 | 
	
		
			
				|  |  | +Rational::operator / (const int& i) {
 | 
	
		
			
				|  |  | +	Rational res = *this;
 | 
	
		
			
				|  |  | +	res /= i;
 | 
	
		
			
				|  |  | +	return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +istream&
 | 
	
		
			
				|  |  | +operator >>(istream& is, Rational& r) {
 | 
	
		
			
				|  |  | +	char input[100];
 | 
	
		
			
				|  |  | +	r.Chisl = 0;
 | 
	
		
			
				|  |  | +	r.Znam = 1;
 | 
	
		
			
				|  |  | +	r.sign = false;
 | 
	
		
			
				|  |  | +	is >> input;
 | 
	
		
			
				|  |  | +	int splitter = 0;
 | 
	
		
			
				|  |  | +	bool checker;
 | 
	
		
			
				|  |  | +	while (input[splitter] != '/' && input[splitter]) {
 | 
	
		
			
				|  |  | +		splitter++;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	checker = input[splitter] != '/';
 | 
	
		
			
				|  |  | +	int dec = 1;
 | 
	
		
			
				|  |  | +	for (int i = splitter - 1; i >= 0; i--) {
 | 
	
		
			
				|  |  | +		if (input[i] != '-')
 | 
	
		
			
				|  |  | +			r.Chisl += (input[i] - 48) * dec;
 | 
	
		
			
				|  |  | +		else {
 | 
	
		
			
				|  |  | +			r.sign = !r.sign;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		dec *= 10;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	if (!checker) {
 | 
	
		
			
				|  |  | +		while (input[splitter])
 | 
	
		
			
				|  |  | +			splitter++;
 | 
	
		
			
				|  |  | +		dec = 1;
 | 
	
		
			
				|  |  | +		r.Znam = 0;
 | 
	
		
			
				|  |  | +		for (int i = splitter - 1; input[i] != '/'; i--) {
 | 
	
		
			
				|  |  | +			if (input[i] != 45)
 | 
	
		
			
				|  |  | +				r.Znam += (input[i] - 48) * dec;
 | 
	
		
			
				|  |  | +			else{
 | 
	
		
			
				|  |  | +				r.sign = !r.sign;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +			dec *= 10;
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	r.socr();
 | 
	
		
			
				|  |  | +	return is;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +ostream&
 | 
	
		
			
				|  |  | +operator <<(ostream& os, Rational& r) {
 | 
	
		
			
				|  |  | +	if (r.Chisl % r.Znam == 0)
 | 
	
		
			
				|  |  | +		return os << "\n  " << r.Chisl / r.Znam << "\n";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	double len_max = log10((double)max(r.Chisl, r.Znam));
 | 
	
		
			
				|  |  | +	for (int i = 0; i < (int)(len_max - log10(r.Chisl)) / 2; i++) {
 | 
	
		
			
				|  |  | +		os << " ";
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	os << "  " << r.Chisl << endl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if (r.sign)
 | 
	
		
			
				|  |  | +		os << '-' << ' ';
 | 
	
		
			
				|  |  | +	else
 | 
	
		
			
				|  |  | +		os << "  ";
 | 
	
		
			
				|  |  | +	for (int i = 0; i < len_max; i++) {
 | 
	
		
			
				|  |  | +		os << "=";
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	os << endl << "  ";
 | 
	
		
			
				|  |  | +	for (int i = 0; i < (int)(len_max - log10(r.Znam)) / 2; i++) {
 | 
	
		
			
				|  |  | +		os << " ";
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	os << r.Znam;
 | 
	
		
			
				|  |  | +	return os;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |