Untitled
unknown
c_cpp
a year ago
6.6 kB
4
Indexable
#include <iostream> using namespace std; // - 55 105 int GCD( int a, int b ) { while( b != 0 ) { int remain = a % b; // -55 % 105 = -55 a = b; // a = 105 b = remain; // b = -55 } return a; } // класс обыкновенная дробь Fraction. // операции сложения, вычитания, деления, умножения и сокращения. class Fraction { // поля класса int num, denom; // метод коррекции дроби void Correct() { if ( denom < 0 ) num = -num, denom = -denom; else if ( denom == 0 || num == 0 ) num = 0, denom = 1; Reduce(); } public: // конструктор с двумя параметрами Fraction(const int n = 0, const int d = 1) : num( n ), denom( d ) { Correct(); } // метод сокращения дроби void Reduce() { int gcd = GCD( num, denom ); // 10 / 50 = 1 / 5 num /= gcd, denom /= gcd; } void SetNum( const int num ) { this->num = num; // this - указатель на текущий объект класса } void SetDenom( const int denom) { // -2 / 5; SetDenom( -10 ); -2 / -10 = 1 / 5 this->denom = denom; Correct(); } int GetNum() const { return num; } int GetDenom() const { return denom; } //перегрузка сложения f + other // параметр в скобках - это дробь справа от знака + // слева от знака + - это наш объект // Fraction f4 = f2 + f; Fraction operator+ ( const Fraction& other ); Fraction operator- ( const Fraction& other ); Fraction operator* ( const Fraction& other ); Fraction operator/ ( const Fraction& other ); Fraction operator+= ( const Fraction& other ); Fraction operator-= ( const Fraction& other ); Fraction operator*= ( const Fraction& other ); Fraction operator/= ( const Fraction& other ); bool operator== ( const Fraction& other ); bool operator!= ( const Fraction& other ); Fraction operator > ( const Fraction& other ); Fraction operator < ( const Fraction& other ); Fraction operator <= ( const Fraction& other ); Fraction operator >= ( const Fraction& other ); // > // < // <= // >= }; Fraction Fraction::operator+ ( const Fraction& other ) { /* f.num - other.num f.denom - other.denom num - f2.num denom - f2.denom*/ Fraction res; res.num = other.denom * num + denom * other.num; res.denom = other.denom * denom; res.Reduce(); return res; // f2 + f // 1/2 + 1/3 = 5/6 // 3 / 10 7 / 45 } Fraction Fraction::operator- ( const Fraction& other ) { Fraction res; res.num = num * other.denom - other.num * denom; res.denom = other.denom * denom; res.Reduce(); return res; } Fraction Fraction:: operator* ( const Fraction& other ) { Fraction res; res.num = num * other.num; res.denom = denom * other.denom; res.Reduce(); return res; } Fraction Fraction::operator / ( const Fraction& other ) { Fraction res; res.num = num * other.denom; res.denom = denom * other.num; res.Reduce(); return res; } Fraction Fraction::operator+= ( const Fraction& other ) { num = num * other.denom + denom * other.num; denom *= other.denom; Reduce(); return *this; } Fraction Fraction::operator-= ( const Fraction& other ){ num = num * other.denom - denom * other.num; denom *= other.denom; Reduce(); return *this; } Fraction Fraction:: operator*= ( const Fraction& other ) { num = num * other.num; denom = denom * other.denom; Reduce(); return *this; } Fraction Fraction::operator /= ( const Fraction& other ) { num = num * other.denom; denom = denom * other.num; Reduce(); return *this; } bool Fraction::operator== (const Fraction& other){ return num * other.denom == other.num * denom; } // 1/2 != 3/4 = true // 1/2 != 1/2 = false bool Fraction::operator!= (const Fraction& other){ return !(*this == other) ; } Fraction Fraction:: operator > ( const Fraction& other ){ if (( denom = other.denom && num > other.num ) || ( num = other.num && denom < other.denom)) return true; return false; } Fraction Fraction:: operator < ( const Fraction& other ){ if (( denom = other.denom && num < other.num ) || ( num = other.num && denom > other.denom)) return true; return false; } Fraction Fraction:: operator <= ( const Fraction& other ){ return num * other.denom <= other.num * denom; } Fraction Fraction:: operator >= ( const Fraction& other ){ return num * other.denom >= other.num * denom; } /*Fraction Fraction:: operator <= ( const Fraction& other ){ if (( denom > other.denom ) || ( denom = other.denom && num < other.num )) return true; return false; }*/ /* double Avg( ... ){ ... return sum / count; } int CountOdd( ... ){ int counter; .... return counter; } Point MinPoint( Point* a, ... ){ Point p; .... if Dist( a[ i ] < Dist( p ) ) p = a[ i ]; return p; }*/ // перегрузка оператора вывода ostream& operator<<(ostream& os, const Fraction& f) { os << f.GetNum() << "/" << f.GetDenom(); return os; } // перегрузка оператора ввода istream& operator>>(istream& is, Fraction& f) { int n, d; is >> n >> d; f.SetNum( n ); f.SetDenom( d ); return is; } int main() { int a = 0, b = 1; cout << GCD( a, b ) << endl; Fraction f( -50, -100 ); Fraction f2( 3, 4 ); // 0 / 1 // cout << 105 % (-55) << endl; // cout << f.GetNum() << "/" << f.GetDenom() << endl; // 1/2 cout << f << endl; Fraction f3; cout << "Input f3: "; cin >> f3; cout << f3 << endl; Fraction f4 = f2 + f; // 5 / 4 cout << f4 << endl; Fraction f5 = f2 - f; cout << f5 << endl; Fraction f6 = f2 * f; cout << f6 << endl; Fraction f7 = f2 / f; cout << f7 << endl; f2 /= f; cout << f2 << endl; return 0; }
Editor is loading...