Fraction Class
Home
Programs Home
This is a simplistc implementation of a fraction class, along with a
sample program to demonstrate how to use it. The main( ) function, and
the class are defined in the same .cpp source file for simplicity. The
class has simple operators like +, and - defined to enable and
facilitate easy operation of fraction objects.
You can copy the code between the 2 HORIZONTAL LINES, paste it and save
it as ASCII test, and compile it with a standards compliant C++ compiler
such as g++.
#include <iostream>
#include <string> //needed for "system", dunno know why???
#include <cmath>
//#define MY_DEBUG
#define GNU_LINUX
//#define WIN32_CRAP
using namespace std;
class fraction {
private:
int num, den;
int size;
int* primes;
public:
fraction (int n = 0, int d = 0):num(n), den(d) { primes = reinterpret_cast<int*>(0); } //constructor
fraction::fraction (const fraction& f);
void generate_primes (int cnt = 100);
void remove_prime (fraction& f);
#ifdef MY_DEBUG
void disp_prime (void) const
{
for (int i = size-1; i >= 0; i--)
cout<<" "<<primes[i];
cout<<endl;
}
#endif
fraction& operator+= (const fraction& f);
fraction& operator-= (const fraction& f);
fraction& operator= (const fraction& f);
fraction& operator= (const int& elem);
~fraction () { if (primes != reinterpret_cast<int*>(0)) delete[] primes; } //destuctor.
friend fraction operator+ (const fraction&, const fraction&);
friend fraction operator- (const fraction&, const fraction&);
int numerator (void) const
{
return (num);
}
int denominator (void) const
{
return (den);
}
};
fraction::fraction (const fraction& f):num(f.num), den(f.den), size(f.size)
{
primes = new int[f.size];
for (int i = 0; i < f.size; i++)
{
primes[i] = f.primes[i];
}
}
fraction& fraction::operator= (const int& elem)
{
num = elem;
den = 1;
return (*this);
}
fraction& fraction::operator= (const fraction& f)
{
#ifdef MY_DEBUG
cout<<endl<<"In op+ const fraction& f"<<endl;
#endif
num = f.num;
den = f.den;
return (*this);
}
fraction& fraction::operator+= (const fraction& f)
{
num = num * f.den + f.num * den;
den = den * f.den;
if (num == 0) den = 1;
else remove_prime (*this);
if (num < 0 && den < 0)
{
num = -num;
den = -den;
}
return (*this);
}
fraction& fraction::operator-= (const fraction& f)
{
num = num * f.den - f.num * den;
den = den * f.den;
if (num == 0) den = 1;
else remove_prime (*this);
if (num < 0 && den < 0)
{
num = -num;
den = -den;
}
return (*this);
}
//generates 100 primes by default.
void fraction::generate_primes (int cnt = 100)
{
#ifdef MY_DEBUG
cout<<endl<<"In generate_prime "<<cnt<<endl;
#endif
int n = 1;
int f;
if (primes != reinterpret_cast<int*>(0))
{
#ifdef MY_DEBUG
cout<<endl<<"In generate_prime prime: not init, now -> init. "<<cnt<<endl;
#endif
delete[] primes;
primes = new int[cnt];
}
else primes = new int[cnt];
for (int i = 0; i < cnt; )
{
n++;
if (n != 2 && n % 2 == 0) continue;
f = 0;
for (int j = static_cast<int>(sqrt (static_cast<double>(n))); j > 1; j--)
if (n % j == 0) f++;
if (f == 0)
primes[i++] = n;
}
size = cnt;
}
//removes common prime factors form the numerator and denominator.
void fraction::remove_prime (fraction& f)
{
int temp;
for (int i = 0; i < size; i++)
{
temp = primes[i];
if (abs(num) >= temp && abs(den) >= temp)
{
if (num % temp == 0 && den % temp == 0)
{
num /= temp;
den /= temp;
i--;
}
}
else return;
}
}
fraction operator+ (const fraction& f1, const fraction& f2)
{
fraction retval = f1;
retval += f2;
return (retval);
}
fraction operator- (const fraction& f1, const fraction& f2)
{
fraction retval = f1;
retval -= f2;
return (retval);
}
int main()
{
#ifdef GNU_LINUX
system ("clear");
#endif
#ifdef WIN32_CRAP
system ("cls");
#endif
int top1, bottom1, top2, bottom2;
fraction f1, f2, f3;
char loop;
do
{
cout << "Input numerator: ";
cin >> top1;
cout << "Input denominator: ";
cin >> bottom1;
cout << "Input numerator: ";
cin >> top2;
cout << "Input denominator: ";
cin >> bottom2;
cout <<top1<<"/"<<bottom1<<endl<<top2<<"/"<<bottom2<<endl;
cout << endl;
f1 = fraction (top1, bottom1);
f2 = fraction (top2, bottom2);
f1.generate_primes (); // by default, 100 primes are generated.
f2.generate_primes (); // by default, 100 primes are generated.
f3 = f1 + f2; // isn't this cleaner than add= (whatever)..........
cout<<endl<<"Sum = "<<f3.numerator ()<<"\\"<<f3.denominator ()<<endl;
f3 = f1 - f2; // same for this.
cout<<endl<<"Difference = "<<f3.numerator ()<<"\\"<<f3.denominator ()<<endl;
#ifdef MY_DEBUG
f1.disp_prime ();
#endif
// add=((top1*bottom2)+(bottom1*top2))/(bottom1*bottom2);
//cout <<add<<endl;
//sub=((top1*bottom2)-(bottom1*top2))/(bottom1*bottom2);
//cout <<sub<<endl;
cout << "Another fraction?(Y or N):";
cin >> loop;
cout << endl;
} while(loop=='Y'|| loop=='y');// the | is a bitwse OR operator, not logical OR.
return 0;
}