Fraction class

I have homework that I've been procrastinating. Here's the problem:

"Create a class for working with fractions. Only 3 private data members are needed: the int numerator, positive int denominator, and a double data member corresponding to the decimal equivalent for the fraction. The following methods should be in your class:

a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced form. Make sure the denominator is not set to 0 or a negative value.
b. Add two fractions and store the sum in reduced form.
c. Subtract two fractions and store the difference in reduced form.
d. Multiply two fractions and store the product in reduced form.
e. Divide two fractions and store the quotient in reduced form.
f. Print a fraction, its decimal equivalent, and as a mixed fraction. For example, the fraction 7/2 should be 3 1/2.

Your main should instantiate two fractions and call each of the class methods. The two fractions should be printed a long with the sum, difference, product, quotient, and after adding 1 to the two fractions. (Please do not overload the operators for this program)."




Also in class for the main he wanted it to look like this:

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

int main()
{
Frac a(3,9), Frac b(10,6), sum, dif, prod, quo;
a,print();
b.print();
sum.add(a,b);
dif.subtract(a,b);
prod.mult(a,b);
quo.div(a,b);
cout << "sum is ";
sum.print();
cout << "difference is ";
dif.print();
cout << "product is ";
prod.print();
cout << "quotient is ";
quo.print();
a.addone();
b.addone();
cout << "After add one the fractions are ";
a.print();
b.print();
return 0;


What I have so far is

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

#include<iostream>

using namespace std;

class Frac
{
public:
	Frac(int = 0, int = 1);
	void print();
	void add(Frac,Frac);
	void subtract (Frac,Frac);
	void mult(Frac, Frac);
	void div (Frac, Frac);
	void addone();
private:
	int num, den;
	double decimal;
};

Frac::Frac(int n, int d)
{
	num = n;
	if (d <= 0)
	{
		cout << "Error, denominator is equal to 0 or negative" << endl;
		exit(0);
	}
	else
		den = d;
	decimal = n/(float)d;
}

}

void Frac::print()
{
	cout << "The fraction form is " << num << "/" << den << endl;
	cout << "The decimal form is " << decimal << endl;
}

void Frac::add(Frac one, Frac two)
{
	
	
}


I'm having trouble on the math part of the program (add,subtract,mult,div).

Can you guys help me out thanks! If you show me the addition part I think I can get the rest. And for addone would I just add the denominator to the numerator?
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
#include <iostream>

class Frac
{
public:
    Frac(int n = 0, int d = 1);
    void add(Frac f1, Frac f2);
    void print();
private:
    void reduce();
    int num, den;
};

Frac::Frac(int n, int d)
{
    num = n;
    if (d <= 0)
    {
        std::cout << "Error, denominator is equal to 0 or negative" << std::endl;
        exit(0);
    }
    else
        den = d;
}

/* Use Euclid's algorithm to get the greatest common divisor.
   We need this to reduce our fractions after we do operations. */
int gcd(int a, int b)
{
    if(b == 0)
        return a;
    else
        return gcd(b, a%b);
}

/* Find the GCD and divide num and den by it to reduce */
void Frac::reduce()
{
    int divisor = gcd(num, den);
    num /= divisor;
    den /= divisor;
}

void Frac::add(Frac f1, Frac f2)
{
    num = (f1.num * f2.den) + (f2.num * f1.den);
    den = f1.den * f2.den;
    reduce();  //don't forget to reduce!
}

void Frac::print()
{
    std::cout << "The fraction form is " << num << "/" << den << std::endl;
}
int main()
{
    Frac x(1,3), y(5,6), z;
    z.add(x,y);
    z.print();
    return 0;
}
Last edited on
Thank you for the quick reply you helped me so much. Also in class for the GCD part he gave us this:

1
2
3
4
5
6
7
8
9
10
 

int gcd(int a, int b)
{
	if(a == 0)
		return b;
	else
		return gcd(b%a, a)
}


is this acceptable as well?
Yes. As long as you've seen some form of the GCD algorithm and understand why you need it, then you're fine.
ok thanks. I also need help with the mixed fraction part (10/6 should be 1 2/3)

I know how to get it math wise, I just dont know the div function for c++

1
2
3
4
5
6
7
8
9
// this part is in the print() method
int number;
number = den div num; // I don't know the div function

num = den % num;
reduce();


cout << "The mixed fraction form is " << number << " " << num << "/" << den << endl;


actually I think it just came to me. I was thinking about it. If I make number an int, then it den/num will basically be the div function
Last edited on
Use the division operator x / y;.
I wouldn't suggest reassigning the numerator in your line 5. Keep it in storage as an improper fraction, and print the mixed form as I show below. Same for the version of the number that is a double. All you need to keep is the numerator and denominator, then perform the calculations for everything else in real time. That way, you never have to worry about things like the double version of your number being inconsistent with the fraction version.
1
2
3
4
5
6
void Frac::print()
{
    std::cout << "The fraction form is " << num << "/" << den << std::endl;
    std::cout << "The double form is " << ((double)num) / den << std::endl;
    std::cout << "The mixed fraction form is " << num / den << " " << num % den << "/" << den << std::endl;
}
Last edited on
Ok thank you soo much you helped me a lot with this homework!!

Thanks for the advice on not reassigning the numerator on line 5. That was really smart.

I just finished here is my code

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
#include<iostream>

using namespace std;

class Frac
{
public:
	Frac(int n = 0, int d = 1);
	void print();
	void add(Frac one,Frac two);
	void sub(Frac one,Frac two);
	void mult(Frac one, Frac two);
	void div (Frac one, Frac two);
	void addone();
private:
	int num, den;
	double decimal;
	void reduce();
};

Frac::Frac(int n, int d)
{
	num = n;
	if (d <= 0)
	{
		cout << "Error, denominator is equal to 0 or negative" << endl;
		exit(0);
	}
	else
		den = d;
}

int gcd(int a, int b)
{
	if(a == 0)
		return b;
	else
		return gcd(b%a, a);
}


void Frac::reduce()
{
	int GCD = gcd(num, den);
	num /= GCD;
	den /= GCD;
}

void Frac::add(Frac one, Frac two)
{
	num = (one.num * two.den) + (two.num * one.den);
	den = one.den * two.den;
	reduce();
}

void Frac::sub(Frac one, Frac two)
{
	num = (one.num * two.den) - (two.num * one.den);
	den = one.den * two.den;
	reduce();
}

void Frac::mult(Frac one, Frac two)
{
	num = one.num * two.num;
	den = one.den * two.den;
	reduce();
}

void Frac::div(Frac one, Frac two)
{
	num = one.num * two.den;
	den = one.den * two.num;
	reduce();
}

void Frac::addone()
{
	num += den;
	
}

void Frac::print()
{
	if (den == 1)
		cout << "The fraction form is " << num << endl;
	else
		cout << "The fraction form is " << num << "/" << den << endl;
	decimal = num/(float)den;
	cout << "The decimal form is " << num/(double)den << endl;
	cout << "The mixed fraction form is " << num/den << " " << num%den << "/" << den << endl<<endl;
}

int main()
{
	Frac a(3,9), b(10,6), sum, dif, prod, quo;
	a.print();
	b.print();
	sum.add(a,b);
	dif.sub(a,b);
	prod.mult(a,b);
	quo.div(a,b);
	cout << "The sum is ";
	sum.print();
	cout << "The difference is ";
	dif.print();
	cout<< "The product is ";
	prod.print();
	cout << "The quotient is ";
	quo.print();
	a.addone();
	b.addone();
	cout << "After add one the fractions are ";
	a.print();
	b.print();
	system("PAUSE");
	return 0;
}


The only problem I don't like about it is when it prints out a mixed fraction that is a whole number for example: 2, it will print out 2 0/1. Is there anyway to not print it out like that?

Or if the number is just 1/5 it will print out 0 1/5
Last edited on
To fix 2 0/1, check that num % den isn't 0.
1
2
3
4
5
6
7
8
// ...
    std::cout << "The mixed fraction form is " << num / den;
    if((num % den) != 0)
    {
        std::cout << " " << num % den << "/" << den;
    }
    std::cout << std::endl;
// ... 


To fix 1/5, check that num / den isn't 0 in a similar way.
Last edited on
Ok thanks again for all your help! I really appreciate it
Topic archived. No new replies allowed.