#include // The 'Rational.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Rational.cpp' file contains all the function // definitions. You can treat all of these functions as if // they were defined in this file. // // Together, these two files provide the Rational class and // the functions that operate on Rational objects. #include "Rational.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { Rational p{ 3, 4 }; Rational q{ 1, 5 }; std::cout << "The original rational numbers:" << std::endl; std::cout << " p = " << to_string( p ) << std::endl; std::cout << " q = " << to_string( q ) << std::endl; ////////////////////////////////////////////////////////// // Arithmetic operations // ////////////////////////////////////////////////////////// // Test the three versions of add(...). std::cout << std::endl << "Addition:" << std::endl; std::cout << " p + q = " << to_string( add( p, q ) ) << std::endl; std::cout << " p + 2 = " << to_string( add( p, 2 ) ) << std::endl; std::cout << " 2 + q = " << to_string( add( 2, q ) ) << std::endl; // Test the three versions of sub(...). std::cout << std::endl << "Subtraction:" << std::endl; std::cout << " p - q = " << to_string( sub( p, q ) ) << std::endl; std::cout << " p - 2 = " << to_string( sub( p, 2 ) ) << std::endl; std::cout << " 2 - q = " << to_string( sub( 2, q ) ) << std::endl; // Test the three versions of mul(...). std::cout << std::endl << "Multiplication:" << std::endl; std::cout << " p*q = " << to_string( mul( p, q ) ) << std::endl; std::cout << " p*2 = " << to_string( mul( p, 2 ) ) << std::endl; std::cout << " 2*q = " << to_string( mul( 2, q ) ) << std::endl; // Test the three versions of div(...). std::cout << std::endl << "Division:" << std::endl; std::cout << " p/q = " << to_string( div( p, q ) ) << std::endl; std::cout << " p/2 = " << to_string( div( p, 2 ) ) << std::endl; std::cout << " 2/q = " << to_string( div( 2, q ) ) << std::endl; // Test negate(...). std::cout << std::endl << "The additive inverse of p:" << std::endl; std::cout << " -p = " << to_string( negate( p ) ) << std::endl; // Test inverse(...). std::cout << std::endl << "The multiplicative inverse of p:" << std::endl; std::cout << " 1/p = " << to_string( inverse( p ) ) << std::endl; // Test abs(...) using a rational number with a negative value. Rational r{ -3, 4 }; std::cout << std::endl << "The absolute value of r:" << std::endl; std::cout << " r = " << to_string( r ) << std::endl; std::cout << " abs( r ) = " << to_string( abs( r ) ) << std::endl; ////////////////////////////////////////////////////////// // Comparisons // ////////////////////////////////////////////////////////// // Display Boolean values as 'true' and 'false' rather // than as the integers 1 and 0. std::cout << std::boolalpha; // Test the three versions of equal(...). std::cout << std::endl << "Equality:" << std::endl; std::cout << " p == q: " << equal( p, q ) << std::endl; std::cout << " p == 2: " << equal( p, 2 ) << std::endl; std::cout << " 2 == q: " << equal( 2, q ) << std::endl; // Test the three versions of unequal(...). std::cout << std::endl << "Inequality:" << std::endl; std::cout << " p != q: " << unequal( p, q ) << std::endl; std::cout << " p != 2: " << unequal( p, 2 ) << std::endl; std::cout << " 2 != q: " << unequal( 2, q ) << std::endl; // Test the three versions of less_than(...). std::cout << std::endl << "Less than:" << std::endl; std::cout << " p < q: " << less_than( p, q ) << std::endl; std::cout << " p < 2: " << less_than( p, 2 ) << std::endl; std::cout << " 2 < q: " << less_than( 2, q ) << std::endl; // Test the three versions of less_than_or_equal(...). std::cout << std::endl << "Less than or equal:" << std::endl; std::cout << " p <= q: " << less_than_or_equal( p, q ) << std::endl; std::cout << " p <= 2: " << less_than_or_equal( p, 2 ) << std::endl; std::cout << " 2 <= q: " << less_than_or_equal( 2, q ) << std::endl; // Test the three versions of greater_than_or_equal(...). std::cout << std::endl << "Greater than or equal:" << std::endl; std::cout << " p >= q: " << greater_than_or_equal( p, q ) << std::endl; std::cout << " p >= 2: " << greater_than_or_equal( p, 2 ) << std::endl; std::cout << " 2 >= q: " << greater_than_or_equal( 2, q ) << std::endl; // Test the three versions of greater_than(...). std::cout << std::endl << "Greater than:" << std::endl; std::cout << " p > q: " << greater_than( p, q ) << std::endl; std::cout << " p > 2: " << greater_than( p, 2 ) << std::endl; std::cout << " 2 > q: " << greater_than( 2, q ) << std::endl; ////////////////////////////////////////////////////////// // Conversions // ////////////////////////////////////////////////////////// // The calls to to_string(...) above have already // demonstrated the to_string(...) function. // Test real(...). std::cout << std::endl << "A floating-point approximation of p:" << std::endl; std::cout << " real( p ) = " << real( p ) << std::endl; return 0; }