@@ -18,19 +18,19 @@ class Fraction {
18
18
friend bool equal ( Fraction const & lhs, Fraction const & rhs ) {
19
19
return (lhs.m_num ==rhs.m_num ) && (lhs.m_denom ==rhs.m_denom );
20
20
}
21
-
21
+
22
22
friend int compare ( Fraction const & lhs, Fraction const & rhs ) {
23
23
int v1 = lhs.m_num * rhs.m_denom ;
24
24
int v2 = rhs.m_num * lhs.m_denom ;
25
25
if (v1 < v2) return -1 ;
26
26
else if (v1 > v2) return 1 ;
27
27
else return 0 ;
28
28
}
29
-
29
+
30
30
friend Fraction multiply ( Fraction const & lhs, Fraction const & rhs ) {
31
31
return {lhs.m_num * rhs.m_num , lhs.m_denom * rhs.m_denom };
32
32
}
33
-
33
+
34
34
Fraction normalized () const {
35
35
const int gcd = std::gcd (m_num, m_denom);
36
36
return {m_num/gcd, m_denom/gcd};
@@ -51,23 +51,23 @@ class TestResultPrinter {
51
51
void process (std::string const & what, bool passed) {
52
52
std::cout << std::left << std::setw (m_width) << what << " : " << (passed ? " PASS" : " ** FAIL **" ) << ' \n ' ;
53
53
}
54
-
55
- private:
54
+
55
+ private:
56
56
57
57
unsigned int m_width;
58
58
59
59
};
60
60
61
61
#define CHECK (printer,what ) printer.process(#what, what)
62
-
62
+
63
63
int main () {
64
64
65
65
// create a fraction with values 3 (which is 3/1) and 1/3
66
66
std::cout<<std::endl;
67
67
const Fraction three{3 };
68
68
const Fraction third{1 ,3 };
69
69
std::cout<<three.str ()<<' ' <<third.str ()<<' \n ' ;
70
-
70
+
71
71
// equality
72
72
std::cout<<std::endl;
73
73
TestResultPrinter p1{40 };
@@ -80,14 +80,14 @@ int main() {
80
80
CHECK (p1,equal (Fraction{1 ,3 },third));
81
81
CHECK (p1,!equal (third,Fraction{2 ,6 }));
82
82
CHECK (p1,equal (third,Fraction{2 ,6 }.normalized ()));
83
-
83
+
84
84
// equivalence
85
85
std::cout<<std::endl;
86
86
TestResultPrinter p2{32 };
87
87
CHECK (p2,compare (third,Fraction{2 ,6 })==0 );
88
88
CHECK (p2,compare (third,Fraction{1 ,4 })>0 );
89
89
CHECK (p2,compare (third,Fraction{2 ,4 })<0 );
90
-
90
+
91
91
// multiply
92
92
std::cout<<std::endl;
93
93
TestResultPrinter p3{48 };
0 commit comments