sure. header file.
Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
class Fraction
{
private:
int num;
int den;
public:
//constructors
Fraction(int n){set(n, 1);}
Fraction(){num = 0; den = 1;}
Fraction(int n, int d){set(n, d);}
Fraction(Fraction const &src); //defined in .cpp
//operators
Fraction &operator=(const Fraction &src){set(src.num, src.den); return *this;}
Fraction operator+(const Fraction &other); //defined in .cpp
Fraction operator*(const Fraction &other); //defined in .cpp
bool operator==(const Fraction &other); //defined in .cpp
friend ostream &operator<<(ostream &os, Fraction &fr);
void set(int n, int d){num = n; den = d; normalize();}
int get_num(){return num;}
int get_den(){return den;}
Fraction add(Fraction const &other); //defined in the .cpp
Fraction mult(Fraction const &other); //defined in the .cpp
private:
void normalize(); //defined in .cpp
int gcf(int a, int b); //defined in .cpp
int lcd(int a, int b); //defined in .cpp
};
ostream &operator<<(ostream &os, Fraction &fr)
{
os << fr.num << "/" << fr.den;
return os;
}
and then .cpp file
Code:
#include "FinalFraction.h"
using namespace std;
int get_number();
int main()
{
int num;
int den;
int count = 0;
Fraction fract[10];
while(true)
{
cout << "Please enter a value for the num: ";
num = get_number();
cout << "Please enter a value for the den(hit 0 to end): ";
den = get_number();
if(den==0)
break;
fract[count].set(num, den);
count++;
}
for(int i = 0; i <= count; i++)
{
cout << "the num entered is " << fract[i].get_num() << " and the den is " << fract[i].get_num() << endl;
}
system("pause");
}
Fraction::Fraction(Fraction const &src)
{
num = src.num;
den = src.den;
}
Fraction Fraction::operator+(const Fraction &other)
{
return add(other);
}
Fraction Fraction::operator*(const Fraction &other)
{
return mult(other);
}
Fraction Fraction::add(Fraction const &other)
{
Fraction fract;
int n = gcf(den, other.den);
int temp1 = n/den;
int temp2 = n/other.den;
fract.set(num*temp1 + other.num*temp2, n);
return fract;
}
Fraction Fraction::mult(Fraction const &other)
{
Fraction fract;
fract.set(num*other.num, den*other.den);
return fract;
}
void Fraction::normalize()
{
if(num==0 || den==0)
{
num = 0;
den = 1;
}
if (den<0)
{
num*=-1;
den*=-1;
}
int n = lcd(num, den);
num/=n;
den/=n;
}
int Fraction::gcf(int a, int b)
{
if(b==0)
return a;
else
return gcf(b, a%b);
}
int Fraction::lcd(int a, int b)
{
int n = gcf(a, b);
return a/n*b;
}
bool Fraction::operator==(const Fraction &other)
{
if(num==other.num && den==other.den)
return true;
else
return false;
}
int get_number()
{
char s[6];
cin.getline(s, 5);
if(strlen(s) == 0)
return 0;
else
return atoi(s);
}
Thankyou again for looking at it.