# Need Help Creating a Fraction CLass

• 08-27-2014, 10:59 PM
mike1971
Need Help Creating a Fraction CLass
Instructions:
Write a class to handle objects of fraction type. In its simplest form, a fraction is just two integer values: a numerator and a denominator. fractions can be negative, may be improper (larger numerator than denominator), and can be whole numbers (denominator of 1).

You should write the class in three phases. I've set it up this way because there are a couple of functions that are very difficult and I want to provide a way for you to skip those two functions and move on if you need to. If you submit the assignment with only phase 1 (or phases 1 and 2) completed you can still earn almost all of the points.

• Phase 1 Write everything except the extraction operator (>>) and the reduce function. To do this, you'll want to comment out that part of the client program that tests the extraction operator, and just leave fractions un-reduced.
Don't try to write phase 1 all at once! Use iterative development! By one week before the due date you should have completed and exhaustively tested the following: the constructors, a super-simple version of the insertion operator (just print the numerator, a slash, and the denominator, so you can see if your output is correct), the basic arithmetic operators, and the relational operators. You can test these by commenting out irrelevant portions of the provided client.

Your class should support the following operations on fraction objects:

• Construction of a fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero fraction.
• Printing a fraction to a stream with an overloaded << operator. The fraction should be printed in reduced form (not 3/6 but 1/2). Whole numbers should print without a denominator (e.g. not 3/1 but just 3). Improper fractions should be printed as a mixed number with a + sign between the two parts (2+1/2). Negative fractions should be printed with a leading minus sign.
• Reading a fraction from a stream using an overloaded >> operator. You should be able to read any of the formats described above (mixed number, negative, whole numbers). You may assume that there are no spaces or formatting errors in the fractions that you read. Note: You may need to exceed 15 lines for this function. My solution is about 20 lines long.
• All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare fractions to other fractions as well as fractions to integers. Either fractions or integers can appear on either side of the binary comparison operator.
• The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow fractions to be combined with other fractions, as well as with integers. Either fractions or integers can appear on either side of the binary operator.
• The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. fractions can appear on the left-hand side, and fractions or integers on the right-hand side.
• The increment and decrement (++, --) operators should be supported in both prefix and postfix form for fractions. To increment or decrement a fraction means to add or subtract (respectively) one (1).

• The name of your class must be "fraction". No variations will work.
• Use exactly two data members.
• Your class must guarantee that fractions are stored in reduced form at all times, not just when they are output.
• The '+' in mixed numbers does not mean add. It is simply a separator (to separate the integer part from the fraction part of the number). So the fraction "negative two and one-sixth" would be written as -2+1/6, even though -2 plus 1/6 is not what we mean.
• Your extraction operator must be able to handle a plain integer in the input stream (and store it as a fraction). Since your extraction operator should not consume anything after the end of the fraction being read, you will have to use the .peek() function to look ahead in the input stream and see what the next character is after the first number is read. If it's not either a '/'' or a '+', then you are done reading and should read no further. I have something like this:
in >> temp;
if (in.peek() == '+'){
doSomething...
} else if (in.peek() == '/'){
doSomethingElse...
} else {
doThirdOption
}
• You should not compare two fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time. I would simply cross multiply and compare the products.
• Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.
• The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations. If you have a problem with overflow even when you are using denominators that are small enough to be multiplied together, then you have a problem.
• About comments: Don't forget to read the Style Conventions section of our syllabus, where commenting guidelines for classes are described in detail. In particular, every public member function (and friend function), however simple, must have a precondition (if there is one) and a postcondition listed. Only the most complex of your function definitions will need additional comment.
• My solution had 22 functions. All but three of them (<<, >>, and reduce()) were less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off. My reduce() and << have about 10 - 14 lines. >> has 21 lines. This is including declarations and lines that have nothing but a close curly brace.
I have included the files below on what I have so far.
Attachment 2279Attachment 2281
• 08-28-2014, 10:07 AM
SpywareDr
Is this a homework assignment?
• 08-21-2017, 04:07 AM
Foose
Did you figure out how to do it?
• 09-02-2017, 06:52 PM
MitchellRandle
Thanks for the provided information! However, whet type of the assignment it is?
• 01-16-2018, 11:18 AM
2kaud
This was probably a homework assignment. However, the requirement has long passed! One way this could be done using VS c++17 is as below

Code:

```#include <iostream> #include <numeric> using namespace std; class fraction { public:         fraction(int n, int d) : num(n), den(d) {                 reduce();         }         explicit fraction(int i = 0) : num(i), den(1) {}         fraction(const fraction& fr) = default;         fraction(fraction&& fr) = default;         fraction& operator=(const fraction& fr) = default;         fraction& operator=(fraction&& fr) = default;         fraction& operator=(int i)         {                 num = i;                 den = 1;                 return *this;         }         fraction& reduce()         {                 if (den == 0) {                         num = 0;                         den = 1;                         throw overflow_error("Denominator zero exception");                 }                 if (den < 0) {                         num *= -1;                         den *= -1;                 }                 if (den > 1)                         if (const int g = gcd(num, den); g != 1) {                                 num /= g;                                 den /= g;                         }                 return *this;         }         fraction& invert()         {                 swap(num, den);                 return *this;         }         fraction& neg()         {                 num *= -1;                 return *this;         }         fraction operator-()         {                 fraction f(*this);                 return f.neg();         }         fraction& operator+=(const fraction& fr)         {                 const int l = lcm(fr.den, den);                 num = l / den * num + l / fr.den * fr.num;                 den = l;                 return reduce();         }         fraction& operator+=(int i)         {                 return *this += fraction(i);         }         fraction operator+(const fraction& fr) const         {                 fraction f(*this);                 return f += fr;         }         fraction operator+(int i) const         {                 fraction f(*this);                 return f += i;         }         fraction& operator*=(const fraction& fr)         {                 num *= fr.num;                 den *= fr.den;                 return reduce();         }         fraction& operator*=(int i)         {                 return *this *= fraction(i);         }         fraction operator*(const fraction& fr) const         {                 fraction f(*this);                 return f *= fr;         }         fraction operator*(int i) const         {                 fraction f(*this);                 return f *= i;         }         fraction& operator/=(const fraction& fr)         {                 fraction f(fr);                 return *this *= f.invert();         }         fraction& operator/=(int i)         {                 fraction f(i);                 return *this *= f.invert();         }         fraction operator/(const fraction& fr) const         {                 fraction f(fr);                 return f.invert() *= *this;         }         fraction operator/(int i) const         {                 fraction f(i);                 return f.invert() *= *this;         }         fraction& operator-=(const fraction& fr)         {                 fraction f(fr);                 return *this += f.neg();         }         fraction& operator-=(int i)         {                 fraction f(i);                 return *this += f.neg();         }         fraction operator-(const fraction& fr) const         {                 fraction f(fr);                 return f.neg() += *this;         }         fraction operator-(int i) const         {                 fraction f(i);                 return f.neg() += *this;         }         fraction& operator++()        //Prefix         {                 return *this += 1;         }         fraction operator++(int) // Postfix         {                 fraction f(*this);                 ++(*this);                 return f;         }         fraction& operator--()        //Prefix         {                 return *this -= 1;         }         fraction operator--(int) // Postfix         {                 fraction f(*this);                 --(*this);                 return f;         }         bool operator==(const fraction& fr) const         {                 return (fr.num == num) && (fr.den == den);         }         bool operator!=(const fraction& fr) const         {                 return !(*this == fr);         }         bool operator>(const fraction& fr) const         {                 const int l = lcm(fr.den, den);                 return ((l / den * num) > (l / fr.den * fr.num));         }         bool operator <=(const fraction& fr) const         {                 return !(*this <= fr);         }         bool operator>=(const fraction& fr) const         {                 return (*this == fr) || (*this > fr);         }         bool operator<(const fraction& fr) const         {                 return !(*this >= fr);         }         bool operator==(int i) const         {                 return *this == fraction(i);         }         bool operator!=(int i) const         {                 return !(*this == i);         }         bool operator>(int i) const         {                 return *this > fraction(i);         }         bool operator<=(int i) const         {                 return !(*this > i);         }         bool operator>=(int i) const         {                 return *this >= fraction(i);         }         bool operator<(int i) const         {                 return !(*this >= i);         }         friend ostream& operator<<(ostream& os, const fraction& fr);         friend istream& operator>>(istream& is, fraction& fr); private:         int num = 0;         int den = 1; }; inline bool operator==(int i, const fraction& fr) {         return fr == i; } inline bool operator!=(int i, const fraction& fr) {         return fr != i; } inline bool operator>(int i, const fraction& fr) {         return fr < i; } inline bool operator>=(int i, const fraction& fr) {         return fr <= i; } inline bool operator<(int i, const fraction& fr) {         return fr > i; } inline bool operator<=(int i, const fraction& fr) {         return fr >= i; } inline fraction operator+(int i, const fraction& fr) {         return fr + i; } inline fraction operator*(int i, const fraction& fr) {         return fr * i; } inline fraction operator/(int i, const fraction& fr) {         fraction f(fr);         return f.invert() * i; } inline fraction operator-(int i, const fraction& fr) {         fraction f(fr);         return f.neg() + i; } ostream& operator<<(ostream& os, const fraction& fr) {         if ((abs(fr.num) > fr.den) && (fr.den > 1))                 cout << fr.num / fr.den << "+" << fraction(abs(fr.num) % fr.den, fr.den);         else {                 os << fr.num;                 if (fr.den != 1)                         os << "/" << fr.den;         }         return os; } istream& operator>>(istream& is, fraction& fr) {         auto getden = [&]() {                 is >> fr.den;                 if (fr.den == 0) {                         is.setstate(ios::failbit);                         fr = 0;                 }         };         int n = 0;         fr = 0;         is >> n;         if (auto ch = is.peek(); (ch == '+') || (ch == '/')) {                 is.get();                 if (ch == '+') {                         is >> fr.num;                         if (is.peek() == '/') {                                 is.get();                                 getden();                                 if (is.good())                                         fr = (n < 0) ? n - fr : n + fr;                         }                 } else {                         fr.num = n;                         getden();                 }         } else                 fr = n;         fr.reduce();         return is; } int main() {         fraction f1(2, 3);         fraction f2(3, 4);         fraction f;         while ((cout << "Enter a fraction: ") && !(cin >> f)) {                 cout << "Invalid fraction" << endl;                 cin.clear();                 cin.ignore(1000, '\n');         }         fraction f3 = f;         cout << f3 << endl;         cout << f1 + f2 << endl;         cout << -f1 - f2 << endl;         cout << f1 * f2 << endl;         cout << -f1 + f2 << endl;         cout << f1 / f2 << endl;         cout << f2 / f1 << endl;         cout << 6 - f3 + f1 + f2 << endl;         cout << (f1 > f2) << endl;         cout << (f2 > f1) << endl; }```
Couple of points of potential interest.

num determines the sign of the fraction with den always positive. den could be an unsigned int but signed int / unsigned int gives a type of unsigned int!

All the checking of a fraction for validity etc is done in reduce(). If a den of 0 is found, then an exception is thrown and the fraction is set to valid 0. Thus when using fraction constructor or / operator then a try block should be used.

The c++17 gcd() and lcm() are used.

operators +, -, *, / are implemented in terms of +=, -=, *= /=. Division by a fraction is the same as multiplication of its inverse. Also subtraction is the addition of its negated value. So only += and *= need to be implemented in terms of num and den. The others are done as part of these.

With the comparison functions, only == and > (or could be <) are implemented in terms of num and dev. The others are defined in terms of these.

With binary functions, a lhs fraction can be implemented either as a member or non-member function. Here I've done them as member functions as == and > require access to the member variables - otherwise they would need to be as friend non-members. If the lhs is a number with a rhs fraction then these must be implemented as non-member variables. However these don't need to be friend functions as they can be implemented using member functions. The order of + and * doesn't matter and 6 - f is the same as (-)f + 6.

The operator << is actually quite easy to implement. Improper fraction needs to be taken care of, but a comparison of num and den can determine if this is required. Note that the instructions require -6/4 to be displayed as -1+1/2. I take issue with this as IMO it should be shown as -1-1/2 which would be mathematically correct. However the code conforms to the instructions.

The hardest part is the stream extraction (>>). The instructions say little about handling errors - other than extraction stops after a valid input has been found. So in keeping with what happens when a problem occurs when reading other types, if an invalid fraction is found during extraction then the stream is set to the fail state. As obtaining den is required to be done in 2 places then this is done as a lambda function (local functions are allowed in c++ but lambda functions can be used instead in many cases).

If anyone has a question, just ask it here.

Also note that as this was written during a couple of coffee breaks, extensive testing hasn't been done! if any issues are found, please post it here.

Cheers!
• 02-23-2018, 02:38 PM
Ann8284
C++ is a great program. I think that its function is well-designed and organized. I wish you every success.
• 02-24-2018, 09:11 AM
2kaud
Quote:

Originally Posted by Ann8284
C++ is a great program. I think that its function is well-designed and organized. I wish you every success.

Thanks!
• 07-19-2019, 06:22 AM