Need Help in development of a fraction class

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.

That leaves the following for week 2: shorthand arithmetic operators, increment/decrement operators, finishing up the insertion operator, and completing Phase 2 and Phase 3.
• Phase 2 : The reduce() function.
• Phase 3 : The extraction operator.

Last edited on
Additional Requirements and Hints:

• 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.
Here is the code so far:
Fraction Header

#ifndef FRACTION_H
#define FRACTION_H

#include <iostream> // for ostream, istream
using namespace std;
class fraction
{
public:
int numerator, denominator;
fraction(); // Set numerator = 0, denominator = 1.

fraction(int n, int d=1); // constructor with parameters

// standard input/output routines
void read(); // input a fraction from keyboard.
void print(); // Display a fraction on screen

// accessors
int GetNumerator();
int GetDenominator();

private:
int n; // top part (any integer)
int d; // denom must be non-zero
};

#endif // FRACTION_H

Fraction cpp

#include "fraction.h"
#include <iostream>

using namespace std;

// member functions
fraction::fraction()
{

// Default constructor. Initializes fraction to 0/1
{
numerator = 0;
denominator = 1;
}
fraction::fraction(int n, int d)
// initializes fraction to n/d
// validation of input added in
{
if (SetValue(n,d) == false)
SetValue(0,1);
}
void fraction::read()
// Get a fraction from standard input, in the form "numerator/denominator."
// Input validation added in
{
char divSign; // used to consume the '/' character during input
do
{
cin >> numerator >> divSign >> denominator;
if (denominator == 0)
cout << "Bad Fraction. Try again: ";
} while (denominator == 0);
}
void fraction::print()
// Display a fraction, in the form "numerator/denominator."
{
cout << numerator << '/' << denominator;
}
int fraction::GetNumerator()
{
return numerator;
}
int Fraction::GetDenominator()
{
return denominator;
}
Here is the client program;
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
int main()
{
BasicTest();
RelationTest();
BinaryMathTest();
MathAssignTest();
}
void BasicTest()
{
cout << "\n----- Testing basic fraction creation & printing\n";
cout << "(fractions should be in reduced form, and as mixed numbers.)\n";

const fraction fr[] = {fraction(4, 8), fraction(-15,21),
fraction(10), fraction(12, -3),
fraction(), fraction(28, 6), fraction(0, 12)};

for (int i = 0; i < 7; i++){
cout << "fraction [" << i <<"] = " << fr[i] << endl;
}



// Comment the rest of this function out until you finish writing the extraction operator.
cout << "\n----- Now reading fractions from file\n";
ifstream in("fraction.data");
assert(in);
while (!eof(in)) {
fraction f;
if (in.peek() == '#') {
in.ignore(128, '\n'); //skip this line, it's a comment
} else {
in >> f;
cout << "Read fraction = " << f << endl;
}
}
}
bool eof(ifstream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}

void RelationTest()
{
cout << "\n----- Testing relational operators between fractions\n";
const fraction fr[] = {fraction(3, 6), fraction(1,2), fraction(-15,30),
fraction(1,10), fraction(0,1), fraction(0,2)};
for (int i = 0; i < 5; i++) {
cout << "Comparing " << fr[i] << " to " << fr[i+1] << endl;
cout << "\tIs left < right? " << (fr[i] < fr[i+1]) << endl;
cout << "\tIs left <= right? " << (fr[i] <= fr[i+1]) << endl;
cout << "\tIs left > right? " << (fr[i] > fr[i+1]) << endl;
cout << "\tIs left >= right? " << (fr[i] >= fr[i+1]) << endl;
cout << "\tDoes left == right? " << (fr[i] == fr[i+1]) << endl;
cout << "\tDoes left != right ? " << (fr[i] != fr[i+1]) << endl;
}

cout << "\n----- Testing relations between fractions and integers\n";
fraction f(-3,6);
int num = 2;
cout << "Comparing " << f << " to " << num << endl;
cout << "\tIs left < right? " << (f < num) << endl;
cout << "\tIs left <= right? " << (f <= num) << endl;
cout << "\tIs left > right? " << (f > num) << endl;
cout << "\tIs left >= right? " << (f >= num) << endl;
cout << "\tDoes left == right? " << (f == num) << endl;
cout << "\tDoes left != right ? " << (f != num) << endl;

fraction g(1,4);
num = -3;
cout << "Comparing " << num << " to " << g << endl;
cout << "\tIs left < right? " << (num < g) << endl;
cout << "\tIs left <= right? " << (num <= g) << endl;
cout << "\tIs left > right? " << (num > g) << endl;
cout << "\tIs left >= right? " << (num >= g) << endl;
cout << "\tDoes left == right? " << (num == g) << endl;
cout << "\tDoes left != right ? " << (num != g) << endl;
}
void BinaryMathTest()
{
cout << "\n----- Testing binary arithmetic between fractions\n";

const fraction fr[] = {fraction(1, 6), fraction(1,3),
fraction(-2,3), fraction(5), fraction(-4,3)};
for (int i = 0; i < 4; i++) {
cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1] << endl;
cout << fr[i] << " - " << fr[i+1] << " = " << fr[i] - fr[i+1] << endl;
cout << fr[i] << " * " << fr[i+1] << " = " << fr[i] * fr[i+1] << endl;
cout << fr[i] << " / " << fr[i+1] << " = " << fr[i] / fr[i+1] << endl;
}
cout << "\n----- Testing arithmetic between fractions and integers\n";
fraction f(-1, 2);
int num = 4;
cout << f << " + " << num << " = " << f + num << endl;
cout << f << " - " << num << " = " << f - num << endl;
cout << f << " * " << num << " = " << f * num << endl;
cout << f << " / " << num << " = " << f / num << endl;

fraction g(-1, 2);
num = 3;
cout << num << " + " << g << " = " << num + g << endl;
cout << num << " - " << g << " = " << num - g << endl;
cout << num << " * " << g << " = " << num * g << endl;
cout << num << " / " << g << " = " << num / g << endl;
}
void MathAssignTest()
{
cout << "\n----- Testing shorthand arithmetic assignment on fractions\n";

fraction fr[] = {fraction(1, 6), fraction(4),
fraction(-1,2), fraction(5)};
for (int i = 0; i < 3; i++) {
cout << fr[i] << " += " << fr[i+1] << " = ";
cout << (fr[i] += fr[i+1]) << endl;
cout << fr[i] << " -= " << fr[i+1] << " = ";
cout << (fr[i] -= fr[i+1]) << endl;
cout << fr[i] << " *= " << fr[i+1] << " = ";
cout << (fr[i] *= fr[i+1]) << endl;
cout << fr[i] << " /= " << fr[i+1] << " = ";
cout << (fr[i] /= fr[i+1]) << endl;
}

cout << "\n----- Testing shorthand arithmetic assignment using integers\n";
fraction f(-1, 3);
int num = 3;
cout << f << " += " << num << " = ";
cout << (f += num) << endl;
cout << f << " -= " << num << " = ";
cout << (f -= num) << endl;
cout << f << " *= " << num << " = ";
cout << (f *= num) << endl;
cout << f << " /= " << num << " = ";
cout << (f /= num) << endl;
cout << "\n----- Testing increment/decrement prefix and postfix\n";
fraction g(-1, 3);
cout << "Now g = " << g << endl;
cout << "g++ = " << g++ << endl;
cout << "Now g = " << g << endl;
cout << "++g = " << ++g << endl;
cout << "Now g = " << g << endl;
cout << "g-- = " << g-- << endl;
cout << "Now g = " << g << endl;
cout << "--g = " << --g << endl;
cout << "Now g = " << g << endl;
}
1) Please use code tags for your code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Simply dumping a homework problem on us and expecting us to complete it is not going to happen. If you have a specific problem with what you're trying to do, then tell us what that problem is, and we'll try and help.
Topic archived. No new replies allowed.