|
| 1 | +#include <iomanip> |
| 2 | +#include <iostream> |
| 3 | +#include <sstream> |
| 4 | +#include <numeric> |
| 5 | + |
| 6 | +class Fraction { |
| 7 | + |
| 8 | +public: |
| 9 | + |
| 10 | + Fraction(int a_num, int a_denom = 1) : m_num(a_num), m_denom(a_denom) {} |
| 11 | + |
| 12 | + std::string str() const { |
| 13 | + std::ostringstream oss; |
| 14 | + oss << m_num << '/' << m_denom; |
| 15 | + return oss.str(); |
| 16 | + } |
| 17 | + |
| 18 | + friend bool equal( Fraction const & lhs, Fraction const & rhs ) { |
| 19 | + return (lhs.m_num==rhs.m_num) && (lhs.m_denom==rhs.m_denom); |
| 20 | + } |
| 21 | + |
| 22 | + friend bool equivalent( Fraction const & lhs, Fraction const & rhs ) { |
| 23 | + return (lhs.m_num*rhs.m_denom==rhs.m_num*lhs.m_denom); |
| 24 | + } |
| 25 | + |
| 26 | + friend Fraction multiply( Fraction const & lhs, Fraction const & rhs ) { |
| 27 | + return {lhs.m_num*rhs.m_num, lhs.m_denom*rhs.m_denom}; |
| 28 | + } |
| 29 | + |
| 30 | + Fraction normalized() const { |
| 31 | + const int gcd = std::gcd(m_num, m_denom); |
| 32 | + return {m_num/gcd, m_denom/gcd}; |
| 33 | + } |
| 34 | + |
| 35 | +private: |
| 36 | + |
| 37 | + int m_num, m_denom; |
| 38 | +}; |
| 39 | + |
| 40 | +class TestResultPrinter { |
| 41 | + |
| 42 | +public: |
| 43 | + |
| 44 | + TestResultPrinter( unsigned int a_width ) : m_width(a_width) {} |
| 45 | + |
| 46 | + void process(std::string const & what, bool passed) { |
| 47 | + std::cout << std::left << std::setw(m_width) << what << ": " << (passed ? "PASS" : "** FAIL **") << '\n'; |
| 48 | + } |
| 49 | + |
| 50 | +private: |
| 51 | + |
| 52 | + unsigned int m_width; |
| 53 | + |
| 54 | +}; |
| 55 | + |
| 56 | +#define CHECK(printer,what) printer.process(#what, what) |
| 57 | + |
| 58 | +int main() { |
| 59 | + |
| 60 | + // create a fraction with values 3 (which is 3/1) and 1/3 |
| 61 | + std::cout<<std::endl; |
| 62 | + const Fraction three{3}; |
| 63 | + const Fraction third{1, 3}; |
| 64 | + std::cout<<three.str()<<' '<<third.str()<<'\n'; |
| 65 | + |
| 66 | + // equality |
| 67 | + std::cout<<std::endl; |
| 68 | + TestResultPrinter p1{27}; |
| 69 | + CHECK(p1,equal(three,three)); |
| 70 | + CHECK(p1,equal(third,third)); |
| 71 | + CHECK(p1,equal(three,Fraction{3})); |
| 72 | + CHECK(p1,equal(three,Fraction{3,1})); |
| 73 | + CHECK(p1,equal(third,Fraction{1,3})); |
| 74 | + CHECK(p1,equal(Fraction{3},three)); |
| 75 | + CHECK(p1,equal(Fraction{1,3},third)); |
| 76 | + |
| 77 | + // equivalence |
| 78 | + std::cout<<std::endl; |
| 79 | + TestResultPrinter p2{40}; |
| 80 | + CHECK(p2,!equal(third,Fraction{2,6})); |
| 81 | + CHECK(p2,equivalent(third,Fraction{2,6})); |
| 82 | + CHECK(p2,equal(third,Fraction{2,6}.normalized())); |
| 83 | + |
| 84 | + // multiply |
| 85 | + std::cout<<std::endl; |
| 86 | + TestResultPrinter p3{48}; |
| 87 | + CHECK(p3,equal(multiply(third,2),Fraction{2,3})); |
| 88 | + CHECK(p3,equal(multiply(2,third),Fraction{2,3})); |
| 89 | + CHECK(p3,equivalent(multiply(three,third),Fraction{1,1})); |
| 90 | + CHECK(p3,equivalent(multiply(3,third),1)); |
| 91 | + |
| 92 | + // end |
| 93 | + std::cout<<std::endl; |
| 94 | + |
| 95 | +} |
0 commit comments