Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CMPSC122 Lab Proj 5 Overloading Operators, Stream, and String Classes Project 5. Building a class for Fraction ADT Phase 2 (20 points. submit your two

CMPSC122 Lab Proj 5 Overloading Operators, Stream, and String Classes Project 5. Building a class for Fraction ADT Phase 2 (20 points. submit your two source files named Frac2.cpp and Frac2.h online only before the due time next week) This assignment is the continuation of project 4, with the modification and improvement of the following modifications: In Frac.h, replace the methods of addition (const Fraction &), subtraction(const Fraction&), multiply( const Fraction &), divide(const Fraction &), void printFraction() by overloading operators +, -, *, /, << respectively. Add overloading operators > and < so that they can compare two Fractions. Write your own implementation file Frac2.cpp to replace Frac.cpp and implement the Fraction class and the necessary overloading operators. Download the driver lab5driver.cpp to test your codes. If your codes in files Frac2.h and Frac2.cpp are correct, the standard output after the execution of your program should be like the following: 7/3 + 1/3 = 8/3 7/3 - 1/3 = 2 7/3 * 1/3 = 7/9 7/3 / 1/3 = 7 7/3 is: > 1/3 according to the overloaded > operator >= 1/3 according to the overloaded < operator Press any key to continue Optional (2 Bonus Points). Improve for cascading use of operators (see LabProjDriver.cpp), Example: x = c + c / d - d * c; Note: 1. Download the driver program to test your class: LabProj5Driver.cpp (don't modify it for your final test). 2. Upload your header file: Frac2.h and your implementation file Frac2.cpp online Grading Policy: (total 20 points) Grading of this programming assignment will reflect two factors, weighted as shown below: 1. (17 points) Correctness -- does the program run correctly and follow the requirement? 2. (3 points) Style -- does the code follow the "Documentation and Submission Guidelines"? Is the code well-structured, and readable? Do you paste your output as the comment after the main function?

CODE:

Frac2.h

// // Name: key solution for header // // a Fraction object holds one Fraction number, one fraction #ifndef FRAC2_H #define FRAC2_H #include using namespace std; class Fraction { // fracional number class public: Fraction(int = 0, int = 1); // default constructor // add the prototypes of required methods here. Fraction operator+(Fraction& b); /* ... */ void printFractionAsFloat(); private: int numerator; int denominator; void reduce(); // utility function, reduce to lowest terms // add the private prototypes methods here. */ Fraction add(const Fraction &); /* ... */ }; ostream& operator<<(ostream& out, Fraction& a); #endif

----------------------------------------------------------------------

Frac2.cpp

// // Name: skeleton template for overloading operators // // a Fraction object holds one Fraction number, one fraction #include "Frac2.h" //------------------------------ Fraction ------------------------------------ // default constructor: parameters are numerator and denominator respectively // if the number is negative, the negative is always stored in the numerator Fraction::Fraction(int n, int d) { numerator = (d < 0 ? -n : n); denominator = (d < 0 ? -d : d); reduce(); } //(a)--------------------------------- add -------------------------------------- // overloaded +: addition of 2 Fractions, current object and parameter //(b)------------------------------ subtract --------------------------------- // subtraction of 2 Fractions, current object and parameter //(c)------------------------------ multiply --------------------------------- // multiplication of 2 Fractions, current object and parameter //(d)-------------------------------- divide --------------------------------- // division of 2 Fractions, current object and parameter, // division by zero crashes //(e)---------------------------- printFraction ------------------------------- //(f)------------------------ printFractionAsFloat ---------------------------- // (g) ------------------------ greater than operator ------------------------- // comparison of 2 Fractions, current object and parameter // (h) ----------------------- less than operator ----------------------------- // comparison of 2 Fractions, current object and parameter //-------------------------------- reduce ------------------------------------- // reduce fraction to lowest terms void Fraction::reduce() { int n = numerator < 0 ? -numerator : numerator; int d = denominator; int largest = n > d ? n : d; int gcd = 0; // greatest common divisor for (int loop = largest; loop >= 2; loop--) if (numerator % loop == 0 && denominator % loop == 0) { gcd = loop; break;

} if (gcd != 0) { numerator /= gcd; denominator /= gcd; } }

----------------------------------------------------------------

Driver Code

// driver for project 5 #include using namespace std; #include "Frac2.h" int main() { Fraction c(7, 3), d(3, 9), x; // c.printFraction(); cout << c; cout << " + "; // d.printFraction(); cout << d; cout << " = "; x = c + d; // x.printFraction(); cout << x; cout << ' '; // c.printFraction(); cout << c; cout << " - "; // d.printFraction(); cout << d; cout << " = "; x = c - d; // x.printFraction(); cout << x; cout << ' '; // c.printFraction(); cout << c; cout << " * "; // d.printFraction(); cout << d; cout << " = "; x = c * d; // x.printFraction(); cout << x; cout << ' '; // c.printFraction(); cout << c; cout << " / "; // d.printFraction(); cout << d; cout << " = "; x = c / d; // x.printFraction(); cout << x; cout << ' '; // c.printFraction(); cout << c; cout << " is: ";

cout << ((c > d) ? " > " : " <= "); // d.printFraction(); cout << d; cout << " according to the overloaded > operator "; cout << ((c < d) ? " < " : " >= "); // d.printFraction(); cout << d; cout << " according to the overloaded < operator "; /* uncomment this line for improved version // c.printFraction(); cout << c; cout << " + "; // d.printFraction(); cout << c; cout << " / "; cout << d; cout << " - "; cout << d; cout << " * "; cout << c; cout << " = "; x = c + c / d - d * c; // cascading use of operators // x.printFraction(); cout << x; cout << ' '; */ // uncomment this line for improve version #ifdef _WIN32 // _WIN32 is used by visual C++ #if (_MSC_VER <= 1916)// check if it Visual Studio 2017 and earlier system("pause"); #endif #endif return 0; }cout << ((c > d) ? " > " : " <= "); // d.printFraction(); cout << d; cout << " according to the overloaded > operator "; cout << ((c < d) ? " < " : " >= "); // d.printFraction(); cout << d; cout << " according to the overloaded < operator "; /* uncomment this line for improved version // c.printFraction(); cout << c; cout << " + "; // d.printFraction(); cout << c; cout << " / "; cout << d; cout << " - "; cout << d; cout << " * "; cout << c; cout << " = "; x = c + c / d - d * c; // cascading use of operators // x.printFraction(); cout << x; cout << ' '; */ // uncomment this line for improve version #ifdef _WIN32 // _WIN32 is used by visual C++ #if (_MSC_VER <= 1916)// check if it Visual Studio 2017 and earlier system("pause"); #endif #endif return 0; }

--------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

Students also viewed these Databases questions