Take A Look....and Help
operator overolad.
The code below does not print the right fractions output,
I am sure the mistake is on the
CFraction (double)
I am looking to add one fraction with a decimal.
and display the outpu as a fraction.
3/2 + 2.75 = 41/12
PLease take a look and see if the logic makes any sense.
Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
class CFraction
{
private:
int numerator, denominator;
public:
CFraction (int num = 0, int den = 1);
CFraction (double);
CFraction operator + (const CFraction&)const;
CFraction operator + (double)const;
CFraction operator * (double)const;
};
int main()
{
CFraction fr(2, 5), fr1, fr2 (2.75);
double x = 2.75;
ifstream in;
ofstream out;
///fr.openInOutFile(in, out);
//cout >>"Enter a value of double: ";
//cin >> x;
//fr2 = x;
//fr1 = fr + x;
fr1 = fr + fr2;
cout << fr << " + " << fr2 << " = " << fr1 << endl;
cout << "\nThe total objects created are: " << fr.getCount();
cout << endl << endl;
return 0;
}
CFraction :: CFraction (int num, int den)
{
numerator = num;
denominator = den;
count ++;
}
CFraction::CFraction(double d)
{
numerator = static_cast<int>(d * 1000);
denominator = static_cast<int> ((d-numerator) * 1000 + 0.5);
}
CFraction CFraction :: operator + (const CFraction& fr2)const
{
CFraction plus;
plus.numerator = (numerator * fr2.denominator)+
(fr2.numerator * denominator);
plus.denominator = denominator * fr2.denominator;
plus.simplify();
return plus;
}
CFraction CFraction :: operator + (double d)const
{
CFraction plus(d);
return *this + d;
}
CFraction CFraction :: operator * (double t)const
{
CFraction times(t);
return *this * times;
}
|
Yes, what is this?
denominator = static_cast<int> ((d-numerator) * 1000 + 0.5);
Try denominator = 1000;
As in: 2.75 = 2750/1000
Can i call a void reduce() inside CFraction::CFraction
And yea i did chamhe that to a 1000. It was late my brain was frozen^^
Topic archived. No new replies allowed.