Fraction Class

Hi, so I had to make a Fraction class for a programming class. It was supposed to convert the fractions to a decimal, reduce them, and use the overloaded ==, <, <=, >, >= operators to compare the fractions. I submitted it and my professor said the following - "If you return an int/int it will return an int: 1/2=0.
You need to return 1.0*numerator/denominator.
The easiest way to compare two fractions is to compare the toDecimal values." I don't really understand what she wants me to do. Here's all of 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
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

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

class Fraction
{
    private:
    double numerator;
    double denominator;
    public:
    Fraction(){};
    Fraction(double num, double denom)
    {
        numerator = num;
        if (denom!=0)
          denominator = denom;
    }

  
void setNumerator(double num)
{
    numerator=num;
}
  
void setDenominator(double denom)
{
    if (denom != 0)
        denominator=denom;

}
  
double toDecimal()
{
    return (1.0*(numerator/denominator));
}
  
string toString() const
{
    string String=static_cast<ostringstream*>(&(ostringstream() << numerator << "/" << denominator) )->str();
    return String;
}
  
double gcd (double num, double denom)
{
    if(denom==0)
    return num;
  
    else
    return gcd(denom, fmod(num, denom));
}

void reduce()
{
    int GCD= gcd(numerator, denominator);
    numerator/= GCD;
    denominator /=GCD;
}

  
void operator ++(int)
{
    numerator++;
    denominator++;
}
  
void operator --(int)
{
    if(denominator-1 !=0)
    {
        denominator--;
        numerator--;
    }
}
  
void operator=(const Fraction &copyfrom)
{
    numerator=copyfrom.numerator;
    denominator=copyfrom.denominator;
}
  
bool operator > (const Fraction &right)
{
    bool status;
    if ((numerator/denominator) > (right.numerator/right.denominator))
        status=true;
    else
        status=false;
    return status;
}
  
bool operator < (const Fraction &right)
{
    bool status;
    if ((numerator/denominator) < (right.numerator/right.denominator))
        status=true;
    else
        status=false;
    return status;
}
  
bool operator == (const Fraction &right)
{
    bool status;
    if ((numerator/denominator) == (right.numerator/right.denominator))
        status=true;
    else
        status=false;
    return status;
}
  
bool operator >= (const Fraction &right)
{
    bool status;
    if ((numerator/denominator) >= (right.numerator/right.denominator))
        status=true;
    else if ((numerator/denominator) == (right.numerator/right.denominator))
        status=true;
    else
        status=false;
    return status;
}
  
bool operator <= (const Fraction &right)
{
    bool status;
    if ((numerator/denominator) < (right.numerator/right.denominator))
        status=true;
    else if ((numerator/denominator) == (right.numerator/right.denominator))
        status=true;
    else
        status=false;
    return status;
}
  
double getNumerator ()
{
    return numerator;
}

double getDenominator()
{
    return denominator;
}

};

int main()
{
    double num;
    double denom;
    double num2;
    double denom2;
    Fraction frac;
    Fraction frac2;
    Fraction frac3;
  
    cout << "Please enter the numerator: ";
    cin >> num;
    frac.setNumerator(num);
  
    cout << "Please enter the denominator: ";
    cin >> denom;
    frac.setDenominator(denom);
      
    cout << "Please enter the second numerator: ";
    cin >> num2;
    frac2.setNumerator(num2);
  
    cout << "Please enter the second denominator: ";
    cin >> denom2;
    frac2.setDenominator(denom2);

    cout << "Your fraction is: " << frac.toString() << "." << endl;
    cout << "As a decimal your fraction is: " << frac.toDecimal() << "." << endl;
    cout << "Your fraction as a string is: " << frac.toString() << "." << endl;
    frac.reduce();
    cout << "Your fraction reduced is: " << frac.toString() << "." << endl;
  
    cout << "Your second fraction is: " << frac2.toString() << "." << endl;
    cout << "As a decimal your second fraction is: " << frac2.toDecimal() << "." << endl;
    cout << "Your second fraction as a string is: " << frac2.toString() << "." << endl;
    frac2.reduce();
    cout << "Your second fraction reduced is: "<< frac2.toString() << "." << endl;
  
    if (frac == frac2)
        cout << "Your first fraction is equal to your second fraction." << endl;
    if (frac > frac2)
        cout << "Your first fraction is greater than your second fraction." << endl;
    if (frac < frac2)
        cout << "Your first fraction is less than your second fraction." << endl;
    if (frac <= frac2)
        cout << "Your first fraction is less than or equal to your second fraction." << endl;
    if (frac >= frac2)
        cout << "Your first fraction is greater than or equal to your second fraction." << endl;
    cout << "I will now increment both fractions." << endl;
    frac++;
    frac2++;
    cout << "Your first fraction: " << frac.toString() << endl;
    cout << "Your second fraction: " << frac2.toString() << endl;
    cout << "I will no decrement both fractions." << endl;
    frac--;
    frac2--;
    cout << "Your first fraction: " << frac.toString() << endl;
    cout << "Your second fraction: " << frac2.toString() << endl;
  
  return 0;
  
}


Thank you in advance!
Last edited on
Your code won't compile as it is because you never defined what variable "frac" was in your operator >

Also it seems you've edited your code because you've gained a partial understanding of what your teacher meant, but in case you weren't quite sure, what she's basically telling you is: integer arithmetic results in whole numbers while floating point and higher grade arithmetic results in more accurate numbers.

Ex. In integer arithmetic, 5/8 = 0. In floating point, 5/8 = 0.625.

And she's also saying that for an easier time of comparison, either use your own written toDecimal method that multiplies everything by 1.0 and therefore converts into float, or you can just straight up cast it into a float like: float(5/8)
Oops! I must've accidentally left that there. I was messing with it before to try and figure out how to fix the toDecimal thing. It used to compile. Let me put that back.

I understood integer arithmetic. I didn't understand why there was an issue in the first place? I defined everything as doubles to ensure there wasn't an issue.

How do I use the toDecimal method within the overloaded functions? I don't understand how I'm supposed to do that.
What do you mean? Your toDecimal function is a class function, part of your Fraction, so it can access any variable within the class scope. Since toDecimal returns a double, all you have to do is compare it to your class objects

1
2
3
4
5
Fraction frac1; // I assume you have it initialized to some value already
Fraction frac2; // same as before

if( frac1.toDecimal() < frac2.toDecimal() ) // since you already have an overloaded operator. it's just a class object calling on a class's member function
    // etc. 

Last edited on
Is that in the main function? I don't understand how to put that in the class?

-sorry I'm really confused, and thank you for your help!
Basically, yes.

You don't need to put it in the class because you already have it in the class. You have basically every overloaded operator you'd need for this level of arithmetic, and you already have your toString and toDecimal class functions.

Just look at line 175 in your code. Notice how you have a class object (frac) and have called a class function (toString). Same principle, but with if statements.
Okay. I thought she was saying my overloaded operator functions were wrong? As in I thought she wanted me to use the toDecimal function within the overloaded operator function to compare the fractions and couldn't figure out how I was supposed to do this. Like this (except this obviously very wrong.) -

1
2
3
4
5
6
7
8
9
10
11
bool operator <= (const Fraction &right)
{
    bool status;
    if ((frac.toDecimal()) < (frac2.toDecimal))
        status=true;
    else if ((frac.toDecimal) == (frac2.todecimal()))
        status=true;
    else
        status=false;
    return status;
}



-thank you again, I really appreciate your patience-
I suspect you'll lose points for having numerator and denominator as doubles instead of integers. After all, Frac f(18.3457, 0.338473) probably shouldn't even be legal.

Line 17: if denom == 0, then denominator is uninitialized. I'd set it to denom in all cases and deal with the possibility of infinity as it comes up.

Line 41: Simplfy to:
1
2
3
ostringstream os;
os << numerator << '/' << denominator;
return os.str();


Line 54: I suggest that you also handle a negative denominator:
1
2
3
4
if (denominator < 0) {
    numerator = -numerator;
    denominator = -denominator;
}

Line 62: So if f=2/3 then f++ is 3/4? That doesn't seem right.

Line 68: Similar comment as line 62.

Lines 85-90: I think the prof meant that you can replace this with return (toDecimal() < right.toDecimal());. Same with the other comparison operators.
closed account (48T7M4Gy)
The easiest way to compare two fractions is to compare the toDecimal values." I don't really understand what she wants me to do. Here's all of my code.


Sounds like the hardest way to me.

If you have two fractions a/b and c/d then,
if a/b == c/d then a * d - b * c == 0
if a/b >= c/d then a * d - b * c >= 0 etc etc

From memory a * d - b * c is called the discriminant which is constant for all the tests (whatever the name).
Thank you everyone!
Topic archived. No new replies allowed.