/****************************************************** ** Name: ** Filename: RationalNumDrv.cpp ** Project #: Deitel & Deitel 8.17 ** Project Description: Create a class RationalNumber (fractions) with the following capabilities: a)Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators. b)Overload the addition, subtraction, multiplication and division operators for this class. c)Overload the relational and quality operators for this class. ** Output: The result of the first and second fraction using: Addition Subtraction Multiplication Division > or < >= or <= != or == ** Input: First fraction numerator and denominator Second fraction numerator and denominator ** Algorithm: Create class RationalNumber.cpp with operator all operator functions. Create header file RationalNumber.h Print out results using RationalNumberDrv.cpp Ask user for first fraction Ask user for second fraction Use operator+ produce result Use operator- produce result Use operator* produce result Use operator/ produce result Use operator> and operator< produce result Use operator>= and operator<= produce result Use operator!= and operator== produce result Print result of all operator functions End program ******************************************************/ // Include files #include #include #include "RationalNum.h" using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); //Global Variables - should not be used without good reason. int main() { // Declaration section int a, b, c, d; // Executable section instruct (); cout << "Enter first fraction numerator: "; cin >> a; cout << " ---"; cout << "\nEnter first fraction denominator: "; cin >> b; cout << "\nEnter second fraction numerator: "; cin >> c; cout << " ---"; cout << "\nEnter second fraction denominator: "; cin >> d; cout << "\n"; RationalNumber x( a, b ), y( c, d ), z; x.printRational(); cout << " + " ; y.printRational(); cout << " = "; z = x + y; z.printRational(); cout << '\n'; x.printRational(); cout << " - " ; y.printRational(); cout << " = "; z = x - y; z.printRational(); cout << '\n'; x.printRational(); cout << " * " ; y.printRational(); cout << " = "; z = x * y; z.printRational(); cout << '\n'; x.printRational(); cout << " / " ; y.printRational(); cout << " = "; z = x / y; z.printRational(); cout << "\n\n"; x.printRational(); cout << " is:"; cout << "\t"; cout << ( ( x > y ) ? " > " : " <= " ); y.printRational(); cout << " according to the overloaded > operator\n"; cout << "\t"; cout << ( ( x < y ) ? " < " : " >= " ); y.printRational(); cout << " according to the overloaded < operator\n"; cout << "\t"; cout << ( ( x >= y ) ? " >= " : " < " ); y.printRational(); cout << " according to the overloaded >= operator\n"; cout << "\t"; cout << ( ( x <= y ) ? " <= " : " > " ); y.printRational(); cout << " according to the overloaded <= operator\n"; cout << "\t"; cout << ( ( x == y ) ? " == " : " != " ); y.printRational(); cout << " according to the overloaded == operator\n"; cout << "\t"; cout << ( ( x != y ) ? " != " : " == " ); y.printRational(); cout << " according to the overloaded != operator" << endl; pause(); return 0; } void instruct (void) { // Declaration section cout << "Display the use of class RationalNumber to "; cout << "perform various operator functions.\n"; cout << "___________________________________________"; cout << "___________________________________\n\n"; // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output Display the use of class RationalNumber to perform various operator functions. ______________________________________________________________________________ Enter first fraction numerator: 2 --- Enter first fraction denominator: 3 Enter second fraction numerator: 1 --- Enter second fraction denominator: 2 2/3 + 1/2 = 7/6 2/3 - 1/2 = 1/6 2/3 * 1/2 = 1/3 2/3 / 1/2 = 4/3 2/3 is: > 1/2 according to the overloaded > operator >= 1/2 according to the overloaded < operator >= 1/2 according to the overloaded >= operator > 1/2 according to the overloaded <= operator != 1/2 according to the overloaded == operator != 1/2 according to the overloaded != operator Enter first fraction numerator: 1 --- Enter first fraction denominator: 5 Enter second fraction numerator: 1 --- Enter second fraction denominator: 5 1/5 + 1/5 = 2/5 1/5 - 1/5 = 0 1/5 * 1/5 = 1/25 1/5 / 1/5 = 1 1/5 is: <= 1/5 according to the overloaded > operator < 1/5 according to the overloaded < operator >= 1/5 according to the overloaded >= operator <= 1/5 according to the overloaded <= operator == 1/5 according to the overloaded == operator == 1/5 according to the overloaded != operator Press any key to continue... */