Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

---- Manipulation of rational (fraction) numbers ------

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hi,

I'm a C++ beginner and I really need some help to write a C++ program that implements an ADT class named Rational for manipulating rational (fraction) numbers.
The operations provided on this type must include: sum (+), difference (-), product (*), division (/), display (<<), and input (>>) of values for Rational objects.
The result produced by each arithmetic operation must automatically be reduced to lowest terms (change 72/28 to 18/7).
I have to allow for positive and negative values and have to use the class in a program that gives the user the ability to enter data and perform each of the calculations. The operator overloading must support expressions like:
R3 =R1+ R2; //each variable is of type Rational
cout << &quot;results:&quot; << R4 << &quot; & &quot; << R5 << endl;

Any examples or suggestions are really appreciated.
Mark S.

 
Here you go. It's not perfect - I've not worried about zero values for instance, and my use of const is bitty, but it should all work.

#include &quot;stdafx.h&quot;
#include &quot;iostream.h&quot;

class Rational
{
public:
Rational();
Rational( int num, int den );
Rational operator+(const Rational &r );
Rational operator-(const Rational &r );
Rational operator*(const Rational &r );
Rational operator/(const Rational &r );
void Set( int num, int den );
void Tidy();
int Num() const { return m_num; }
int Den() const { return m_den; }
char *Text();
private:
int AbsMin();
int m_num, m_den;
char m_str[ 20 ];
};

Rational::Rational()
{
m_num = 0;
m_den = 0;
}

Rational::Rational( int num, int den )
{
m_num = num;
m_den = den;
Tidy();
}

void Rational::Set( int num, int den )
{
m_num = num;
m_den = den;
Tidy();
}

Rational Rational::eek:perator+(const Rational &r )
{
int n1, n2, d1, d2;
Rational retval;
n1 = Num();
d1 = Den();
n2 = r.Num();
d2 = r.Den();

n1 *= d2;
n2 *= d1;

retval.Set( n1 + n2, d1 * d2 );
return retval;
}

Rational Rational::eek:perator-(const Rational &r )
{
int n1, n2, d1, d2;
Rational retval;
n1 = Num();
d1 = Den();
n2 = r.Num();
d2 = r.Den();

n1 *= d2;
n2 *= d1;

retval.Set( n1 - n2, d1 * d2 );
return retval;
}

Rational Rational::eek:perator*(const Rational &r )
{
int n1, n2, d1, d2;
Rational retval;
n1 = Num();
d1 = Den();
n2 = r.Num();
d2 = r.Den();

retval.Set( n1 * n2, d1 * d2 );
return retval;
}

Rational Rational::eek:perator/(const Rational &r )
{
int n1, n2, d1, d2;
Rational retval;
n1 = Num();
d1 = Den();
n2 = r.Num();
d2 = r.Den();

retval.Set( n1 * d2, d1 * n2 );
return retval;
}

char* Rational::Text()
{
sprintf( m_str, &quot;%d/%d&quot;, m_num, m_den );
return m_str;
}

int Rational::AbsMin()
{
int n, d;

n = Num();
d = Den();
if( n < 0 ) n = 0 - n;
return n < d ? n : d;
}

void Rational::Tidy()
{
int i;

if( m_den < 0 )
{
m_den = 0 - m_den;
m_num = 0 - m_num;
}
for( i = 2; i <= AbsMin(); i++ )
{
while( ( m_num % i ) == 0 && ( m_den % i ) == 0 )
{
m_num /= i;
m_den /= i;
}
}
}

int main(int argc, char* argv[])
{
Rational R1, R2, R3;

R1.Set( 3, 4 );
R2.Set( 5, 12 );
R3 = R1 + R2;
cout << R3.Text() << &quot;=&quot; << R1.Text() << &quot;+&quot; << R2.Text() << &quot; : &quot;;
R3 = R1 - R2;
cout << R3.Text() << &quot;=&quot; << R1.Text() << &quot;-&quot; << R2.Text() << &quot; : &quot;;
R3 = R1 * R2;
cout << R3.Text() << &quot;=&quot; << R1.Text() << &quot;*&quot; << R2.Text() << &quot; : &quot;;
R3 = R1 / R2;
cout << R3.Text() << &quot;=&quot; << R1.Text() << &quot;/&quot; << R2.Text() << &quot;\n&quot;;

return 0;
}

Jonathan Clarke
jonathan.clarke@whitespace.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top