Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
3.6 kB
2
Indexable
Never
#include <iostream>
using namespace std;

class Fraction {
private:
    int numerator;
    int denominator;

public:
    // Constructors
    Fraction(int num = 0, int den = 1) : numerator(num), denominator(den)
{
        if (denominator == 0)
            {
            cout << "Error: Denominator cannot be zero. Setting denominator to 1." << endl;//checking if the denominator zero or not?
            denominator = 1;//if denominator is zero make it 1 and return the value 1
        }
        simplify();
    }

    // Getter functions
    int getNumerator() const {
        return numerator;
    }

    int getDenominator() const {
        return denominator;
    }

    // Arithmetic operators
    //addition
    Fraction operator+(const Fraction& other) const {
        int num = numerator * other.denominator + other.numerator * denominator;
        int den = denominator * other.denominator;
        return Fraction(num, den);
    }
//subtraction
    Fraction operator-(const Fraction& other) const {
        int num = numerator * other.denominator - other.numerator * denominator;
        int den = denominator * other.denominator;
        return Fraction(num, den);
    }
//multiply
    Fraction operator*(const Fraction& other) const {
        int num = numerator * other.numerator;
        int den = denominator * other.denominator;
        return Fraction(num, den);
    }
//divide
    Fraction operator/(const Fraction& other) const {
        int num = numerator * other.denominator;
        int den = denominator * other.numerator;
        return Fraction(num, den);
    }

    // Comparison operators
    bool operator==(const Fraction& other) const {
        return (numerator == other.numerator && denominator == other.denominator);
    }

    bool operator!=(const Fraction& other) const {
        return !(*this == other);
    }

    // Simplify the fraction using gcd(greatest common factor)
    void simplify() {
        int gcd_val = gcd(numerator, denominator);
        numerator /= gcd_val;
        denominator /= gcd_val;
        // Making sure denominator is positive
        if (denominator < 0) {
            numerator *= -1;
            denominator *= -1;
        }
    }

    // Helper function to calculate gcd
    int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
};

// Overload output stream operator to print Fraction object
ostream& operator<<(ostream& o, const Fraction& frac) {
    o << frac.getNumerator() << "/" << frac.getDenominator();
    return o;
}

// Function to get user input for a Fraction
Fraction get() {
    int num, den;

    cout << "Enter numerator: ";
    cin >> num;

    cout << "Enter denominator: ";
    cin >> den;

    return Fraction(num, den);
}

int main() {
    system("color f0");
    cout << "Enter fraction A:" << endl;
    Fraction a = get();

    cout << "Enter fraction B:" << endl;
    Fraction b = get();

    Fraction sum = a + b;
    Fraction difference = a - b;
    Fraction product = a * b;
    Fraction quotient = a / b;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "a + b = " << sum << endl;
    cout << "a - b = " << difference << endl;
    cout << "a * b = " << product << endl;
    cout << "a / b = " << quotient << endl;

    if (a == b) {
        cout << "a is equal to b" << endl;
    } else {
        cout << "a is not equal to b" << endl;
    }

    return 0;
}
Leave a Comment