Question: include using namespace std; #include Fraction.h // for Fraction declarations int main() { // Try all three possible fraction constructors // and the input/output routines.
include using namespace std; #include \"Fraction.h\" // for Fraction declarations
int main() { // Try all three possible fraction constructors // and the input/output routines. Fraction f1(3, 2), f2(4), f3, f4; // four test fractions
// Test constructor with 2 parameters. cout f1.Show(); cout
//test constructor with 1 parameter cout f2.Show(); cout //test default constructor cout f3.Show(); cout
//test add function Fraction f5 = f1.Add(&f2); cout f5.Show(); cout
// test the friend function operator >> cout cin >> f3; cout f3.Show(); cout
// Find the floating-point value of f1 cout f1.Show(); cout
// Now try the overloaded operator +. /*f4 = f1 + f3; cout cout cout cout
// Test the ++ operator f3.Show(); cout ++f3; f3.Show(); cout //Test the * operator /*Fraction f6 = f1 * f1; cout cout cout
//Test the == operator //if (f1 == f2)//test when they are not equal // cout //else // cout // //if (f1 == f1)//test when they are equal // cout //else // cout
cout char key; cin >> key;
return 0; }
//************************************************************ Fraction.cpp // Purpose: // This file contains the class definitions for a class that does simple // fraction manipulation. //************************************************************************* #include using namespace std; #include \"Fraction.h\"
// A Fraction constructor which allows the numerator and denominator // to be specified. Fraction::Fraction(int n, int d) { numerator = n; if (d != 0) { denominator = d; } else { denominator = 1; } }
// Get a fraction from standard input, in the form \"numerator/denominator.\" istream& operator>>(istream& in, Fraction& frac) { char divSign; // used to consume the '/' character during input in >> frac.numerator >> divSign >> frac.denominator; return in; }
// Display a fraction, in the form \"numerator/denominator.\" void Fraction::Show() { Reduce(); if (denominator == 1) { cout } else { cout } }
// Evaluate returns the decimal equivalent of the fraction double Fraction::Evaluate() { double n = numerator; // convert numerator to float double d = denominator; // convert denominator to float return (n / d); // compute and return float representation }
// This function reduces a fraction to its lowest terms. void Fraction::Reduce() { int dividend; // the number to be divided int divisor; // the number to divide into the dividend int remainder; if (numerator == 0) { numerator = 1; return; //can't reduce 0 } else if (numerator > denominator) { dividend = numerator; divisor = denominator; } else { dividend = denominator; divisor = numerator; }
remainder = dividend % divisor;
while (remainder != 0) { dividend = divisor; divisor = remainder; remainder = dividend % divisor; } //reduce by dividing numerator and denominator by the LCD which is currently in the divisor if (divisor != 0) { numerator /= divisor; denominator /= divisor; } }
Fraction Fraction::Add(const Fraction * f2) { Fraction r; // the return value of f1 + f2 r.numerator = (this->numerator * f2->denominator) + (f2->numerator * this->denominator);// compute numerator r.denominator = this->denominator * f2->denominator;// compute denominator r.Reduce(); return r; }
// Overload of operator ++ for incrementing Fraction Fraction::operator++ () { numerator = numerator + denominator;// compute numerator Reduce(); return *this; // return the result }
//************************************************************ Fraction.h // Purpose: // This file contains the class declaraions for a class that does simple // fraction manipulation. //************************************************************************* #include using namespace std; #ifndef FRACTION_H #define FRACTION_H class Fraction { // operator overload, so we can use standard C++ notation // Get a fraction from keyboard. friend istream& operator >> (istream& in, Fraction& frac); // print a fraction to the console
public: // The counstructor // Set numerator = n, denominator = d. // if no second argument (whole number), default is 1 //default constructor sets fraction to 0 Fraction(int n = 0, int d = 1);
// copy constructor // destructor
// The print function void Show(); // Display a fraction on screen
// Addition of fractions Fraction Add(const Fraction * f2);
// Assignment operator
// Overloading operators for addition, multiplication and incrementing Fraction operator++ (); // prefix only
//Overloading operators for == comparison
double Evaluate(); // Return the decimal value of a fraction void Reduce(); // Reduce the fraction to lowest terms.
private: int numerator; // top part int denominator; // bottom part }; #endif ----------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
