how can I make these ones to reduce.

Okay so I got everything work on my code except this one part which I am not sure how I would. There are 3 files and one I am not allowed to change.
Here is the one I am not allowed to change.
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
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8); //calling a parameterized class constructor
    fraction f2(2,3); //calling a parameterized class constructor
    fraction result;  //calling a default class constructor
    fraction f3; //calling a default class constructor

    cout << "The result starts off at ";
    result.print(); //calling an observer function
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.MultipliedBy(f2); //a class binary operation - function
    result.print();
    cout << endl;

    f3 = result; //assignment

     if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
        f2.print();
        cout <<" is greater than ";
        f3.print();
        cout<<endl;
    } else {
        f2.print();
        cout <<" is less than ";
        f3.print();
        cout<<endl;
    }

    cout << "The sum of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.AddedTo(f2); //a class binary operation - function
    result.print();
    cout << endl;

    cout << "The difference of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.Subtract(f2); //a class binary operation - function
    result.print();
    cout << endl;

    if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function
        cout << "The two fractions are equal." << endl;
    } else {
        cout << "The two fractions are not equal." << endl;
    }

    const fraction f4(12, 8);
    const fraction f5(202, 303);

    result = f4.DividedBy(f5); //a class binary operation - function
    cout << "The quotient of ";
    f4.print();
    cout << " and ";
    f5.print();
    cout << " is ";
    result.print();
    cout << endl;

    return 0;
}



Now this is my code and I can change this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED

class fraction
{
public:
    fraction (); // default constructor
    fraction (int,int);
    void print() const;
    fraction MultipliedBy(fraction) const;
    bool isGreaterThan(fraction) const;
    fraction AddedTo(fraction) const;
    fraction Subtract(fraction) const;
    bool isEqualTo(fraction) const;
    fraction DividedBy(fraction) const;
private:
    int numerator;
    int denominator;
};

#endif // FRACTION_H_INCLUDED


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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include "fraction.h"

using namespace std;

    fraction::fraction ()// default constructor
    {
        numerator=0;
        denominator=1;
    }

    fraction::fraction (int newNumerator,int newDenominator)
    {
        denominator=newDenominator;
        numerator=newNumerator;
    }

    void fraction::print() const
    {
        cout<<numerator<<"/"<<denominator;
    }

    fraction fraction::MultipliedBy(fraction f) const
    {
        int temp,m,n;
        f.denominator*=denominator;
        f.numerator*=numerator;
       // here is Euclid's Algorithm to reduce the fractions
       //http://simple.wikipedia.org/wiki/Euclidean_algorithm
        m=f.numerator;
        n=f.denominator;
                 if(m < n)
                {
                temp = m;
                m = n;
                n = temp;
                }
        while(n != 0)
                {
                temp = m % n;
                m = n;
                n = temp;
                }
        f.denominator=f.denominator/m;
        f.numerator=f.numerator/m;

        return f;
}

    bool fraction::isGreaterThan(fraction f) const
    {
        int a,b,c,d,e,m;
        a=f.numerator;
        b=f.denominator;
        c=numerator;
        d=denominator;

        e=a*d;
        m=b*c;

        if (e<m)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    fraction fraction::AddedTo(fraction f) const
    {
       int a,b,c,d,num,dem,m,n,temp;
       a=numerator;
       b=denominator;
       c=f.numerator;
       d=f.denominator;

       num=(a*d)+(c*b);
       dem=b*d;
       m=num;
       n=dem;

        if(m < n)
                {
                temp = m;
                m = n;
                n = temp;
                }
        while(n != 0)
                {
                temp = m % n;
                m = n;
                n = temp;
                }
        f.denominator=dem/m;
        f.numerator=num/m;
        return f;
    }

    fraction fraction::Subtract(fraction f) const
    {

        int a,b,c,d,num,dem,m,n,temp;
       a=numerator;
       b=denominator;
       c=f.numerator;
       d=f.denominator;

       num=(a*d)-(c*b);
       dem=b*d;
       m=num;
       n=dem;
        if(m < n)
                {
                temp = m;
                m = n;
                n = temp;
                }
        while(n != 0)
                {
                temp = m % n;
                m = n;
                n = temp;
                }
        f.denominator=dem/m;
        f.numerator=num/m;
        return f;
    }

    bool fraction::isEqualTo(fraction f) const
    {
        int a,b,c,d,e,m;
        a=f.numerator;
        b=f.denominator;
        c=numerator;
        d=denominator;

        e=a*d;
        m=b*c;

        if(e==m)
        {
            return true;
        }

        else
        {
            return false;
        }

    }
    fraction fraction::DividedBy(fraction f) const
    {
      int a,b,c,d,num,dem,m,n,temp;
       a=numerator;
       b=denominator;
       c=f.numerator;
       d=f.denominator;

       num=a*d;
       dem=b*c;
       m=num;
       n=dem;
        if(m < n)
                {
                temp = m;
                m = n;
                n = temp;
                }
        while(n != 0)
                {
                temp = m % n;
                m = n;
                n = temp;
                }
        f.denominator=dem/m;
        f.numerator=num/m;
        return f;

    }


So this is my output


The result starts off at 0/1
The product of 9/8 and 2/3 is 3/4
2/3 is less than 3/4
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
The quotient of 12/8 and 202/303 is 9/4

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.



And the goal output this:


The result starts off at 0/1
The product of 9/8 and 2/3 is 3/4
2/3 is less than 3/4
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
The quotient of 3/2 and 2/3 is 9/4
Press any key to continue . . .



So I need to my 12/8 to reduce to 3/2 and my 202/303 to reduce to 2/3, but I am not sure how to and I am pretty sure it needs to go into my Dividedby function but how I go about it I am not sure.
Last edited on
> I am pretty sure it needs to go into my Dividedby function
The prototype fraction DividedBy(fraction) const;
_ changing this is impossible
_ changing the parameter is meaningless

You need to reduce them in the construction stage.


By the way, make a gcd() function.
Thank you I got it!
Topic archived. No new replies allowed.