Program to do simple fraction math...reduce function won't reduce.

Hey guys,

I'm having a bit of trouble getting my reduce function to work on this. It's a homework assignment whose purpose is to do simple fraction math (including reducing it to lowest terms). For some reason my reduce won't reduce...and my operator functions aren't working correctly. Just wondering if anyone could take a look and see if anything is obviously wrong. Thank you so much!

Jade

rational.h
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

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

class Rational
{

    private:
        int numerator;
        int denominator;

    public:
        friend ostream&operator << (ostream&, Rational);
        Rational operator + (Rational);
        Rational operator - (Rational);
        Rational operator * (Rational);
        Rational operator / (Rational);
        Rational operator = (Rational);
        bool operator < (Rational);
        bool operator > (Rational);
        bool operator == (Rational);
        Rational (int, int);
        Rational ();
        int getNumerator();
        int getDenominator();
        void setNumerator(int);
        void setDenominator(int);
        void reduce (int&, int&);

};

#endif 


testrational.cpp
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

#include <iostream>
using namespace std;
#include "rational.h"

int main()
{
    cout << "Testing Constructors" << endl;
    Rational A(9,-12), B(8,12), C(2,0), D(1,3), E, F(0,4);

    cout << endl <<"object A - expecting -3/4" << endl;
    cout << A;
    cout << endl << "object B - expecting 2/3" << endl;
    cout << B;
    cout << endl << "object C - expecting substitute default values"
          << endl;
    cout << C;
    cout << endl << "object D - expecting 1/3" << endl;
        cout << D;
    cout << endl << "object E - expecting default, legal fraction"
          << endl;
    cout << E;
    cout << endl << "object F - expecting 0/4" << endl;
    cout << F << endl;

    cout << endl << "Float of B - expecting 0.667 showing a fixed"
         << " number of decimals" << endl;

    cout << endl << endl;
    cout << "Testing math and assignment";
    C=B+D;
    cout << endl << "*** + expecting 1/1" << endl;
    cout << C;
    C=B-D;
    cout << endl << "*** - expecting 1/3" << endl;
    cout << C;
    C=A*B;
    cout << endl << "*** * expecting -1/2" << endl;
    cout << C;
    C=A/B;
    cout << endl << "*** / expecting -9/8" << endl;
    cout << C << endl;
    cout << "*** /0 expecting some legal default value" << endl;
    C=B/F;
    cout << C << endl;

    cout << endl << endl << "Testing set functions" << endl;
    C.setNumerator(-12);
    C.setDenominator(-16);
    cout << "*** expecting 3/4" << endl;
    cout << C << endl;
    C.setNumerator(-10);
    cout << "*** expecting -5/2" << endl;
    cout << C << endl;
    cout << "*** expecting divide by zero error -> 1/1" << endl;
    C.setDenominator(0);
    cout << C << endl;

    cout << endl << endl << "Testing Comparisons" << endl;

    if( A < D )
       cout << A << " is less than " << D << endl;
    if( D > A )
       cout << D << " is greater than " << A << endl;
    if(!( A == D ))
       cout << A << " is not equal to " << D << endl;
    Rational G(-12,-36);
    if( D == G )
       cout << D << " equals " << G << endl;
        return 0;
}


rational.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

#include "rational.h"

ostream & operator << (ostream&output, Rational r)
{
    output << r.numerator << "/" << r.denominator;
    return output;
}

Rational::Rational() : numerator(1), denominator(1)
{

}


Rational::Rational(int num, int den)
{

    reduce(num, den);
    setNumerator(num);
    setDenominator(den);

}


int Rational::getNumerator()
{
    return numerator;
}

int Rational::getDenominator()
{
    return denominator;
}

void Rational::setNumerator(int num)
{
    int den = getDenominator();
    reduce(num, den);
    numerator = num;
}

void Rational::setDenominator(int den)
{
    if (den == 0)
    {
        cout << "Zero is not a legal denominator. Setting to 1 instead.";
        denominator = 1;
    }

    else
    {
        int num = getNumerator();
        reduce(num, den);

        denominator = den;
    }
}

void Rational::reduce(int&numer, int&denom)
{
    int min;

    if (numer >= 0 && denom > 0)
    {

        if (numer < denom)
        {
            min = numer;
        }

        else
        {
            min = denom;
        }
    }

    if (numer < 0 && denom > 0)
    {
        if (numer * -1 < denom)
        {
            min = numer;
        }
        else
        {
            min = denom;
        }
    }

    if (numer > 0 && denom < 0)
    {
        if (numer < denom * -1)
        {
            min = numer;
        }
        else
        {
            min = denom;
        }
    }

    int n = min;

    for (int count = n; count > 1; count --)
    {
        if (numer %count == 0 && denom % count == 0)

        {
            numer /= count;
            denom /= count;
        }
    }

}


Rational Rational::operator + (Rational r)
{
    int nm1, nm2, nmsum;
    int dn;
    Rational sum;


    dn = getDenominator() * r.getDenominator();
    nm1 = dn * getNumerator();
    nm2 = dn * r.getNumerator();
    nmsum = nm1 + nm2;
    sum.reduce(nmsum, dn);
    sum.setDenominator(dn);
    sum.setNumerator(nmsum);
    return sum;
}

Rational Rational::operator - (Rational r)
{
    int nm1, nm2, nmdiff;
    int dn;
    Rational diff;

    dn = getDenominator() * r.getDenominator();
    nm1 = dn * getNumerator();
    nm2 = dn * r.getNumerator();
    nmdiff = nm1 - nm2;
    diff.reduce(nmdiff, dn);
    diff.setDenominator(dn);
    diff.setNumerator(nmdiff);
    return diff;
}

Rational Rational::operator * (Rational r)
{
    int nm;
    int dn;
    Rational prod;

    dn = getDenominator() * r.getDenominator();
    nm = getNumerator() * r.getNumerator();
    prod.reduce(nm, dn);
    prod.setNumerator(nm);
    prod.setDenominator(dn);
    return prod;
}

Rational Rational::operator / (Rational r)
{

    int nm;
    int dn;
    Rational quot;

    nm = getNumerator() * r.getDenominator();
    dn = r.getNumerator() * getDenominator();
    quot.reduce(nm, dn);
    quot.setNumerator(nm);
    quot.setDenominator(dn);
    return quot;
}

Rational Rational::operator = (Rational r)
{
    int nmTemp;
    int dnTemp;

    nmTemp = getNumerator();
    dnTemp = getDenominator();
    r.setNumerator(nmTemp);
    r.setDenominator(dnTemp);
    return r;
}

bool Rational::operator < (Rational r)
{
    int nm1, nm2;
    int dn;
    bool compare;

    dn = getDenominator() * r.getDenominator();
    nm1 = getNumerator() * dn;
    nm2 = r.getNumerator() * dn;

    if (nm1 < nm2)
    {
        compare = 1;
    }

    else
    {
        compare = 0;
    }
    return compare;
}

bool Rational::operator > (Rational r)
{
    int nm1, nm2;
    int dn;
    bool compare;

    dn = getDenominator() * r.getDenominator();
    nm1 = getNumerator() * dn;
    nm2 = r.getNumerator() * dn;

    if (nm1 > nm2)
    {

        compare = 1;
    }

    else
    {
        compare = 0;
    }

    return compare;
}

bool Rational::operator == (Rational r)
{
    int nm1, nm2;
    int dn;
    bool compare;

    dn = getDenominator() * r.getDenominator();
    nm1 = getNumerator() * dn;
    nm2 = r.getNumerator() * dn;

    if (nm1 == nm2)
    {
        compare = 1;
    }
    else
    {
        compare = 0;
    }

    return compare;
}

Topic archived. No new replies allowed.