Question

In: Computer Science

*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp********* *****PLEASE MAKE SURE...

*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp*********

*****PLEASE MAKE SURE THAT mixed.h AND mixed.cpp WORK WITH THE DRIVER FILE PROVIDED*******************

Objective: Upon completion of this program, you should gain experience with overloading basic operators for use with a C++ class.

The code for this assignment should be portable -- make sure you test with g++ on linprog.cs.fsu.edu before you submit.

Task

Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files "mixed.h" and "mixed.cpp".

Details and Requirements

  1. Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 -- "three and one-half"). The Mixed class must allow for both positive and negative mixed number values. A zero in the denominator of the fraction part constitutes an illegal number and should not be allowed. You should create appropriate member data in your class. All member data must be private.
  2. There should be two constructors. One constructor should take in three parameters, representing the integer part, the numerator, and the denominator (in that order), used to initialize the object. If the mixed number is to be a negative number, the negative should be passed on the first non-zero parameter, but on no others. If the data passed in is invalid (negatives not fitting the rule, or 0 denominator), then simply set the object to represent the value 0. Examples of declarations of objects:
     Mixed m1(3, 4, 5);    // sets object to 3 4/5 
     Mixed m2(-4, 1, 2);   // sets object to -4 1/2 
     Mixed m3(0, -3, 5);   // sets object to -3/5 (integer part is 0). 
     Mixed m4(-1, -2, 4);  // bad parameter combination.  Set object to 0. 
    

    The other constructor should expect a single int parameter with a default value of 0 (so that it also acts as a default constructor). This constructor allows an integer to be passed in and represented as a Mixed object. This means that there is no fractional part. Example declarations:

     Mixed m5(4);  // sets object to 4 (i.e. 4 and no fractional part). 
     Mixed m6;     // sets object to 0 (default)
    
    Note that this last constructor will act as a "conversion constructor", allowing automatic type conversions from type int to type Mixed.
  3. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a double, the others don't return anything. These functions have no parameters. The names must match the ones here exactly. They should do the following:
    • The Evaluate function should return the decimal equivalent of the mixed number.
    • The Simplify function should simplify the mixed number representation to lowest terms. This means that the fraction part should be reduced to lowest terms, and the fraction part should not be an improper fraction (i.e. disregarding any negative signs, the numerator is smaller than the denominator).
    • The ToFraction function should convert the mixed number into fraction form. (This means that the integer part is zero, and the fraction portion may be an improper fraction).
  4. Create an overload of the extraction operator >> for reading mixed numbers from an input stream. The input format for a Mixed number object will be:
     integer numerator/denominator 
    

    i.e. the integer part, a space, and the fraction part (in numerator/denominator form), where the integer, numerator, and denominator parts are all of type int. You may assume that this will always be the format that is entered (i.e. your function does not have to handle entry of incorrect types that would violate this format). However, this function should check the values that come in. In the case of an incorrect entry, just set the Mixed object to represent the number 0, as a default. An incorrect entry occurs if a denominator value of 0 is entered, or if an improper placement of a negative sign is used. Valid entry of a negative number should follow this rule -- if the integer part is non-zero, the negative sign is entered on the integer part; if the integer part is 0, the negative sign is entered on the numerator part (and therefore the negative sign should never be in the denominator). Examples:

     Valid inputs:     2 7/3 , -5 2/7  , 4 0/7  , 0 2/5  , 0 -8/3 
     Invalid inputs:   2 4/0 , -2 -4/5 , 3 -6/3 , 0 2/-3 
    
  5. Create an overload of the insertion operator << for output of Mixed numbers. This should output the mixed number in the same format as above, with the following exceptions: If the object represents a 0, then just display a 0. Otherwise: If the integer part is 0, do not display it. If the fraction part equals 0, do not display it. For negative numbers, the minus sign is always displayed to the left.
     Examples:   0  ,  2  ,  -5  ,  3/4  ,  -6/7  ,  -2 4/5  ,  7 2/3 
    
  6. Create overloads for all 6 of the comparison operators ( < , > , <= , >= , == , != ). Each of these operations should test two objects of type Mixed and return an indication of true or false. You are testing the Mixed numbers for order and/or equality based on the usual meaning of order and equality for numbers. (These functions should not do comparisons by converting the Mixed numbers to decimals -- this could produce round-off errors and may not be completely accurate).
  7. Create operator overloads for the 4 standard arithmetic operations ( + , - , * , / ) , to perform addition, subtraction, multiplication, and division of two mixed numbers. Each of these operators will perform its task on two Mixed objects as operands and will return a Mixed object as a result - using the usual meaning of arithmetic operations on rational numbers. Also, each of these operators should return their result in simplified form. (e.g. return 3 2/3 instead of 3 10/15, for example).
    • In the division operator, if the second operand is 0, this would yield an invalid result. Since we have to return something from the operator, return 0 as a default (even though there is no valid answer in this case). Example:
      Mixed m(1, 2, 3);               // value is 1 2/3
      Mixed z;                        // value is 0
      Mixed r = m / z;                // r is 0 (even though this is not good math)
      
  8. Create overloads for the increment and decrement operators (++ and --). You need to handle both the pre- and post- forms (pre-increment, post-increment, pre-decrement, post-decrement). These operators should have their usual meaning -- increment will add 1 to the Mixed value, decrement will subtract 1. Since this operation involves arithmetic, leave incremented (or decremented) object in simplified form (to be consistent with other arithmetic operations). Example:
      Mixed m1(1, 2, 3);            //  1 2/3
      Mixed m2(2, 1, 2);            //  2 1/2
      cout << m1++;                   //  prints 1 2/3, m1 is now 2 2/3
      cout << ++m1;                   //  prints 3 2/3, m1 is now 3 2/3
      cout << m2--;                   //  prints 2 1/2, m2 is now 1 1/2
      cout << --m2;                   //  prints 1/2  , m2 is now 0 1/2
    
  9. General Requirements
    • As usual, no global variables
    • All member data of the Mixed class must be private
    • Use the const qualifier whenever appropriate
    • The only libraries that may be used in the class files are iostream and iomanip
    • Do not use langauge or library features that are C++11-only
    • Since the only output involved with your class will be in the << overload (and commands to invoke it will come from some main program or other module), your output should match mine exactly when running test programs.

*************DRIVER FILE TO TEST PROGRAM******************

//**Bigginning of main.cpp**//

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

using namespace std;

int main()
{

  // demonstrate behavior of the two constructors and the << overload

  Mixed x(3,20,6), y(-4,9,2), m1(0,-35,10), m2(-1,-2,4), m3(4), m4(-11), m5;
  char answer;
  cout << "Initial values: \nx = " << x << "\ny = " << y
       << "\nm1 = " << m1 << "\nm2 = " << m2 << "\nm3 = " << m3 
       << "\nm4 = " << m4 << "\nm5 = " << m5 << "\n\n";

  // Trying Simplify
  x.Simplify();
  m1.Simplify();
  cout << "x simplified: " << x << " and m1 simplified " << m1 << "\n\n";

  // Trying ToFraction
  x.ToFraction();
  y.ToFraction();
  m1.ToFraction();
  cout << "Values as fractions: \nx = " << x << "\ny = " << y
       << "\nm1 = " << m1 << "\n\n";
 
  // demonstrate >> overload

  cout << "Enter first number: ";
  cin >> x;
  cout << "Enter second number: ";
  cin >> y;

  cout << "You entered:\n";
  cout << "  x = " << x << '\n';
  cout << "  y = " << y << '\n';

  // demonstrate comparison overloads
  if (x < y) cout << "(x < y) is TRUE\n";
  if (x > y) cout << "(x > y) is TRUE\n";
  if (x <= y)        cout << "(x <= y) is TRUE\n";
  if (x >= y)        cout << "(x >= y) is TRUE\n";
  if (x == y)   cout << "(x == y) is TRUE\n";
  if (x != y)   cout << "(x != y) is TRUE\n";

  // demonstrating Evaluate
  cout << "\nDecimal equivalent of " << x << " is " << x.Evaluate() << '\n';
  cout << "Decimal equivalent of " << y << " is " << y.Evaluate() << "\n\n";

  // demonstrating arithmetic overloads
  cout << "(x + y) = " << x + y << '\n';
  cout << "(x - y) = " << x - y << '\n';
  cout << "(x * y) = " << x * y << '\n';
  cout << "(x / y) = " << x / y << '\n';

  // demonstrating arithmetic that uses conversion constructor
  //  to convert the integer operand to a Mixed object
  cout << "(x + 10) = " << x + 10 << '\n';
  cout << "(x - 4) = " << x - 4 << '\n';
  cout << "(x * -13) = " << x * -13 << '\n';
  cout << "(x / 7) = " << x / 7 << '\n';

  return 0;
}

//**End of main.cpp **//

RESULT OF DRIVER FILE SHOULD PRODUCE THE FOLLOWING OUTPUT:

Initial values: 
x = 3 20/6
y = -4 9/2
m1 = -35/10
m2 = 0
m3 = 4
m4 = -11
m5 = 0

x simplified: 6 1/3 and m1 simplified -3 1/2

Values as fractions: 
x = 19/3
y = -17/2
m1 = -7/2

Enter first number: 1 2/3
Enter second number: 2 3/4
You entered:
  x = 1 2/3
  y = 2 3/4
(x < y) is TRUE
(x <= y) is TRUE
(x != y) is TRUE

Decimal equivalent of 1 2/3 is 1.66667
Decimal equivalent of 2 3/4 is 2.75

(x + y) = 4 5/12
(x - y) = -1 1/12
(x * y) = 4 7/12
(x / y) = 20/33
(x + 10) = 11 2/3
(x - 4) = -2 1/3
(x * -13) = -21 2/3
(x / 7) = 5/21

Solutions

Expert Solution

// mixed.h
#ifndef MIXED_H_INCLUDED
#define MIXED_H_INCLUDED

#include <iostream>
using namespace std;

class Mixed
{
private :
int whole, numerator, denominator;
void validate();
public:
Mixed(int w, int num, int den);
Mixed(int w=0);
double Evaluate() const;
void ToFraction();
void Simplify();
friend istream& operator>>(istream& in, Mixed& m);
friend ostream& operator<<(ostream& out, const Mixed& m);
bool operator<(const Mixed& m);
bool operator>(const Mixed& m);
bool operator<=(const Mixed& m);
bool operator>=(const Mixed& m);
bool operator==(const Mixed& m);
bool operator!=(const Mixed& m);

Mixed operator+(const Mixed& m) const;
Mixed operator-(const Mixed& m) const;
Mixed operator*(const Mixed& m) const;
Mixed operator/(const Mixed& m) const;
Mixed& operator++();
Mixed operator++(int x);
Mixed& operator--();
Mixed operator--(int x);
};

#endif // MIXED_H_INCLUDED

//end of mixed.h

// mixed.cpp
#include "mixed.h"
#include <cmath>

// constructors
Mixed::Mixed(int w, int num, int den)
{
whole = w;
numerator = num;
denominator = den;
validate();
}

// helper function to validate the fraction
void Mixed:: validate()
{
if(whole == 0)
{
if((numerator < 0 && denominator < 0) || (numerator > 0 && denominator < 0))
{
whole = 0;
numerator = 0;
denominator = 1;
}
}else
{
if(numerator < 0 || denominator < 0)
{
whole = 0;
numerator = 0;
denominator = 1;
}
}
}

Mixed::Mixed(int w)
{
whole = w;
numerator = 0;
denominator = 1;
}

// function to evaluate the fraction and return the value as double
double Mixed:: Evaluate() const
{
bool negative = false;
if(whole < 0 || numerator < 0)
negative = true;

double val = ((double)((abs(whole)*abs(denominator))+abs(numerator)))/abs(denominator);
if(negative)
return -val;
return val;
}

// function to convert mixed to improper fraction
void Mixed:: ToFraction()
{
bool negative = false;
if(whole < 0 || numerator < 0)
negative = true;

numerator = abs(whole)*abs(denominator)+abs(numerator);
whole = 0;
if(negative)
numerator = -numerator;
}

// function to simplify the fraction
void Mixed::Simplify()
{
int a, b;
bool negative = false;
if((whole < 0) || (numerator < 0))
negative = true;

a = abs(numerator);
b = abs(denominator);

while(a >= b)
{
whole = abs(whole)+1;
a -= b;
}

numerator = a;
denominator = b;

int r=a%b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}


numerator /= b;
denominator /= b;

if(negative)
whole = -whole;
}

// overloaded extraction operator
istream& operator>>(istream& in, Mixed& m)
{
char sep;
in>>m.whole>>m.numerator>>sep>>m.denominator;
m.validate();
return in;
}

// overloaded insertion operator
ostream& operator<<(ostream& out, const Mixed& m)
{
if(m.whole == 0)
{
if(m.numerator == 0)
out<<"0";
else
out<<m.numerator<<"/"<<m.denominator;
}else
{
if(m.numerator == 0 && m.denominator == 1)
out<<m.whole;
else
out<<m.whole<<" "<<m.numerator<<"/"<<m.denominator;
}

return out;
}

// comparison operators
bool Mixed::operator<(const Mixed& m)
{
double decimalValue = Evaluate();
double m_decimalValue = m.Evaluate();
return decimalValue < m_decimalValue;
}

bool Mixed:: operator>(const Mixed& m)
{
double decimalValue = Evaluate();
double m_decimalValue = m.Evaluate();
return decimalValue > m_decimalValue;
}

bool Mixed:: operator<=(const Mixed& m)
{
double decimalValue = Evaluate();
double m_decimalValue = m.Evaluate();
return decimalValue <= m_decimalValue;
}

bool Mixed:: operator>=(const Mixed& m)
{
double decimalValue = Evaluate();
double m_decimalValue = m.Evaluate();
return decimalValue >= m_decimalValue;
}

bool Mixed:: operator==(const Mixed& m)
{
return(whole == m.whole && numerator == m.numerator && denominator == m.denominator );
}

bool Mixed:: operator!=(const Mixed& m)
{
return !(*this == m);
}

// arithmetic operators
Mixed Mixed:: operator+(const Mixed& m) const
{
int num = ((this->whole*this->denominator+this->numerator)*m.denominator)+((m.whole*m.denominator+m.numerator)*this->denominator);
   int den = (this->denominator*m.denominator);

Mixed result(0, num, den);
result.Simplify();
return result;
}

Mixed Mixed:: operator-(const Mixed& m) const
{
int num = ((this->whole*this->denominator+this->numerator)*m.denominator)-((m.whole*m.denominator+m.numerator)*this->denominator);
   int den = (this->denominator*m.denominator);

Mixed result(0, num, den);
result.Simplify();
return result;
}

Mixed Mixed:: operator*(const Mixed& m) const
{
int num = ((this->whole*this->denominator+this->numerator))*((m.whole*m.denominator+m.numerator));
   int den = (this->denominator*m.denominator);

   Mixed result(0, num, den);
result.Simplify();
return result;
}

Mixed Mixed:: operator/(const Mixed& m) const
{
int num = ((this->whole*this->denominator+this->numerator))*(m.denominator);
   int den = (m.whole*m.denominator+m.numerator)*denominator;

   Mixed result(0, num, den);
result.Simplify();
return result;
}

// increment and decrement operators
Mixed& Mixed:: operator++()
{
++whole;
return *this;
}

Mixed Mixed:: operator++(int x)
{
Mixed temp = *this;
++*this;
return temp;
}

Mixed& Mixed:: operator--()
{
--whole;
return *this;
}

Mixed Mixed:: operator--(int x)
{
Mixed temp = *this;
--*this;
return temp;
}

//end of mixed.cpp

//**Bigginning of main.cpp**//

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

using namespace std;

int main()
{

// demonstrate behavior of the two constructors and the << overload

Mixed x(3,20,6), y(-4,9,2), m1(0,-35,10), m2(-1,-2,4), m3(4), m4(-11), m5;
char answer;
cout << "Initial values: \nx = " << x << "\ny = " << y
<< "\nm1 = " << m1 << "\nm2 = " << m2 << "\nm3 = " << m3
<< "\nm4 = " << m4 << "\nm5 = " << m5 << "\n\n";

// Trying Simplify
x.Simplify();
m1.Simplify();
cout << "x simplified: " << x << " and m1 simplified " << m1 << "\n\n";

// Trying ToFraction
x.ToFraction();
y.ToFraction();
m1.ToFraction();
cout << "Values as fractions: \nx = " << x << "\ny = " << y
<< "\nm1 = " << m1 << "\n\n";

// demonstrate >> overload

cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;

cout << "You entered:\n";
cout << " x = " << x << '\n';
cout << " y = " << y << '\n';

// demonstrate comparison overloads
if (x < y) cout << "(x < y) is TRUE\n";
if (x > y) cout << "(x > y) is TRUE\n";
if (x <= y) cout << "(x <= y) is TRUE\n";
if (x >= y) cout << "(x >= y) is TRUE\n";
if (x == y) cout << "(x == y) is TRUE\n";
if (x != y) cout << "(x != y) is TRUE\n";

// demonstrating Evaluate
cout << "\nDecimal equivalent of " << x << " is " << x.Evaluate() << '\n';
cout << "Decimal equivalent of " << y << " is " << y.Evaluate() << "\n\n";

// demonstrating arithmetic overloads
cout << "(x + y) = " << x + y << '\n';
cout << "(x - y) = " << x - y << '\n';
cout << "(x * y) = " << x * y << '\n';
cout << "(x / y) = " << x / y << '\n';

// demonstrating arithmetic that uses conversion constructor
// to convert the integer operand to a Mixed object
cout << "(x + 10) = " << x + 10 << '\n';
cout << "(x - 4) = " << x - 4 << '\n';
cout << "(x * -13) = " << x * -13 << '\n';
cout << "(x / 7) = " << x / 7 << '\n';

return 0;
}

//end of main.cpp

Output:


Related Solutions

I need to make sure that my answers are correct please review. The Case as following:...
I need to make sure that my answers are correct please review. The Case as following: Focus Drilling Supplies has been growing steadily over the last 20 years. With increased exploration in the mining sector, the company has decided to expand their facilities for supplies and custom drill bit production to meet the increased demand. The expansion will occur over 4 years and is expected to require $2.8 million. Management has developed a payment plan for carrying out this expansion....
NOTE: I am just checking my own work so if you respond, please make sure I...
NOTE: I am just checking my own work so if you respond, please make sure I can read your handwriting. A loan officer wants to compare the interest rates for 48-month fixed-rate auto loans and 48-month variable-rate auto loans. Two independent, random samples of auto loans are selected. A sample of eight 48-month fixed-rate auto-loans have the following loan rates: 4.29%, 3.75%, 3.50%, 3.99%, 3.75%, 3.99%, 5.40%, 4.00% while a sample of five 48-month variable-rate auto loans have the loan...
please Make A Resume For Truck driver
please Make A Resume For Truck driver
I just need to understand Question 1 and 2. I just wanted to make sure I...
I just need to understand Question 1 and 2. I just wanted to make sure I did the revenue management right. Freedom Airlines recently started operations in the Southwest. The airline owns two airplanes, one based in Phoenix and the other in Denver. Each airplane has a coach section with 140 seats available. Each afternoon, the Phoenix based airplane flies to San Francisco with stopovers in Las Vegas and in San Diego. The Denver-based airplane also flies to San Francisco...
I need a NPV calculation. I just want to make sure I got it correct. Initial...
I need a NPV calculation. I just want to make sure I got it correct. Initial Cost at t=0 is $150 rate = 16% growth rate in perpetuity after year 5 at 1.5% Year 1 2 3 4 5 Cash Flow 5 11.4 14 21 28
I just need to understand Question 1 and 2. I just wanted to make sure I...
I just need to understand Question 1 and 2. I just wanted to make sure I did the revenue management right. Freedom Airlines recently started operations in the Southwest. The airline owns two airplanes, one based in Phoenix and the other in Denver. Each airplane has a coach section with 140 seats available. Each afternoon, the Phoenix based airplane flies to San Francisco with stopovers in Las Vegas and in San Diego. The Denver-based airplane also flies to San Francisco...
I have placed my answers in bold I just need to make sure these are right...
I have placed my answers in bold I just need to make sure these are right I have some with two answers because I'm just not sure which is correct. I just really want to do well on this last assignment any help is apperciated. Thank you! Abby is a 20-year-old female college student. For at least the last 3 months, Abby has experienced ongoing anxiety and worry without a specific cause for these feelings. She has been restless and...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each paragraph is error/grammar free. Please separate each paragraph written by number 1 and 2. .please write with differing viewson their response. Thanks, Chegg 1- 97% of America or 97% of Americans. Obviously, one is based on land area and the other is based on the number of people. If a cell phone company can achieve 97% of Americans, then the company's income must be...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each paragraph is error/grammar free. Please separate each paragraph written by number 1 and 2. Thanks, Chegg 1.In general, when we want to use statistical methods to help us make inferences, we will first put forward hypotheses based on the results and hope that we can use limited data to verify our hypotheses and hypothesis testing are a way to test the statistical hypothesis. In...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each...
I need 7-8 sentences responding to each paragraph written by my peers. Please make sure each paragraph is error/grammar free. Please separate each paragraph written by number 1 and 2. Thanks, Chegg 1- The financial crisis in the United States in 2008-09 was caused by a lack of understanding of probability. Do you think this statement is correct? Why or why not? Please find evidence to support your argument.The statement is correct. Work in risk management has produced a large...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT