need the following output: here is Fraction.h #ifndef SICT_Fraction_H__ #define
ID: 3797715 • Letter: N
Question
need the following output:
here is Fraction.h
#ifndef SICT_Fraction_H__
#define SICT_Fraction_H__
#include <iostream>
using namespace std;
namespace sict {
class Fraction {
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common divisor
Fraction(); // default constructor
Fraction(int n, int d = 1); // construct n/d as a Fraction number
void display() const;
bool isEmpty() const;
// member operator functions
// TODO: write the prototype of member operator+= function HERE
Fraction& operator+=(Fraction f2);
// TODO: write the prototype of member operator+ function HERE
Fraction operator+(Fraction f2);
// TODO: write the prototype of member operator* function HERE
Fraction operator*(Fraction f2);
};
};
#endif
here is Fraction.cpp
#include "Fraction.h"
using namespace std;
namespace sict {
Fraction::Fraction() {
denom = -1; // safe empty state
}
Fraction::Fraction(int n, int d) // n: numerator, d: denominator
{
if (n >= 0 && d > 0) {
num = n;
denom = d;
reduce();
} else
denom = -1; // set to safe empty state
}
int Fraction::gcd() // returns the greatest common divisor of num and denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x = mn; x > 0; x--) // find the greatest common divisor
if (mx % x == 0 && mn % x == 0) return x;
return 1;
}
void Fraction::reduce() // simplify the Fraction number
{
int tmp = gcd();
num /= tmp;
denom /= tmp;
}
int Fraction::max() { return (num >= denom) ? num : denom; }
int Fraction::min() { return (num >= denom) ? denom : num; }
// in_lab
// TODO: write the implementation of display function HERE
void Fraction::display() const {
if (isEmpty()) {
cout << "Invalid Fraction Object!";
} else if (denom == 1) {
cout << num;
} else {
cout << num << "/" << denom;
}
}
// TODO: write the implementation of isEmpty function HERE
bool Fraction::isEmpty() const {
return denom == -1;
}
// TODO: write the implementation of member operator+= function
Fraction &Fraction::operator+=(Fraction f2) {
if (isEmpty() || f2.isEmpty()) {
denom = -1; // safe empty state
} else {
num = (num * f2.denom) + (denom * f2.num);
denom = (denom * f2.denom);
}
return *this; // returns reference to current object
}
// TODO: write the implementation of member operator+ function HERE
Fraction Fraction::operator+(Fraction f2) {
Fraction fraction;
if (isEmpty() || f2.isEmpty()) {
fraction.denom = -1; // safe empty state
} else {
fraction.num = (num * f2.denom) + (denom * f2.num);
fraction.denom = (denom * f2.denom);
}
return fraction;
}
// TODO: write the implementation of member operator* function HERE
Fraction Fraction::operator*(Fraction f2) {
Fraction fraction;
if (isEmpty() || f2.isEmpty()) {
fraction.denom = -1;
} else {
fraction.num = (num * f2.num);
fraction.denom = (denom * f2.denom);
}
return fraction;
}
}
main:
#include <iostream>
#include "Fraction.h"
using namespace sict;
using namespace std;
int main(){
cout << "------------------------------" << endl;
cout << "Fraction Class Operators Test:" << endl;
cout << "------------------------------" << endl;
Fraction A;
cout << "Fraction A; // ";
cout << "A = ";
A.display();
cout << endl;
Fraction B(1, 3);
cout << "Fraction B(1, 3); // ";
cout << "B = ";
B.display();
cout << endl;
Fraction C(-5, 15);
cout << "Fraction C(-5, 15); //";
cout << " C = " ;
C.display();
cout << endl;
Fraction D(2, 4);
cout << "Fraction D(2, 4); //";
cout << " D = ";
D.display();
cout << endl;
Fraction E(8, 4);
cout << "Fraction E(8, 4); //";
cout << " E = " ;
E.display();
cout << endl;
cout << endl;
cout << "(B* Fraction(6) == E) equals ";
cout << (B*Fraction(6) == E);
cout << endl;
cout << "(A == C) equals ";
cout << (A == C);
cout << endl;
cout << "(double) B equals ";
cout.precision(3);
cout << (double) B;
cout << endl;
cout << "(int) B equals ";
cout << (int) B;
cout << endl;
cout << "S := ";
Fraction s = 0;
for(Fraction r = Fraction(1,2) ; r != Fraction(3,1);r +=Fraction(1,4)){
r.display();
cout << ", " ;
s += r;
}
cout << endl << "The sum of elements in S equals " ;
s.display();
cout << endl ;
return 0;
}
Output Example:
(Your output should exactly match the following)
------------------------------
Fraction Class Operators Test:
------------------------------
Fraction A; // A = Invalid Fraction Object!
Fraction B(1, 3); // B = 1/3
Fraction C(-5, 15); // C = Invalid Fraction Object!
Fraction D(2, 4); // D = 1/2
Fraction E(8, 4); // E = 2
(B* Fraction(6) == E) equals 1
(A == C) equals 0
(double) B equals 0.333
(int) B equals 0
S := 1/2, 3/4, 1, 5/4, 3/2, 7/4, 2, 9/4, 5/2, 11/4,
The sum of elements in S equals 65/4
Explanation / Answer
need the following output:
here is Fraction.h
#ifndef SICT_Fraction_H__
#define SICT_Fraction_H__
#include <iostream>
using namespace std;
namespace sict {
class Fraction {
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common divisor
Fraction(); // default constructor
Fraction(int n, int d = 1); // construct n/d as a Fraction number
void display() const;
bool isEmpty() const;
// member operator functions
// TODO: write the prototype of member operator+= function HERE
Fraction& operator+=(Fraction f2);
bool operator<(Fraction f2 );
bool operator<=(Fraction f2);
// TODO: write the prototype of member operator+ function HERE
Fraction operator+(Fraction f2);
bool operator > (Fraction f2);
bool operator >= (Fraction f2);
// TODO: write the prototype of member operator* function HERE
Fraction operator*(Fraction f2);
bool operator == (Fraction f2);
bool operator != (Fraction f2);
};
};
#endif
here is Fraction.cpp
#include "Fraction.h"
using namespace std;
namespace sict {
Fraction::Fraction() {
denom = -1; // safe empty state
}
Fraction::Fraction(int n, int d) // n: numerator, d: denominator
{
if (n >= 0 && d > 0) {
num = n;
denom = d;
reduce();
} else
denom = -1; // set to safe empty state
}
int Fraction::gcd() // returns the greatest common divisor of num and denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x = mn; x > 0; x--) // find the greatest common divisor
if (mx % x == 0 && mn % x == 0) return x;
return 1;
}
void Fraction::reduce() // simplify the Fraction number
{
int tmp = gcd();
num /= tmp;
denom /= tmp;
}
int Fraction::max() { return (num >= denom) ? num : denom; }
int Fraction::min() { return (num >= denom) ? denom : num; }
// in_lab
// TODO: write the implementation of display function HERE
void Fraction::display() const {
if (isEmpty()) {
cout << "Invalid Fraction Object!";
} else if (denom == 1) {
cout << num;
} else {
cout << num << "/" << denom;
}
}
// TODO: write the implementation of isEmpty function HERE
bool Fraction::isEmpty() const {
if (num.empty)
return denom == -1;
}
// TODO: write the implementation of member operator+= function
Fraction &Fraction::operator+=(Fraction f2) {
if (isEmpty() || f2.isEmpty()) {
denom = -1; // safe empty state
} else {
num = (num * f2.denom) + (denom * f2.num);
denom = (denom * f2.denom);
}
return *this; // returns reference to current object
}
// TODO: write the implementation of member operator+ function HERE
Fraction Fraction::operator+(Fraction f2) {
Fraction fraction;
if (isEmpty() || f2.isEmpty()) {
fraction.denom = -1; // safe empty state
} else {
fraction.num = (num * f2.denom) + (denom * f2.num);
fraction.denom = (denom * f2.denom);
}
return fraction;
}
// TODO: write the implementation of member operator* function HERE
Fraction Fraction::operator*(Fraction f2) {
Fraction fraction;
if (isEmpty() || f2.isEmpty()) {
fraction.denom = -1;
} else {
fraction.num = (num * f2.num);
fraction.denom = (denom * f2.denom);
}
return fraction;
}
}
main:
#include <iostream>
#include "Fraction.h"
using namespace sict;
using namespace std;
int main(){
cout << "------------------------------" << endl;
cout << "Fraction Class Operators Test:" << endl;
cout << "------------------------------" << endl;
Fraction A;
cout << "Fraction A; // ";
cout << "A = ";
A.display();
cout << endl;
Fraction B(1, 3);
cout << "Fraction B(1, 3); // ";
cout << "B = ";
B.display();
cout << endl;
Fraction C(-5, 15);
cout << "Fraction C(-5, 15); //";
cout << " C = " ;
C.display();
cout << endl;
Fraction D(2, 4);
cout << "Fraction D(2, 4); //";
cout << " D = ";
D.display();
cout << endl;
Fraction E(8, 4);
cout << "Fraction E(8, 4); //";
cout << " E = " ;
E.display();
cout << endl;
cout << endl;
cout << "(B* Fraction(6) == E) equals ";
cout << (B*Fraction(6) == E);
cout << endl;
cout << "(A == C) equals ";
cout << (A == C);
cout << endl;
cout << "(double) B equals ";
cout.precision(3);
cout << (double) B;
cout << endl;
cout << "(int) B equals ";
cout << (int) B;
cout << endl;
cout << "S := ";
Fraction s = 0;
for(Fraction r = Fraction(1,2) ; r != Fraction(3,1);r +=Fraction(1,4)){
r.display();
cout << ", " ;
s += r;
}
cout << endl << "The sum of elements in S equals " ;
s.display();
cout << endl ;
return 0;
}
Output Example:
(Your output should exactly match the following)
------------------------------
Fraction Class Operators Test:
------------------------------
Fraction A; // A = Invalid Fraction Object!
Fraction B(1, 3); // B = 1/3
Fraction C(-5, 15); // C = Invalid Fraction Object!
Fraction D(2, 4); // D = 1/2
Fraction E(8, 4); // E = 2
(B* Fraction(6) == E) equals 1
(A == C) equals 0
(double) B equals 0.333
(int) B equals 0
S := 1/2, 3/4, 1, 5/4, 3/2, 7/4, 2, 9/4, 5/2, 11/4,
The sum of elements in S equals 65/4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.