Maurice Tutor

(5)

$15/per page/Negotiable

About Maurice Tutor

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Algebra,Applied Sciences See all
Algebra,Applied Sciences,Biology,Calculus,Chemistry,Economics,English,Essay writing,Geography,Geology,Health & Medical,Physics,Science Hide all
Teaching Since: May 2017
Last Sign in: 406 Weeks Ago, 1 Day Ago
Questions Answered: 66690
Tutorials Posted: 66688

Education

  • MCS,PHD
    Argosy University/ Phoniex University/
    Nov-2005 - Oct-2011

Experience

  • Professor
    Phoniex University
    Oct-2001 - Nov-2016

Category > Computer Science Posted 12 Sep 2017 My Price 10.00

interesting issue

C++ question. Operator Overloading.

Client Program:

#include <iostream> #include <string> using namespace std; void BasicTest(); void RelationTest(); void BinaryMathTest(); void MathAssignTest(); string boolString(bool convertMe); int main() { BasicTest(); RelationTest(); BinaryMathTest(); MathAssignTest(); } void BasicTest() { cout << "\n----- Testing basic fraction creation & printing\n"; const fraction fr[] = {fraction(4, 8), fraction(-15,21), fraction(10), fraction(12, -3), fraction(), fraction(28, 6), fraction(0, 12)}; for (int i = 0; i < 7; i++){ cout << "fraction [" << i <<"] = " << fr[i] << endl; } } string boolString(bool convertMe) { if (convertMe) { return "true"; } else { return "false"; } } void RelationTest() { cout << "\n----- Testing relational operators between fractions\n"; const fraction fr[] = {fraction(3, 6), fraction(1,2), fraction(-15,30), fraction(1,10), fraction(0,1), fraction(0,2)}; for (int i = 0; i < 5; i++) { cout << "Comparing " << fr[i] << " to " << fr[i+1] << endl; cout << "\tIs left < right? " << boolString(fr[i] < fr[i+1]) << endl; cout << "\tIs left <= right? " << boolString(fr[i] <= fr[i+1]) << endl; cout << "\tIs left > right? " << boolString(fr[i] > fr[i+1]) << endl; cout << "\tIs left >= right? " << boolString(fr[i] >= fr[i+1]) << endl; cout << "\tDoes left == right? " << boolString(fr[i] == fr[i+1]) << endl; cout << "\tDoes left != right ? " << boolString(fr[i] != fr[i+1]) << endl; } cout << "\n----- Testing relations between fractions and integers\n"; fraction f(-3,6); int num = 2; cout << "Comparing " << f << " to " << num << endl; cout << "\tIs left < right? " << boolString(f < num) << endl; cout << "\tIs left <= right? " << boolString(f <= num) << endl; cout << "\tIs left > right? " << boolString(f > num) << endl; cout << "\tIs left >= right? " << boolString(f >= num) << endl; cout << "\tDoes left == right? " << boolString(f == num) << endl; cout << "\tDoes left != right ? " << boolString(f != num) << endl; fraction g(1,4); num = -3; cout << "Comparing " << num << " to " << g << endl; cout << "\tIs left < right? " << boolString(num < g) << endl; cout << "\tIs left <= right? " << boolString(num <= g) << endl; cout << "\tIs left > right? " << boolString(num > g) << endl; cout << "\tIs left >= right? " << boolString(num >= g) << endl; cout << "\tDoes left == right? " << boolString(num == g) << endl; cout << "\tDoes left != right ? " << boolString(num != g) << endl; } void BinaryMathTest() { cout << "\n----- Testing binary arithmetic between fractions\n"; const fraction fr[] = {fraction(1, 6), fraction(1,3), fraction(-2,3), fraction(5), fraction(-4,3)}; for (int i = 0; i < 4; i++) { cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1] << endl; cout << fr[i] << " - " << fr[i+1] << " = " << fr[i] - fr[i+1] << endl; cout << fr[i] << " * " << fr[i+1] << " = " << fr[i] * fr[i+1] << endl; cout << fr[i] << " / " << fr[i+1] << " = " << fr[i] / fr[i+1] << endl; } cout << "\n----- Testing arithmetic between fractions and integers\n"; fraction f(-1, 2); int num = 4; cout << f << " + " << num << " = " << f + num << endl; cout << f << " - " << num << " = " << f - num << endl; cout << f << " * " << num << " = " << f * num << endl; cout << f << " / " << num << " = " << f / num << endl; fraction g(-1, 2); num = 3; cout << num << " + " << g << " = " << num + g << endl; cout << num << " - " << g << " = " << num - g << endl; cout << num << " * " << g << " = " << num * g << endl; cout << num << " / " << g << " = " << num / g << endl; } void MathAssignTest() { cout << "\n----- Testing shorthand arithmetic assignment on fractions\n"; fraction fr[] = {fraction(1, 6), fraction(4), fraction(-1,2), fraction(5)}; for (int i = 0; i < 3; i++) { cout << fr[i] << " += " << fr[i+1] << " = "; cout << (fr[i] += fr[i+1]) << endl; cout << fr[i] << " -= " << fr[i+1] << " = "; cout << (fr[i] -= fr[i+1]) << endl; cout << fr[i] << " *= " << fr[i+1] << " = "; cout << (fr[i] *= fr[i+1]) << endl; cout << fr[i] << " /= " << fr[i+1] << " = "; cout << (fr[i] /= fr[i+1]) << endl; } cout << "\n----- Testing shorthand arithmetic assignment using integers\n"; fraction f(-1, 3); int num = 3; cout << f << " += " << num << " = "; cout << (f += num) << endl; cout << f << " -= " << num << " = "; cout << (f -= num) << endl; cout << f << " *= " << num << " = "; cout << (f *= num) << endl; cout << f << " /= " << num << " = "; cout << (f /= num) << endl; cout << "\n----- Testing increment/decrement prefix and postfix\n"; fraction g(-1, 3); cout << "Now g = " << g << endl; cout << "g++ = " << g++ << endl; cout << "Now g = " << g << endl; cout << "++g = " << ++g << endl; cout << "Now g = " << g << endl; cout << "g-- = " << g-- << endl; cout << "Now g = " << g << endl; cout << "--g = " << --g << endl; cout << "Now g = " << g << endl; }

Correct output:

----- Testing basic fraction creation & printing fraction [0] = 4/8 fraction [1] = -15/21 fraction [2] = 10/1 fraction [3] = 12/-3 fraction [4] = 0/1 fraction [5] = 28/6 fraction [6] = 0/12 ----- Testing relational operators between fractions Comparing 3/6 to 1/2 Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false Comparing 1/2 to -15/30 Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing -15/30 to 1/10 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing 1/10 to 0/1 Is left < right? false Is left <= right? false Is left > right? true Is left >= right? true Does left == right? false Does left != right ? true Comparing 0/1 to 0/2 Is left < right? false Is left <= right? true Is left > right? false Is left >= right? true Does left == right? true Does left != right ? false ----- Testing relations between fractions and integers Comparing -3/6 to 2 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true Comparing -3 to 1/4 Is left < right? true Is left <= right? true Is left > right? false Is left >= right? false Does left == right? false Does left != right ? true ----- Testing binary arithmetic between fractions 1/6 + 1/3 = 9/18 1/6 - 1/3 = -3/18 1/6 * 1/3 = 1/18 1/6 / 1/3 = 3/6 1/3 + -2/3 = -3/9 1/3 - -2/3 = 9/9 1/3 * -2/3 = -2/9 1/3 / -2/3 = 3/-6 -2/3 + 5/1 = 13/3 -2/3 - 5/1 = -17/3 -2/3 * 5/1 = -10/3 -2/3 / 5/1 = -2/15 5/1 + -4/3 = 11/3 5/1 - -4/3 = 19/3 5/1 * -4/3 = -20/3 5/1 / -4/3 = 15/-4 ----- Testing arithmetic between fractions and integers -1/2 + 4 = 7/2 -1/2 - 4 = -9/2 -1/2 * 4 = -4/2 -1/2 / 4 = -1/8 3 + -1/2 = 5/2 3 - -1/2 = 7/2 3 * -1/2 = -3/2 3 / -1/2 = 6/-1 ----- Testing shorthand arithmetic assignment on fractions 1/6 += 4/1 = 25/6 25/6 -= 4/1 = 1/6 1/6 *= 4/1 = 4/6 4/6 /= 4/1 = 4/24 4/1 += -1/2 = 7/2 7/2 -= -1/2 = 16/4 16/4 *= -1/2 = -16/8 -16/8 /= -1/2 = -32/-8 -1/2 += 5/1 = 9/2 9/2 -= 5/1 = -1/2 -1/2 *= 5/1 = -5/2 -5/2 /= 5/1 = -5/10 ----- Testing shorthand arithmetic assignment using integers -1/3 += 3 = 8/3 8/3 -= 3 = -1/3 -1/3 *= 3 = -3/3 -3/3 /= 3 = -3/9 ----- Testing increment/decrement prefix and postfix Now g = -1/3 g++ = -1/3 Now g = 2/3 ++g = 5/3 Now g = 5/3 g-- = 5/3 Now g = 2/3 --g = -1/3 Now g = -1/3

Here are the client program and correct output.

Write a class to handle objects of type fraction. In its simplest form, a fraction is just two integer values: a numerator and a denominator. fractions can be negative, may be improper (larger numerator than denominator), and can be whole numbers (denominator of 1).

This is the first part of a two part assignment. Next week you will be making some refinements to the class that you create this week. For example, no documentation is required this week, but full documentation will be required next week. Also, I expect you to work with just a single file this week. You should begin by copy/pasting the provided client program into a file and add your class to the file, above the client code. Next week you will divide the program up into three files.

Your class should support the following operations on fraction objects:

Construction of a fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero fraction. Use default parameters so that you only need a single function to implement all three of these constructors.

Printing a fraction to a stream with an overloaded << operator. Next week we will get fancy with this, but for now just print the numerator, a forward-slash, and the denominator. No need to change improper fractions to mixed numbers, and no need to reduce.

All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare fractions to other fractions as well as fractions to integers. Either fractions or integers can appear on either side of the binary comparison operator. You should only use one function for each operator.

The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow fractions to be combined with other fractions, as well as with integers. Either fractions or integers can appear on either side of the binary operator. Only use one function for each operator.

The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. fractions can appear on the left-hand side, and fractions or integers on the right-hand side.

The increment and decrement (++, --) operators should be supported in both prefix and postfix form for fractions. To increment or decrement a fraction means to add or subtract (respectively) one (1).

Additional Requirements and Hints:

The name of your class must be "fraction". No variations will work.

Use exactly two data members.

You should not compare two fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time. I would simply cross multiply and compare the products.

Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.

The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations.

My solution has 20 member functions. All of them are less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off.

Do not use as a resource a supplementary text or website if it includes a fraction class (or rational or ratio or whatever).

Answers

(5)
Status NEW Posted 12 Sep 2017 11:09 AM My Price 10.00

Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll

Not Rated(0)