11#include < iomanip>
22#include < iostream>
3+ #include < numeric>
34
45class Fraction {
6+ public:
57 // TODO: constructors and operators
8+ 9+ private:
10+ void normalize () {
11+ const int gcd = std::gcd (m_num, m_denom);
12+ m_num /= gcd;
13+ m_denom /= gcd;
14+ }
15+ 16+ int m_num, m_denom;
617};
718
8- // TODO:operators
19+ // TODO:operators
920
1021
11- void printAndCheck (const std::string & what, const Fraction & result, const Fraction & expected) {
22+ void printAndCheck (std::string const & what, Fraction const & result, Fractionconst & expected) {
1223 const bool passed = result == expected;
1324 std::cout << std::left << std::setw (40 ) << what << " : " << (passed ? " PASS" : " ** FAIL **" ) << " " << result << " \n " ;
1425}
15- void printAndCheck (const std::string & what, bool result, bool expected) {
26+ void printAndCheck (std::stringconst & what, bool result, bool expected) {
1627 const bool passed = result == expected;
1728 std::cout << std::left << std::setw (40 ) << what << " : " << (passed ? " PASS" : " ** FAIL **" ) << " " << result << " \n " ;
1829}
@@ -36,45 +47,20 @@ int main() {
3647 printAndCheck (" Three times one third" , three * athird, Fraction{1 , 1 });
3748 // normalize the fraction after multiplication so the above statement
3849 // prints 1/1 instead of e.g. 3/3
39- // you might need to compute the greatest common divisor, for which you can
40- // use the function std::gcd(a, b) from the <numeric> header
4150 printAndCheck (" Three times one third" , 3 * athird, Fraction{1 , 1 });
4251
4352 // multiply in place
4453 Fraction f = athird;
4554 f *= 2 ;
4655 printAndCheck (" One third times two" , f, Fraction{2 , 3 });
4756
48- f = athird;
4957 f *= athird;
50- printAndCheck (" One third times one third" , f, Fraction{1 , 9 });
51- 52- f = athird;
53- f *= f *= f;
54- printAndCheck (" One third times itself twice" , f, Fraction{1 , 81 });
58+ printAndCheck (" One third times one third" , f, Fraction{2 , 9 });
5559
5660 // you might have some redundancy between the implementation of operator* and
5761 // operator*=. Can you refactor your code and implement operator* in terms of
5862 // operator*=?
5963
60- // add an int to a fraction
61- printAndCheck (" One third plus 2" , athird + 2 , Fraction{7 , 3 });
62- // ensure symmetry
63- printAndCheck (" 2 plus one third" , 2 + athird, Fraction{7 , 3 });
64- 65- // add two fractions
66- printAndCheck (" One third plus two sixth" , athird + Fraction{2 , 6 }, Fraction{2 , 3 });
67- // normalize the fraction after addition too so the above statement
68- // prints 2/3 instead of 12/18
69- // make sure the normalization between addition and multiplication is not
70- // duplicated, e.g. by putting it into a separate method
71- 72- // add in place
73- f = athird;
74- f += f += 1 ;
75- printAndCheck (" One third plus one and added to itself" , f, Fraction{8 , 3 });
76- // again, try to refactor and implement operator+ in terms of operator+=
77- 7864 std::cout << std::boolalpha; // print bools as 'true' or 'false' from now on
7965
8066 // more equality comparisons
0 commit comments