Here is the start of my rational class program. The idea is to add, subtract, multiply and divide two fractions. You can see I've declared 1/3 as the first fraction and 2/5 as the second. However I'm not sure how to point to the 1 separate of the 3, and the 2 separate of the 5, to perform the addition correctly. Do I have to set up numerator1 and numerator2, etc?
Also, I have a simplify function to reduce the fraction. Currently the first fraction will reduce (for example, if I replaces 1/3 with 4/8 it would return 1/2) but the second will not, and I'm not sure why.
Code:#include <iostream>
using namespace std;
class Rational
{
public:
Rational(int n=0,int d=1)
{
numerator = n;
denominator = d;
}
void print()
{
cout << numerator << "/" << denominator << endl;
}
void operator = (Rational c)
{
numerator = c.numerator;
denominator = c.denominator;
}
void Rational::simplify() // to simplify the fraction
{
int n=(int)this->numerator;
int d=(int)this->denominator;
int i=2;
while((i<=n) && (n!=1))
{
while((n/i==(int)n/i) && (d/i ==(int)d/i))
{
n/=i;
d/=i;
}
i++;
}
numerator=n;
denominator=(n==0)?1:d;
}
Rational operator + (Rational c) // adds fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator + c.numerator;
t.denominator = denominator + c.denominator;
return t;
}
Rational operator - (Rational c) // subtracts fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator - c.numerator;
t.denominator = denominator - c.denominator;
return t;
}
Rational operator * (Rational c) // multiplies fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator * c.numerator;
t.denominator = denominator * c.denominator;
return t;
}
Rational operator / (Rational c) // divides fractions
{
Rational::simplify();
Rational t;
t.numerator = numerator / c.numerator;
t.denominator = denominator / c.denominator;
return t;
}
private:
int numerator;
int denominator;
}; // end class Rational
int main()
{
Rational a(1,3),b(2,5); // a is (1/3); b is (2/5) // a reduces, b does not
Rational z; // test variable to check default constructor
Rational c; // instantiates variable c to class Rational initializing to zero
c = a + b; // currently adds two numerators (1 + 2) and two denominators (3 + 5)
Rational d; // instantiates d
d = a - b; // subtracts a from b
Rational e; //
e = a*b;
Rational f;
f = a/b;
cout << "First fraction is: " << endl;
a.print();
cout << "\nSecond fraction is: " << endl;
b.print();
cout << "\nAdding first fraction to second fraction: " << endl;
c.print();
cout << "\nSubracting first fraction from second fraction: " << endl;
d.print();
cout << "\nMultiplying first fraction to second fraction: " << endl;
e.print();
cout << "\nDividing first fraction by second fraction: " << endl;
f.print();
cout << "\nTest default fraction: " << endl;
z.print();
return 0;
}