Thread: Cancelling down fractions

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Cancelling down fractions

    Hi all!

    I've written a class and test program which seems to cancel down fractions to their lowest equivalent form!

    Firstly, I'd like to know if anybody can spot any flaws with the method.

    Secondly, If you know a more efficent/elegant way of doing it, then please inform me!

    Please find my code below!

    Code:
    // Rational.h
    // Member functions defined in Rational.cpp
    #ifndef RATIONAL_H
    #define RATIONAL_H
    
    
    class Rational
    {
    public:
        Rational( int = 1, int = 2 ); // default constructor
    
        // Set functions
        void setNumerator( int );
        void setDenominator( int );
    
        // Get functions
        int getNumerator();
        int getDenominator();
    
        // Utility functions
        void simplify();
        void print();
    
    private:
        int numerator;
        int denominator;
    }; // end Rational class
    
    #endif // RATIONAL_H
    Code:
    // main.cpp
    #include <iostream>
    #include "Rational.h"
    using namespace std;
    
    int main()
    {
        Rational fraction1( 75, 100 );
    
        fraction1.print();
    } // end main
    Code:
    // Rational.cpp
    #include "Rational.h"
    #include <iostream>
    using namespace std;
    
    // *** Constructor definition ***
    Rational::Rational( int Num, int Denom )
    {
        setNumerator( Num );
        setDenominator( Denom );
    
        if ( getNumerator() != 1 )
        {
            simplify();
        } // end if
    } // end Rational constructor
    
    
    // *** Set function definition ***
    void Rational::setNumerator( int Num )
    {
        if ( Num < 1 )
        {
            Num = 0;
        } // end if
        numerator = Num;
    } // end function setNumerator
    
    void Rational::setDenominator( int Denom )
    {
        while ( Denom < ( getNumerator() ) )
        {
            cout << "NOT A VALID DENOMINATOR !!!";
        } // end if
        denominator = Denom;
    } // end function setDenominator
    
    
    // *** Get function definition ***
    int Rational::getNumerator()
    {
        return numerator;
    } // end function getNumerator
    
    int Rational::getDenominator()
    {
        return denominator;
    } // end function getDenominator
    
    
    // *** Utility function definition ***
    void Rational::print()
    {
        cout << "(" << getNumerator() << ", " << getDenominator() << ")" << endl;
    } // end function print
    
    void Rational::simplify()
    {
        int counter = 2;
        int a = getNumerator();
        int b = getDenominator();
        bool bothDivisible = true;
    
        while ( bothDivisible == true )
        {
            for ( counter; counter <= a; counter++ )
            {
                if ( a % counter == 0 && b % counter == 0 )
                {
                    setNumerator( a / counter );
                    setDenominator( b / counter );
                    simplify();
                } // end if
            } // end for
            bothDivisible = false;
        } // end while
    } // end function simplify

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess it works then, but it seems rather messy and quite inefficient.

    Better way to simplify fractions: divide numerator and denominator by their greatest common divisor which can be found with the Euclidean algorithm.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fractions are just = to 0
    By Macca in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 05:41 AM
  2. Fractions
    By madmax2006 in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 11:58 PM
  3. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  4. Cancelling down of fractions
    By Finchie_88 in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2005, 02:54 AM
  5. fractions
    By joeshmoe1337 in forum C Programming
    Replies: 21
    Last Post: 09-11-2004, 01:34 AM