Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 63c1a6a

Browse files
Simplify operators exercise
* provide definition of normalize() * provide two data members for numerator and denominator * remove addition operators * do not check whether *= can be chained
1 parent 22c7860 commit 63c1a6a

File tree

2 files changed

+16
-73
lines changed

2 files changed

+16
-73
lines changed

‎code/operators/operators.cpp‎

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
#include <iomanip>
22
#include <iostream>
3+
#include <numeric>
34

45
class 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(conststd::string & what, constFraction & result, constFraction & 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(conststd::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

‎code/operators/solution/operators.sol.cpp‎

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <cassert>
21
#include <iomanip>
32
#include <iostream>
43
#include <numeric>
@@ -32,23 +31,6 @@ class Fraction {
3231
friend Fraction operator*(int i, Fraction const & f) { return f * i; }
3332
friend Fraction operator*(Fraction a, Fraction const & b) { return a *= b; }
3433

35-
Fraction & operator+=(int i) {
36-
m_num += i * m_denom;
37-
return *this;
38-
}
39-
40-
Fraction & operator+=(Fraction o) {
41-
m_num *= o.m_denom;
42-
m_num += o.m_num * m_denom;
43-
m_denom *= o.m_denom;
44-
normalize();
45-
return *this;
46-
}
47-
48-
friend Fraction operator+(Fraction r, int i) { return r += i; }
49-
friend Fraction operator+(int i, Fraction const & r) { return r + i; }
50-
friend Fraction operator+(Fraction a, Fraction const & b) { return a += b; }
51-
5234
friend bool operator==(Fraction const & a, Fraction const & b) {
5335
return a.m_num == b.m_num && a.m_denom == b.m_denom;
5436
}
@@ -100,45 +82,20 @@ int main() {
10082
printAndCheck("Three times one third", three * athird, Fraction{1, 1});
10183
// normalize the fraction after multiplication so the above statement
10284
// prints 1/1 instead of e.g. 3/3
103-
// you might need to compute the greatest common divisor, for which you can
104-
// use the function std::gcd(a, b) from the <numeric> header
10585
printAndCheck("Three times one third", 3 * athird, Fraction{1, 1});
10686

10787
// multiply in place
10888
Fraction f = athird;
10989
f *= 2;
11090
printAndCheck("One third times two", f, Fraction{2, 3});
11191

112-
f = athird;
11392
f *= athird;
114-
printAndCheck("One third times one third", f, Fraction{1, 9});
115-
116-
f = athird;
117-
f *= f *= f;
118-
printAndCheck("One third times itself twice", f, Fraction{1, 81});
93+
printAndCheck("One third times one third", f, Fraction{2, 9});
11994

12095
// you might have some redundancy between the implementation of operator* and
12196
// operator*=. Can you refactor your code and implement operator* in terms of
12297
// operator*=?
12398

124-
// add an int to a fraction
125-
printAndCheck("One third plus 2", athird + 2, Fraction{7, 3});
126-
// ensure symmetry
127-
printAndCheck("2 plus one third", 2 + athird, Fraction{7, 3});
128-
129-
// add two fractions
130-
printAndCheck("One third plus two sixth", athird + Fraction{2, 6}, Fraction{2, 3});
131-
// normalize the fraction after addition too so the above statement
132-
// prints 2/3 instead of 12/18
133-
// make sure the normalization between addition and multiplication is not
134-
// duplicated, e.g. by putting it into a separate method
135-
136-
// add in place
137-
f = athird;
138-
f += f += 1;
139-
printAndCheck("One third plus one and added to itself", f, Fraction{8, 3});
140-
// again, try to refactor and implement operator+ in terms of operator+=
141-
14299
std::cout << std::boolalpha; // print bools as 'true' or 'false' from now on
143100

144101
// more equality comparisons

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /