Click here to Skip to main content
15,907,493 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRational Number constructor Pin
aab199025-Mar-09 13:20
aab199025-Mar-09 13:20 
AnswerRe: Rational Number constructor Pin
Cedric Moonen5-Mar-09 20:22
Cedric Moonen5-Mar-09 20:22 
QuestionRe: Rational Number constructor Pin
CPallini5-Mar-09 21:09
mveCPallini5-Mar-09 21:09 
QuestionRe: Rational Number constructor Pin
David Crow6-Mar-09 3:40
David Crow6-Mar-09 3:40 
QuestionIterating string to add escape sequence. Pin
mmayur5-Mar-09 11:27
mmayur5-Mar-09 11:27 
AnswerRe: Iterating string to add escape sequence. Pin
«_Superman_»5-Mar-09 17:22
professional«_Superman_»5-Mar-09 17:22 
QuestionRe: Iterating string to add escape sequence. Pin
David Crow6-Mar-09 3:43
David Crow6-Mar-09 3:43 
QuestionFirst C++ class Pin
aab199025-Mar-09 11:18
aab199025-Mar-09 11:18 
Can someone please take a look at this class and tell me what I'm doing so terribly wrong. The compiler gives me an error I search around fix it and then another one pops up.
I will post my header file and my code...And if someone has a thought about how to overload the input operator that would help because i am lost on that one

#ifndef RATIONAL_
#define RATIONAL_

using namespace std;

#include <iostream>

class Rational
{
  public:

    // Construct rational from numerator and denominator
    //
    Rational( int = 0, int = 1 );

    // Construct rational by copying existing rational
    //
    Rational( const Rational& );

    // Assign into rational by copying existing rational
    //
    Rational& operator=( const Rational& );

         //Find Greatest Common Divisor
         int GCD();

         //Simplify the rational number to its smallest form
         void Simplify();

    // Return true if rational is valid (non-zero denominator)
    //
    bool IsValid() const;

    // Return value of numerator
    //
    int Numerator() const;

    // Return value of denominator
    //
    int Denominator() const;

    // Input/Output operations
    //
    friend istream& operator>>( istream&, Rational& );
    friend ostream& operator<<( ostream&, const Rational& );

  private:

    // Insert your declarations here
         int Numerator_;
         int Denominator_;
};

// Comparative operations
//
bool operator==( const Rational&, const Rational& );
bool operator!=( const Rational&, const Rational& );
bool operator< ( const Rational&, const Rational& );
bool operator<=( const Rational&, const Rational& );
bool operator> ( const Rational&, const Rational& );
bool operator>=( const Rational&, const Rational& );

// Arithmetic operations
//
Rational operator+( const Rational&, const Rational& );
Rational operator-( const Rational&, const Rational& );
Rational operator*( const Rational&, const Rational& );
Rational operator/( const Rational&, const Rational& );

#endif
</iostream>


using namespace std;

#include <iostream>

#include "proj06.rational.h"


/*-----------------------------------------------------------------------------
   Name:  constructor for class "Rational"

   Purpose:  Initialize the contents of a rational number
   Receive:  The numerator and denominator or rational number
-----------------------------------------------------------------------------*/

Rational::Rational( int Num, int Den )
{
   Numerator_ = Num;

  if (Den <= 0)
  {
    Denominator_ = 1;
  }
  else
  {
    Denominator_ = Den;
  }
}

/*-----------------------------------------------------------------------------
   Name:  copy constructor for class "Rational"

   Purpose:  Initialize the value and scale inside a Rational object
   Recieve:   The rational to be copied
-----------------------------------------------------------------------------*/

Rational::Rational( const Rational& C )
{
  Numerator_ = C.Numerator_;
  Denominator_ = C.Denominator_;
}

/*-----------------------------------------------------------------------------
   Name:  assignment operator for class "Rational"

   Purpose:  Copy the contents of one rational to another
        Recieve:         The rational which is to be copied
   Return:   The rational number
-----------------------------------------------------------------------------*/

Rational& Rational::operator=(const Rational& C )
{
        Numerator_ = C.Numerator_;
        Denominator_ = C.Denominator_;

        return *this;
}
 
/*-----------------------------------------------------------------------------
   Name: GCD 
 
   Purpose:  Find the greatest common divisor of a rational number
   Return:   GCD
-----------------------------------------------------------------------------*/
 
int Rational::GCD()
{
        int A = Numerator_;
        int B = Denominator_;

        int Temp;

        while(B)
        {
                Temp = B;
                B = A%B;
                A = Temp;
        }

        return A;
}

/*-----------------------------------------------------------------------------
   Name:        Simplify
 
   Purpose:  Simplify a rational number
-----------------------------------------------------------------------------*/
 
void Rational::Simplify()
{
        int GCDNumber = GCD();

        if(GCDNumber != 0)
        {
                Numerator_ = Numerator_/GCDNumber;
                Denominator_ = Denominator_/GCDNumber;
        }
}

/*-----------------------------------------------------------------------------
   Name:  IsValid
 
   Purpose:  Test whether or not the rational is valid
   Return:   The Boolean value for the result of the test
-----------------------------------------------------------------------------*/
 
bool Rational::IsValid() const
{
  return ( Denominator_ != 0 ); 
}

/*-----------------------------------------------------------------------------
   Name:  Numerator
 
   Purpose:  extract numerator from rational number
        Return:  numerator
-----------------------------------------------------------------------------*/

int Rational::Numerator() const
{
        return Numerator_;
}

/*-----------------------------------------------------------------------------
   Name:  Denominator
 
   Purpose:  extract denominator from rational number
        Return:          denominator
-----------------------------------------------------------------------------*/

int Rational::Denominator() const
{
        return Denominator_;
}

/*-----------------------------------------------------------------------------
   Name:  operator==
 
   Purpose:  compare 2 rationals for equality
        Recieve:         the 2 rationals
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator==( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator() == B.Numerator() and A.Denominator()== B.Denominator());
}
 

/*-----------------------------------------------------------------------------
   Name:  operator!=
 
   Purpose:  compare 2 rationals for inequality
        Recieve:         the 2 rationals
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator!=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator() != B.Numerator() and A.Denominator()!= B.Denominator());
}

/*-----------------------------------------------------------------------------
   Name:  operator<<br mode="hold" /> 
   Purpose:  tell whether A is less than B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator<( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())<(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator<=
 
   Purpose:  tell whether A is less than or equal to B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator<=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())<=(A.Denominator()*B.Numerator());
}


/*-----------------------------------------------------------------------------
   Name:  operator>
 
   Purpose:  tell whether A is greater than B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator>( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())>(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator>=
 
   Purpose:  tell whether A is greater than or equal to B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator>=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())>=(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator+
 
   Purpose:  Add two rational numbers
        Recieve:         The two rationals to be added
        Return:  The rational result of adding the two rationals
-----------------------------------------------------------------------------*/

Rational operator+( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Denominator()+A.Denominator()*B.Numerator();
        Den = A.Denominator()*B.Denominator();

        Rational C(Num, Den);
        return C.Simplify();
}


/*-----------------------------------------------------------------------------
   Name:  operator-
 
   Purpose:  Subtract two rational numbers
        Recieve:         The two rationals to be subtracted
        Return:  The rational result of subtracting the two rationals
-----------------------------------------------------------------------------*/

Rational operator-( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Denominator()-A.Denominator()*B.Numerator();
        Den = A.Denominator()*B.Denominator();

        Rational C(Num, Den);
        return C.Simplify();
}

/*-----------------------------------------------------------------------------
   Name:  operator*
 
   Purpose:  Multiply two rational numbers
        Recieve:         The two rationals to be multiplied
        Return:  The rational result of multiplying the two rationals
-----------------------------------------------------------------------------*/

Rational operator*( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Numerator();
        Den = A.Denominator()*B.Denominator();

        Rational C(Num, Den);
        return C.Simplify();
}

/*-----------------------------------------------------------------------------
   Name:  operator/
 
   Purpose:  Divide two rational numbers
        Recieve:         The two rationals to be divided
        Return:  The rational result of dividing the two rationals
-----------------------------------------------------------------------------*/

Rational operator/( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Denominator();
        Den = A.Denominator()*B.Numerator();

        Rational C(Num, Den);
        return C.Simplify();
}


/*-----------------------------------------------------------------------------
   Name:  operator<<

   Purpose:  Put a rational into an output stream
   Receive:  The reference to an output stream
             The refernce to a rational which is to be written
   Return:   The output stream (for chaining)
        Ouput:   A rational number "numerator/denominator"
-----------------------------------------------------------------------------*/

ostream& operator<<( ostream& Out, const Rational& Item ) 
{
        Out << Item.Numerator() << "/" << Item.Denominator();

        return Out;
}


/*-----------------------------------------------------------------------------
   Name:  operator>>

   Purpose:  extract a rational from an input stream
   Receive:  The reference to aninput stream
             The reference to a rational which is to be written
   Return:   The input stream (for chaining)
        Input:   a rational number
-----------------------------------------------------------------------------*/</iostream>

AnswerRe: First C++ class Pin
Yusuf5-Mar-09 11:29
Yusuf5-Mar-09 11:29 
GeneralRe: First C++ class Pin
aab199025-Mar-09 11:36
aab199025-Mar-09 11:36 
GeneralRe: First C++ class Pin
Yusuf5-Mar-09 11:55
Yusuf5-Mar-09 11:55 
QuestionRe: First C++ class Pin
Maximilien5-Mar-09 11:39
Maximilien5-Mar-09 11:39 
AnswerRe: First C++ class Pin
aab199025-Mar-09 12:01
aab199025-Mar-09 12:01 
QuestionTo Host a Dot net control(C# or WPF) in MFC 6(VC6) Dialog Pin
Jaskaran815-Mar-09 9:53
Jaskaran815-Mar-09 9:53 
AnswerRe: To Host a Dot net control(C# or WPF) in MFC 6(VC6) Dialog Pin
rwe181211-May-09 0:25
rwe181211-May-09 0:25 
QuestionEmbedded C++ SDK problems Pin
gauravxxx5-Mar-09 7:36
gauravxxx5-Mar-09 7:36 
QuestionApplication upgrade Pin
jpyp5-Mar-09 6:41
jpyp5-Mar-09 6:41 
AnswerRe: Application upgrade Pin
Ric Ashton5-Mar-09 6:50
Ric Ashton5-Mar-09 6:50 
AnswerRe: Application upgrade Pin
bulg5-Mar-09 6:58
bulg5-Mar-09 6:58 
AnswerRe: Application upgrade Pin
Yusuf5-Mar-09 9:16
Yusuf5-Mar-09 9:16 
GeneralRe: Application upgrade Pin
jpyp5-Mar-09 11:08
jpyp5-Mar-09 11:08 
GeneralRe: Application upgrade Pin
Yusuf5-Mar-09 11:23
Yusuf5-Mar-09 11:23 
GeneralRe: Application upgrade Pin
malaugh5-Mar-09 17:07
malaugh5-Mar-09 17:07 
AnswerRe: Application upgrade Pin
David Crow6-Mar-09 3:49
David Crow6-Mar-09 3:49 
GeneralRe: Application upgrade Pin
jpyp6-Mar-09 5:42
jpyp6-Mar-09 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.