Class Functions: fractions/gcd

I am attempting to write a program in which the user is prompted to enter the integer values for numerator and denominator of four(4) fractions, named f1,f2,f3,f4 respectively. The program should be capable of returning values for addition, subtraction, multiplication, and division of the user's data input. Also, the program must automatically compute and display the following expressions:

1. f1 + f3
2. f3(f4 − f2)/(f3 − f4)
3. f
6
3
4. (f1/f2)/(f3 + f4)
5. 1/(f3f4)
6. f1(f3 − f4)/f2 = f1/(f2(f3 − f4))

I am hitting a few roadblocks and not sure where to go from here. The compiler errors I am currently receiving (per add/sub/mult/div) are :

" In member function `void Fraction::add(const Fraction&, const Fraction&) const':
80: assignment of data-member `Fraction::numerator' in read-only structure
81: assignment of data-member `Fraction::denominator' in read-only structure "

Any words of wisdom??? Thanks!!!!


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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

class Fraction
{
	private:
		int numerator;
		int denominator;
		int gcd(int a, int b);
		void reduce(Fraction);
	
	public:
		Fraction() : numerator(0), denominator(1)
		{}
		Fraction(int n) : numerator(n), denominator(1)
		{}
		Fraction(int n, int d) : numerator(n), denominator(d)
		{}
		
		void sglfrac();
		void displaysgl();
		
		void str() const;
		
		void add(const Fraction& f1, const Fraction& f2) const;
		void subtract(const Fraction& f1, const Fraction& f2) const;
		void multiply(const Fraction& f1, const Fraction& f2) const;
		void divide(const Fraction& f1, const Fraction& f2) const;
		void equal(const Fraction& f1, const Fraction& f2) const;
		
			
};

void Fraction::reduce(Fraction)
{
	int a,b,r,gcd;
	a = abs(numerator);
	b = abs(denominator);
	if (b == 0)
	{
		cout << "Illegal fraction: division by 0";
		exit(1);
	}
	else if (a == 0)
	{
		numerator = 0;
		denominator = 1;
		return;
	}
	while (a != 0)
		if (a < b)
		{
			r = a; a = b; b = r;
			a = a - b;
		}
	gcd = b;
	numerator = numerator / gcd;
	denominator = denominator / gcd;
}
			
void Fraction::sglfrac()
{
	cout << "numerator: ";
	cin >> numerator;
	cout << "denominator: ";
	cin >> denominator;
}

void Fraction::displaysgl()
{
	cout << numerator << " / " << denominator;
}

void Fraction::add(const Fraction& f1, const Fraction& f2) const
{
	numerator = (f1.numerator * f2.denominator) + (f1.denominator * f2.numerator);
	denominator = (f1.denominator * f2.denominator);
}

void Fraction::subtract(const Fraction& f1, const Fraction& f2) const
{
	numerator = (f1.numerator * f2.denominator) - (f1.denominator * f2.numerator);
	denominator = (f1.denominator * f2.denominator);
}

void Fraction::multiply(const Fraction& f1, const Fraction& f2) const
{
	numerator = f1.numerator * f2.numerator;
	denominator = f1.denominator * f2.denominator;
}

void Fraction::divide(const Fraction& f1, const Fraction& f2) const
{
	numerator = (f1.numerator * f2.denominator);
	denominator = (f1.denominator * f2.numerator);
}

int main()
{
	int a,b;
	
	Fraction f1(a,b);
	Fraction f2(a,b);
	Fraction f3(a,b);
	Fraction f4(a,b);
	
	cout << "Enter the numerator and denominator of f1 >";
	f1.sglfrac();
	cout << "Enter the numerator and denominator of f2 >";
	f2.sglfrac();
	cout << "Enter the numerator and denominator of f3 >";
	f3.sglfrac();
	cout << "Enter the numerator and denominator of f4 >";
	f4.sglfrac();
}

The errors say that because add is a const method, you can't modify members in it. In general, these functions are declared poorly. Now, if you wrote foo.add(bar, baz), it would do foo = bar + baz. I guess it's fine if it doesn't bother you, but I think that a static method add(a, b) equivalent to return a+b, or a method a.add(b), equivalent to a+=b (or return a+b) would make more sense.

As for the rest of the assignment, do you need to do parsing? If yes, try to find what is the outermost operation. Then split the string at that point and evaluate the two halves.
As stated in the project syllabus:


A sample run of the program should execute as follows:

Enter the numerator and denominator of f1 > 3 5
Enter the numerator and denominator of f2 > 6 10
Enter the numerator and denominator of f3 > -5 3
Enter the numerator and denominator of f4 > 5 3

3/5 + -5/3 = ....
-5/3(5/3 - 6/10) / (-5/3 - 5/3) = ...
(-5/3)^6 = ...
(3/5 / 6/10) / (-5/3 + 5/3) = ...
1 / (-5/3 x 5/3) = ...
3/5(-5/3 - 5/3) / 6/10 = -10/3 <> 3/5 / (6/10(-5/3 - 5/3)) = -3/10

Run you program for various values of the fractions f1, f2, f3 and f4, including
cases in which there are divide-by-zero errors and cases in which there are
not any. Make sure that all of the arithmetic operators work properly and
that all fractions are displayed in their normalized in-line form

It may be helpful to include this, as instructed in the program's syllabus:

The Fraction class should also provide the following functions:

1. constructors: There will be three versions of the constructor:

(a) Fraction() : the default constructor sets the top to 0 and bottom
to 1; it creates the fraction 0/1.

(B) Fraction(int n) : the second constructor sets top to n and bottom
to 1; it creates the fraction n/1.

© Fraction(int n, int d): the third constructor sets top to n and
bottom to d. A precondition here is that d must not be 0. If d is
0, terminate the program..

2. Define these functions using the headers below and observe the following:

(a) Fraction add(const Fraction& f ) const : The sum must be
normalized. Neither operands should be changed.

(B) Fraction subtract(const Fraction& f ) const : The difference
must be normalized. Neither operands should be changed.

© Fraction multiply(const Fraction& f ) const : The product
must be normalized. Neither operands should be changed.

(d) Fraction divide(const Fraction& f ) const : The quotient
must be normalized. Neither operands should be changed. If f is
equal to the zero fraction, throw a string exception.

(e) bool equal(const Fraction& f ) const : determines whether
two fractions are equal.


I have updated my code to the following, but still receiving errors::

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
121
122
123
124
 #include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

class Fraction
{
	private:
		int numerator;
		int denominator;
		int gcd(int a, int b);
		void reduce();
	
	public:
		Fraction() : numerator(0), denominator(1)
		{}
		Fraction(int n) : numerator(n), denominator(1)
		{}
		Fraction(int n, int d) : numerator(n), denominator(d)
		{}
		
		void sglfrac();
		void displaysgl();
		
		string str();
		
		Fraction add(const Fraction& f ) const;
		Fraction subtract(const Fraction& f ) const;
		Fraction multiply(const Fraction& f ) const;
		Fraction divide(const Fraction& f ) const;
		bool equal(const Fraction& f );
		
			
};

void Fraction::reduce()
{
	int a,b,r,gcd;
	a = abs(numerator);
	b = abs(denominator);
	if (b == 0)
	{
		cout << "Illegal fraction: division by 0";
		exit(1);
	}
	else if (a == 0)
	{
		numerator = 0;
		denominator = 1;
		return;
	}
	while (a != 0)
		if (a < b)
		{
			r = a; a = b; b = r;
			a = a - b;
		}
	gcd = b;
	numerator = numerator / gcd;
	denominator = denominator / gcd;
}
			
void Fraction::sglfrac()
{
	cout << "numerator: ";
	cin >> numerator;
	cout << "denominator: ";
	cin >> denominator;
}

void Fraction::displaysgl()
{
	cout << numerator << " / " << denominator;
}

Fraction add(const Fraction& f1, const Fraction& f2 )
{
	Fraction result;
	numerator = (f1.numerator * f2.denominator) + (f1.denominator * f2.numerator);
	denominator = (f1.denominator * f2.denominator);
	numerator.reduce();
	result.reduce();
	return result;
}

Fraction subtract(const Fraction& f1, const Fraction& f2 ) 
{
	numerator = (f1.numerator * f2.denominator) - (f1.denominator * f2.numerator);
	denominator = (f1.denominator * f2.denominator);
}

Fraction multiply(const Fraction& f1, const Fraction& f2 )
{
	numerator = f1.numerator * f2.numerator;
	denominator = f1.denominator * f2.denominator;
}

Fraction divide(const Fraction& f1, const Fraction& f2 ) 
{
	numerator = (f1.numerator * f2.denominator);
	denominator = (f1.denominator * f2.numerator);
}

int main()
{
	int a,b;
	
	Fraction f1(a,b);
	Fraction f2(a,b);
	Fraction f3(a,b);
	Fraction f4(a,b);
	
	cout << "Enter the numerator and denominator of f1 >";
	f1.sglfrac();
	cout << "Enter the numerator and denominator of f2 >";
	f2.sglfrac();
	cout << "Enter the numerator and denominator of f3 >";
	f3.sglfrac();
	cout << "Enter the numerator and denominator of f4 >";
	f4.sglfrac();
}
 


So the assignment tells you to use the kind of add, such that a.add(b) does return a+b. The problem with your code is that you made add and etc. global functions, so they can't have access to private members of fraction. Note, also, that these functions have nothing to do with the methods you declared.
Topic archived. No new replies allowed.