#include #include "Rational.hpp" // Function declaration int main(); // Function definition int main() { Rational p{ 3, 4 }; Rational q{ 1, 5 }; // The constructor puts every Rational object into // normal form. Rational r{ 6, 8 }; std::cout << "p = " << p << std::endl; std::cout << "q = " << q << std::endl; std::cout << "r = " << r << std::endl; // Although r was constructed using 6/8, it is stored // in normal form as 3/4. std::cout << std::endl << "The numerator of r is " << r.numer() << "." << std::endl; std::cout << "The denominator of r is " << r.denom() << "." << std::endl; // Demonstrate the arithmetic operators. std::cout << std::endl << "Arithmetic operators:" << std::endl; std::cout << "p + q = " << p + q << std::endl; std::cout << "p + 2 = " << p + 2 << std::endl; std::cout << "2 + q = " << 2 + q << std::endl; std::cout << "p - q = " << p - q << std::endl; std::cout << "p - 2 = " << p - 2 << std::endl; std::cout << "2 - q = " << 2 - q << std::endl; std::cout << "p*q = " << p*q << std::endl; std::cout << "p*2 = " << p*2 << std::endl; std::cout << "2*q = " << 2*q << std::endl; std::cout << "p/q = " << p/q << std::endl; std::cout << "p/2 = " << p/2 << std::endl; std::cout << "2/q = " << 2/q << std::endl; // Demonstrate the unary operators. std::cout << std::endl << "Unary operators:" << std::endl; std::cout << "+p = " << +p << std::endl; std::cout << "-p = " << -p << std::endl; // Demonstrate the comparison operators. // // Notice that p == r even though r was constructed // using 6/8. Both objects are stored internally as 3/4. std::cout << std::endl << std::boolalpha << "Comparison operators:" << std::endl; std::cout << "p == q: " << (p == q) << std::endl; std::cout << "p == r: " << (p == r) << std::endl; std::cout << "p == 1: " << (p == 1) << std::endl; std::cout << "1 == p: " << (1 == p) << std::endl; std::cout << "p != q: " << (p != q) << std::endl; std::cout << "p != r: " << (p != r) << std::endl; std::cout << "p != 1: " << (p != 1) << std::endl; std::cout << "1 != p: " << (1 != p) << std::endl; std::cout << "p < q: " << (p < q) << std::endl; std::cout << "q < p: " << (q < p) << std::endl; std::cout << "p < 1: " << (p < 1) << std::endl; std::cout << "1 < p: " << (1 < p) << std::endl; std::cout << "p <= q: " << (p <= q) << std::endl; std::cout << "p <= r: " << (p <= r) << std::endl; std::cout << "p <= 1: " << (p <= 1) << std::endl; std::cout << "1 <= p: " << (1 <= p) << std::endl; std::cout << "p >= q: " << (p >= q) << std::endl; std::cout << "p >= r: " << (p >= r) << std::endl; std::cout << "p >= 1: " << (p >= 1) << std::endl; std::cout << "1 >= p: " << (1 >= p) << std::endl; std::cout << "p > q: " << (p > q) << std::endl; std::cout << "q > p: " << (q > p) << std::endl; std::cout << "p > 1: " << (p > 1) << std::endl; std::cout << "1 > p: " << (1 > p) << std::endl; // Demonstrate the other member functions. std::cout << std::endl << "Other member functions:" << std::endl; std::cout << "p.numer() = " << p.numer() << std::endl; std::cout << "p.denom() = " << p.denom() << std::endl; std::cout << "p.inverse() = " << p.inverse() << std::endl; std::cout << "p.real() = " << p.real() << std::endl; std::cout << "p.to_string() = " << p.to_string() << std::endl; Rational s{ -3, 4 }; std::cout << "s = " << s << std::endl; std::cout << "s.abs() = " << s.abs() << std::endl; // Demonstrate the default arguments and normalization // performed by the constructor. std::cout << std::endl << "Constructors and normal form:" << std::endl; Rational a{}; Rational b{ 100, -200 }; Rational c{ -47, 94 }; // Rational{} uses the default arguments 0 and 1. std::cout << "Rational a{} gives a = " << a << std::endl; // The negative sign is moved to the numerator and the // numerator and denominator are divided by their GCD. std::cout << "Rational b{ 100, -200 } gives b = " << b << std::endl; std::cout << "Rational c{ -47, 94 } gives c = " << c << std::endl; // Both b and c are therefore stored in exactly the // same normal form. std::cout << "b == c: " << (b == c) << std::endl; return 0; }