Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a class Fraction that defines adding, subtracting, multiplying, and
dividing fractions by overloading standard operators for these operations. Write
a function member for reducing factors and overload I/O operators to input and
output fractions

C++
#include <iostream>

class Fraction
{
    int m_n1, m_n2, m_d1, m_d2, m_n, m_d;
public:
    Fraction():m_n1(0), m_n2(0), m_d1(0), m_d2(0)
    {

    }
    Fraction(int n1, int n2, int d1, int d2):m_n1(n1), m_n2(n2), m_d1(d1), m_d2(d2)
    {
        fractionsAddition(n1, n2, d1, d2);
        reduce(m_n, m_d);
    }
    int gcd(int n, int d);
    int lcm(int d1, int d2);
    void fractionsAddition(int n1, int n2, int d1, int d2);
    void reduce(int n, int d);

    friend std::ostream &operator<<(std::ostream &out, const Fraction &f);
    friend std::istream &operator>>(std::istream &in, const Fraction &f);

};


int Fraction::gcd(int n, int d)
{
    if(d==0)
    {
        return n;
    }
    return gcd(d, n%d);
}

int Fraction::lcm(int d1, int d2)
{
    int max=(d1>d2) ? d1: d2;
    do
    {
        if((max%d1==0) && (max%d2==0))
        {
            return max;
        }

        else
        {
            ++max;
        }
    }
    while(max<d1*d2);

    return d1*d2;
}

void Fraction::reduce(int n, int d)
{
    int g=gcd(m_n, m_d);
    m_n=m_n/g;
    g=g/m_d;
}

void Fraction::fractionsAddition(int n1, int n2, int d1, int d2)
{
    //return (n1*lcm(d1, d2)+n2*lcm(d1, d2))/lcm(d1, d2);
    std::cout<<(n1*lcm(d1, d2)+n2*lcm(d1, d2))<<"/"<<lcm(d1,d2);

}

std::istream &operator>>(std::istream &in, const Fraction &f)
{
    in>>f.m_n1>>f.m_n2>>f.m_d1>>f.m_d2;
    return in;
}

std::ostream &operator<<(std::ostream &out, const Fraction &f)
{
    out<<f.m_n1<<"/"<<f.m_d1<<"+"<<f.m_n2<<"/"<<f.m_d2;
    return out;
}

int main()
{
    Fraction f(1,4,5,6);
    std::cout<<f;

}


What I have tried:

I have searched on Google, but I couldn't find a good response
Posted
Updated 9-Mar-18 11:40am
v2
Comments
Richard MacCutchan 10-Mar-18 4:25am    
That code does not seem to have anything to do with the description of your assignment. A fraction comprises two numbers, a numerator and a denominator. You are tasked with writing methods for +, -, *, /. What you have written bears no relation to that.

This code is highly inefficient:
C++
int Fraction::lcm(int d1, int d2)
{
    int max=(d1>d2) ? d1: d2;
    do
    {
        if((max%d1==0) && (max%d2==0))
        {
            return max;
        }

        else
        {
            ++max;
        }
    }
    while(max<d1*d2);

    return d1*d2;
}

would be smarter to use the formula
lcm(d1 , d2) = d1 * d2 / gcd(d1 , d2)

[Update]
Using the tail recursion for the GCD is not efficient either. A loop is a preferred solution because it need less resources and thus is faster.
Note that most compilers transform the tail recursion to a loop.
Tail call - Wikipedia[^]
 
Share this answer
 
v2
Comments
Member 13277493 9-Mar-18 14:40pm    
thanks for a good advice!
Member 13277493 9-Mar-18 14:41pm    
the code is not running, it has some errors with overloaded operators
Patrice T 9-Mar-18 14:45pm    
Sorry, can't help you on operators.
You are passing a constant Fraction reference to the istream operator, which makes all the integer values constant. There is no istream operator that takes a 'const int' on the right-hand side.

Remove the const from the declaration and definition of the istream operator as shown below.

std::istream &operator>>(std::istream &in, Fraction &f)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900