#include #include #include "Rational.hpp" ///////////////////////////////////////////////// // This file contains all function definitions // // - All function declarations are in // // the file Rational.hpp // ///////////////////////////////////////////////// // Calculate p + q. Rational add( Rational const &p, Rational const &q ) { return Rational{ p.numer_*q.denom_ + q.numer_*p.denom_, p.denom_*q.denom_ }; } // Calculate p + n by treating n as the rational n/1. Rational add( Rational const &p, int const n ) { return add( p, Rational{ n, 1 } ); } // Calculate n + p by treating n as the rational n/1. Rational add( int const n, Rational const &p ) { return add( p, Rational{ n, 1 } ); } // Calculate p - q using addition and the additive inverse. Rational sub( Rational const &p, Rational const &q ) { return add( p, negate( q ) ); } // Calculate p - n. Rational sub( Rational const &p, int const n ) { return add( p, -n ); } // Calculate n - p. Rational sub( int const n, Rational const &p ) { return add( n, negate( p ) ); } // Calculate p*q. Rational mul( Rational const &p, Rational const &q ) { return Rational{ p.numer_*q.numer_, p.denom_*q.denom_ }; } // Calculate p*n by treating n as the rational n/1. Rational mul( Rational const &p, int const n ) { return mul( p, Rational{ n, 1 } ); } // Calculate n*p by treating n as the rational n/1. Rational mul( int const n, Rational const &p ) { return mul( Rational{ n, 1 }, p ); } // Calculate p/q by multiplying p by the inverse of q. Rational div( Rational const &p, Rational const &q ) { assert( q.numer_ != 0 ); return mul( p, inverse( q ) ); } // Calculate p/n for a non-zero integer n. Rational div( Rational const &p, int const n ) { assert( n != 0 ); return mul( p, Rational{ 1, n } ); } // Calculate n/p for a non-zero rational p. Rational div( int const n, Rational const &p ) { assert( p.numer_ != 0 ); return mul( n, inverse( p ) ); } // Return the additive inverse -p. Rational negate( Rational const &p ) { return Rational{ -p.numer_, p.denom_ }; } // Return the multiplicative inverse 1/p. Rational inverse( Rational const &p ) { assert( p.numer_ != 0 ); return Rational{ p.denom_, p.numer_ }; } // Return the absolute value of p. Rational abs( Rational const &p ) { return Rational{ std::abs( p.numer_ ), std::abs( p.denom_ ) }; } // Determine whether p == q. bool equal( Rational const &p, Rational const &q ) { return p.numer_*q.denom_ == q.numer_*p.denom_; } // Determine whether p == n. bool equal( Rational const &p, int const n ) { return equal( p, Rational{ n, 1 } ); } // Determine whether n == p. bool equal( int const n, Rational const &p ) { return equal( Rational{ n, 1 }, p ); } // Determine whether p != q. bool unequal( Rational const &p, Rational const &q ) { return !equal( p, q ); } // Determine whether p != n. bool unequal( Rational const &p, int const n ) { return !equal( p, n ); } // Determine whether n != p. bool unequal( int const n, Rational const &p ) { return !equal( n, p ); } // Determine whether p < q. // - If the product of the denominators is negative, // multiplying by the denominators reverses the inequality. bool less_than( Rational const &p, Rational const &q ) { if ( p.denom_*q.denom_ > 0 ) { return p.numer_*q.denom_ < q.numer_*p.denom_; } else { return p.numer_*q.denom_ > q.numer_*p.denom_; } } // Determine whether p < n. bool less_than( Rational const &p, int const n ) { return less_than( p, Rational{ n, 1 } ); } // Determine whether n < p. bool less_than( int const n, Rational const &p ) { return less_than( Rational{ n, 1 }, p ); } // Determine whether p <= q. bool less_than_or_equal( Rational const &p, Rational const &q ) { return less_than( p, q ) || equal( p, q ); } // Determine whether p <= n. bool less_than_or_equal( Rational const &p, int const n ) { return less_than( p, n ) || equal( p, n ); } // Determine whether n <= p. bool less_than_or_equal( int const n, Rational const &p ) { return less_than( n, p ) || equal( n, p ); } // Determine whether p >= q. bool greater_than_or_equal( Rational const &p, Rational const &q ) { return !less_than( p, q ); } // Determine whether p >= n. bool greater_than_or_equal( Rational const &p, int const n ) { return !less_than( p, n ); } // Determine whether n >= p. bool greater_than_or_equal( int const n, Rational const &p ) { return !less_than( n, p ); } // Determine whether p > q. bool greater_than( Rational const &p, Rational const &q ) { return !less_than_or_equal( p, q ); } // Determine whether p > n. bool greater_than( Rational const &p, int const n ) { return !less_than_or_equal( p, n ); } // Determine whether n > p. bool greater_than( int const n, Rational const &p ) { return !less_than_or_equal( n, p ); } // Return a floating-point approximation of p. double real( Rational const &p ) { assert( p.denom_ != 0 ); return static_cast( p.numer_ ) /static_cast( p.denom_ ); } // Return a string representation of p. // - If the denominator is negative, move the negative // sign to the numerator when constructing the string. // - A denominator of 1 is not displayed. std::string to_string( Rational const &p ) { if ( p.denom_ == 1 ) { return std::to_string( p.numer_ ); } else if ( p.denom_ == -1 ) { return std::to_string( -p.numer_ ); } else if ( p.denom_ > 0 ) { return std::to_string( p.numer_ ) + "/" + std::to_string( p.denom_ ); } else { return std::to_string( -p.numer_ ) + "/" + std::to_string( -p.denom_ ); } }