Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Fractions Class

Tags:

c++

I have to create a class called Fractions with 2 private fields Numerator, Denominator. And a public constructor that sets Numerator and Denominator to 1 by default. I have included 4 members functions in my my Fractions class: Sum, Difference, Product, Division.

Then I am not sure what to do next. Why does book show fraction equivalences? What do I have to do with that? I guess a very important question would be what parameters should my member functions take?

Also what would be a good way to prohibit denominator of 0? Throw exception or force it to be equal to 1?

Problem #5

Here is the complete source code for the question #5 and #6 (not shown) after struggling with the problem for days. Questions #6 just asks to implement the greatest common divisor function to return fraction in a simplified form. So here it is...

If you think there is a way to optimize this code I'd be happy to hear your responses!

#include <iostream>
using namespace std;

class Fraction
{
private:
    int numerator, denominator; 
public:
    Fraction()
    {
        numerator = 1;
        denominator = 1;
    }
    Fraction(int n, int d)
    {
        numerator = n;
        if (d==0) 
        {
            cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
            exit(0); // will terminate the program if division by 0 is attempted
        }
        else
            denominator = d;
    }
    /*In the following functions I am dividing both numerator and denominator by the gcd function.
    GCD function accepts both numerator and denominator values. If we had 2 fractions, 1/2 and 1/4
    and we passed it into the Sum, the result would be n=6 and d=8. These are the values that GCD
    function will accept, find greatest common divisor and return the integer value of 2. In my case 
    am diving both numerator and denominator on the same line by the greatest common divisor. Although 
    it probably would be more efficient to create a local int variable and store GCD value in it, but
    for such small program it shouldn't make any difference.*/
    Fraction Sum(Fraction otherFraction)
    {
        int n = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
        int d = denominator*otherFraction.denominator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }
    Fraction Difference(Fraction otherFraction)
    {
        int n = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
        int d = denominator*otherFraction.denominator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }
    Fraction Product(Fraction otherFraction)
    {
        int n = numerator*otherFraction.numerator;
        int d = denominator*otherFraction.denominator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }
    Fraction Division(Fraction otherFraction)
    {
        int n = numerator*otherFraction.denominator;
        int d = denominator*otherFraction.numerator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }
    // I got the GCD algorithm from the following source:
    // Source C#: http://www.ww.functionx.com/csharp2/examples/gcd.htm
    int gcd(int n, int d)
    {
        int remainder;
        while (d != 0)
        {
            remainder = n % d;
            n = d;
            d = remainder;
        }
        return n;
    }
    void show() // Display method
    {
        if (denominator == 1) // e.g. fraction 2/1 will display simply as 2
            cout << numerator << endl;
        else
            cout << numerator << "/" << denominator << endl;
    }
};

int main()
{
    Fraction a(1,2);
    Fraction b(1,4);
    Fraction c;

    c = a.Sum(b); // Result: 3/4
    c.show();

    c = a.Difference(b); // Result: 1/4
    c.show();

    c = a.Product(b); // Result: 1/8
    c.show();

    c = a.Division(b); // Result: 2
    c.show();

    return 0;
}
like image 959
Sahat Yalkabov Avatar asked Apr 17 '11 00:04

Sahat Yalkabov


People also ask

What is a Class 4 fraction?

Fractions are the numerical values that are a part of the whole. A whole can be an object or a group of objects. If a number or a thing is divided into equal parts, then each part will be a fraction of the whole. A fraction is denoted as a/b, where a is the numerator and b is the denominator.

What is a fraction class 3?

The part of a whole is known as Fraction. For example if a watermelon is cut into 4 parts then each part will be a fraction of the whole watermelon. Understanding Fractions. A fraction consists of two parts:- Numerator: The part which is written above the horizontal line is called as numerator.

What is a fraction for Class 2?

What is a Fraction? Fractions represent the parts of a whole or collection of objects. A fraction has two parts. The number on the top of the line is called the numerator.


1 Answers

You may want to start with just the first part, create the class with two constructors, one if they give the two numbers, and one that defaults to 1.

Then, write the four functions, the assignment is nice enough to give you the equations.

If a denominator is ever equal to zero then I would throw an exception, as that is something the user may be able to fix. If you can show what led to it being zero, for example, if they divide (1/2)/(0/4) then your denominator is zero, which should be an error condition.

One issue you will want to look at is if this will be immutable, so, if I have (1/2) and (2/3) and I do an operation, it should return a new number, not modify either of the two that I passed in. In that case your check is always in the constructor for a zero, but you should explain which operation led to this error.

like image 55
James Black Avatar answered Sep 22 '22 04:09

James Black