Category : Files from Magazines
Archive   : CUJ9201.ZIP
Filename : 1001098D

 
Output of file : 1001098D contained in archive : CUJ9201.ZIP

Listing 11

//
// rational.cpp
//
#include
#include "rational.h"

rational &rational::operator+=(rational r)
{
num = num * r.denom + r.num * denom;
denom *= r.denom;
simplify();
return *this;
}

rational &rational::operator-=(rational r)
{
num = num * r.denom - r.num * denom;
denom *= r.denom;
simplify();
return *this;
}

rational &rational::operator*=(rational r)
{
num *= r.num;
denom *= r.denom;
simplify();
return *this;
}

rational &rational::operator/=(rational r)
{
num *= r.denom;
denom *= r.num;
simplify();
return *this;
}

rational rational::operator+(rational r)
{
rational result(*this);
return result += r;
}

rational rational::operator-(rational r)
{
rational result(*this);
return result -= r;
}

rational rational::operator*(rational r)
{
rational result(*this);
return result *= r;
}

rational rational::operator/(rational r)
{
rational result(*this);
return result /= r;
}

void rational::put(FILE *f)
{
fprintf(f, "(%ld/%ld)", num, denom);
}

long gcd(long x, long y)
{
x = labs(x);
y = labs(y);
while (x != y)
{
if (x < y)
y -= x;
if (y < x)
x -= y;
}
return x;
}

void rational::simplify()
{
long x = gcd(num, denom);
num /= x;
denom /= x;
}


  3 Responses to “Category : Files from Magazines
Archive   : CUJ9201.ZIP
Filename : 1001098D

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/