overloading operators

Hi, my assignment is to use overloaded operators to do arithmetic on fractions. My question is, am I supposed to write code to find the LCD when adding and subtracting fractions?

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
using namespace std;

class Rational
{
private:
   int numerator, denominator;
public:
   // Constructor builds a rational number n/d
   Rational(int n, int d):numerator(n), denominator(d)
   {    
      reduce();
   }
   Rational()
   {
	   numerator = 1;
	   denominator = 1;
   }
   void setFraction(int, int);
	   
   friend ostream &operator<<(ostream &output, Rational);
   friend Rational operator*(Rational myNumber, Rational mult);
   friend Rational operator/(Rational myNumber, Rational div);
private:
   // This member function transforms a rational number into 
   // reduced form where the numerator and denominator have 1
   // as greatest common factor
   void reduce();  
};

//************************************************************
// This member function transforms a rational number into    *
// reduced form where the numerator and denominator have 1   *
// as greatest common factor.                                *
//************************************************************
void Rational::reduce()
{
    bool negative = (numerator < 0) != (denominator < 0);
    numerator = abs(numerator);
    denominator = abs(denominator);

    int factor = 2;
    while (factor <= min(numerator, denominator))
    {
       if (numerator % factor == 0 && denominator % factor == 0)
       {
           numerator = numerator / factor;
           denominator = denominator / factor;
           continue;
       }
       factor ++;
    }
    if (negative) 
       numerator = - numerator;
}
ostream &operator<<(ostream &output, Rational myNumber)
{
	output << myNumber.numerator << "/" << myNumber.denominator;
	return output;
}
Rational operator*(Rational myNumber, Rational mult)
{
	Rational multiplied;

	multiplied.numerator = myNumber.numerator * mult.numerator;
	multiplied.denominator = myNumber.denominator * mult.denominator;
	multiplied.reduce();
	return multiplied;
}
 Rational operator/(Rational myNumber, Rational div)
 {
	 Rational divided;
	 divided.numerator = myNumber.numerator * div.denominator;
	 divided.denominator = myNumber.denominator * div.numerator;
	 divided.reduce();
	 return divided;
 }
 void Rational::setFraction(int x , int y)
 {
	 numerator = x;
	 denominator = y;
	 
 }


int main()
{
	Rational number1, number2, result;
	int x, y;
	char sign;

	cout << "What is the numerator and denominator for the first number\n"
	     << "seperated by a space: ";
	cin >> x >> y;
	number1.setFraction(x,y);
	cout << "What is the numerator and denominator for the second number\n"  
		 << "seperated by a space: ";
	cin >> x >> y;
	number2.setFraction(x,y);
	cout << "Which operation would you like to perform (+, -, *, /): ";
	cin >> sign;
	if (sign == '*')
		result = number1 * number2;
	else if (sign == '/')
		result = number1 / number2;
	/*else if (sign == '+')
		result = number1 + number2;
	else if (sign == '-')
		result = number1 - number2;
	else 
	{
		cout << "Invalid sign" << endl;
		return 0;
	}
	*/
	cout << number1 << " " << sign << " " << number2 << " = " << result << endl;
	system ("pause");
	return 0;

}
I wouldn't have thought so.
Topic archived. No new replies allowed.