I am so stuck. Any assistance will result in my undying gratitude. Unfortunately, I don't have much else to offer.
Here are the instructions for my assignment:
Imagine that the company you work for is going to create a lot of tutorials on Fractions. You are to create a robust Fraction class that will have all of the following (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):
Private integers numerator and denominator ;
All public getter and setter functions for the numerator and denominator;
Safeguard that the denominator will NEVER become 0!
a default constructor with no arguments;
a constructor that accepts both the numerator and denominator;
a toDecimal method that returns the decimal value of the fraction, example: 1/2 will be 0.5;
a toString method that will return the fraction as a string, , example: 1/2 will be "1/2";
a reduce method that will change the numerator and denominator by finding a common denominator and reducing the fraction. Example 3/12 becomes 1/4;
Expand your Fraction class to include all of the following (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):
Overload ==, <, <=, >, >= operators (use the decimal values to compare);
Overload = operator (assigns the numerator and denominator to the fraction);
Overload ++ so that 1/3 becomes 2/3 for example.
Overload -- so that 2/3 becomes 1/3 for eaxample.
It is OK if the numerator is negative.
Here's the code I have so far. Everything is working except for the overloaded -- and ++ operations. The functions work if I have the functions print out information. The information just isn't getting back to the main function.
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
|
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction(){};
Fraction(int num, int denom)
{
numerator = num;
if (denom==0)
exit(0);
else if (denom!=0)
denominator = denom;
}
void setNumerator(double num)
{
numerator=num;
}
void setDenominator(double denom)
{
if (denom==0)
exit(0);
else if (denom != 0)
denominator=denom;
}
double toDecimal(double num, double denom)
{
return num/denom;
}
string toString(double number)
{
string String=static_cast<ostringstream*>(&(ostringstream() << number) )->str();
return String;
}
double reduce(double num, double denom)
{
if(denom==0)
return num;
else
return reduce(denom, fmod(num, denom));
}
void operator ++(int)
{
numerator++;
denominator++;
}
void operator --(int)
{
if(denominator-1 !=0)
{
denominator--;
numerator--;
}
}
void operator=(const Fraction ©from)
{
numerator=copyfrom.numerator;
denominator=copyfrom.denominator;
}
bool operator > (const Fraction &right)
{
bool status;
if (numerator > right.numerator && denominator > right.denominator)
status=true;
else
status=false;
return status;
}
bool operator < (const Fraction &right)
{
bool status;
if (numerator<right.numerator && denominator < right.denominator)
status=true;
else
status=false;
return status;
}
bool operator == (const Fraction &right)
{
bool status;
if (numerator==right.numerator && denominator==right.denominator)
status=true;
else
status=false;
return status;
}
bool operator >= (const Fraction &right)
{
bool status;
if (numerator>right.numerator && denominator>right.denominator)
status=true;
else if (numerator==right.numerator && denominator==right.denominator)
status=true;
else
status=false;
return status;
}
bool operator <= (const Fraction &right)
{
bool status;
if (numerator<right.numerator && denominator<right.denominator)
status=true;
else if (numerator==right.numerator && denominator==right.denominator)
status=true;
else
status=false;
return status;
}
};
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);
Fraction(num, denom);
cout << "Please enter the second numerator: ";
cin >> num2;
frac2.setNumerator(num2);
cout << "Please enter the second denominator: ";
cin >> denom2;
frac2.setDenominator(denom2);
Fraction(num2, denom2);
cout << "Your fraction is: " << num << "/" << denom << "." << endl;
cout << "As a decimal your fraction is: " << frac.toDecimal(num, denom) << "." << endl;
cout << "Your fraction as a string is: " << frac.toString(num) << "/" << frac.toString(denom) << "." << endl;
int gcd = frac.reduce(num, denom);
cout << "Your fraction reduced is: " << num/gcd << "/" << denom/gcd << "." << endl;
cout << "Your second fraction is: " << num2 << "/" << denom2 << "." << endl;
cout << "As a decimal your second fraction is: " << frac2.toDecimal(num2, denom2) << "." << endl;
cout << "Your second fraction as a string is: " << frac2.toString(num2) << "/" << frac2.toString(denom2) << "." << endl;
int gcd2=frac2.reduce(num2, denom2);
cout << "Your second fraction reduced is: "<< num2/gcd2 << "/" << denom2/gcd2 << "." << 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: " << num << "/" << denom << endl;
cout << "Your second fraction: " << num2 << "/" << denom2 << endl;
return 0;
}
|
Thank you in advance for any help. I have spent a week beating my head against the well and getting no where.