Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
4
Indexable
class Fraction {
private:
    int first, second;
    int nod(int a, int b) {
        while (a && b) {
            if (a > b) {
                a = a % b;
            }
            else {
                b = b % a;
            }
        }
        return(max(a, b));
    }

public:
    Fraction() :first(1), second(1) {
    }
    Fraction(int a, int b) :first(a), second(b) {
        int del;
        do {
            del = nod(first, second);
            first /= del;
            second /= del;
        } while (del != 1);
    }

    void set_data(int a, int b) {
        first = a;
        second = b;
    }

    void get_data_simple() {
        cout << this->first << " / " << this->second << endl;
    }

    void get_data_dec() {
        cout << first / (float)second << endl;
    }

    Fraction operator+(Fraction b) {
        this->first = this->first * b.second + b.first * this->second;
        this->second = this->second * b.second;
        return(*this);
    }
    Fraction operator-(Fraction b) {
        this->first = this->first * b.second - b.first * this->second;
        this->second = this->second * b.second;
        return(*this);
    }
    Fraction operator*(Fraction b) {
        this->first = b.second * b.first;
        this->second = this->second * this->first;
        return(*this);
    }
    Fraction operator/(Fraction b) {
        this->first = this->first / this->second;
        this->second = b.first * b.second;
        return(*this);
    }