: Complete the MixedNumber class by the completing the add member function (adds
ID: 3691254 • Letter: #
Question
: Complete the MixedNumber class by the completing the add member function (adds two mixed numbers), and providing and additional member function which will return true if the MixedNumber object is a whole number with a fractional part equal to zero.
#ifndef MIXEDNUMBER_H_
#define MIXEDNUMBER_H_
#include "GCD.h"
#include <cassert>
#include <iostream>
using namespace std;
class MixedNumber
{
public:
// toss in for lab1
MixedNumber()
: whole(0), numerator(0), denominator(1)
{}
// LAB1
MixedNumber(int a, int b, int c)
{
assert(a >= 0 && b >= 0 && c > 0);
whole = a;
numerator = b;
denominator = c;
simplify();
}
MixedNumber(const MixedNumber& other)
: whole(other.whole), numerator(other.numerator), denominator(other.denominator)
{}
// LAB1
void print() const
{
if (numerator == 0)
{
cout << whole;
}
else
cout << whole << "_" << numerator << "/" << denominator;
}
// example: mix1.add(mix2) adds mixed number mix2 to
// mixed number mix1 and returns a new mixed number
// which is the sum. use lcm function from GCD.h to
// compute least common multiple as needed; handle
// improper fractions when they occur;
MixedNumber add(const MixedNumber& other) const
{
// HW1 CODE HERE:
}
// true if the mixed number is actually just a whole
// number;
bool is_whole() const
{
// HW1 CODE HERE:
}
private:
// LAB1
void simplify()
{
if (numerator % denominator == 0)
{
whole += numerator / denominator;
numerator = 0;
denominator = 1; // not 0 !!!
}
else if (numerator < denominator)
{
int commondiv = gcd(numerator, denominator);
numerator = numerator / commondiv;
denominator = denominator / commondiv;
}
else
{
// numer > denom
whole = whole + numerator / denominator;
numerator = numerator % denominator;
assert(numerator <= denominator);
simplify();
}
return;
}
int whole;
int numerator;
int denominator;
};
#endif
Explanation / Answer
bool is_whole() const
{
// HW1 CODE HERE:
if(numerator==0 && denominator==0)
return true;
else
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.