In: Computer Science
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
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.
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
Examples: 0 , 2 , -5 , 3/4 , -6/7 , -2 4/5 , 7 2/3
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)
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
The main program for the given problem is -
#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 necessory 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;
}
FOR SAMPLE RUNNING-
Let 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
YOU MAY TRY DIFFERENT SAMPLE RUNS.