-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrational-1.cpp
More file actions
140 lines (120 loc) · 3.27 KB
/
Copy pathrational-1.cpp
File metadata and controls
140 lines (120 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* A simple Rational number class. It prints messages to show which constructor (ctor) is being called.
* This version does not have move constructor, it is just showing how things are before using move semantics.
*/
#include <cstdio>
#include <string>
void message( const char * s ) {
puts(s);
fflush(stdout);
}
class Rational {
int _n; // numerator
int _d; // denominator
static const int _bufsize = 128; // c_str buffer size
mutable char * _buf = nullptr; // c_str buffer
public:
//default constructor
Rational() {
reset();
message("default contructor");
}
//int constructor
Rational( int n )
: _n{ n }, _d{ 1 } {
message("int constructor");
}
//int, int constructor
Rational( int n, int d )
: _n{ n }, _d{ d } {
message("int, int constructor");
}
//copy constructor
Rational( const Rational & other )
: _n{ other._n }, _d{ other._d} {
message("copy constructor");
}
//destructor
~Rational();
//reset
void reset();
//numerator
inline int numerator() const {
return _n;
}
//denominator
inline int denominator() const {
return _d;
}
//operator =
Rational& operator = ( const Rational & );
//operator +
Rational operator + ( const Rational & ) const;
//operator -
Rational operator - ( const Rational & ) const;
//operator *
Rational operator * ( const Rational & ) const;
//operator /
Rational operator / ( const Rational & ) const;
//operator std::string()
operator std::string() const;
//std::string string()
std::string string() const;
//const char * c_str()
const char * c_str() const;
};
Rational :: ~Rational() {
message("destructor");
reset();
}
void Rational :: reset() {
_n = 0;
_d = 1;
delete[] _buf;
_buf = nullptr;
}
Rational & Rational :: operator = ( const Rational & rhs ) {
message("assignment operator");
if ( this != &rhs ) {
_n = rhs.numerator();
_d = rhs.denominator();
}
return *this;
}
Rational Rational :: operator + ( const Rational & rhs ) const {
return Rational( (_n * rhs._d) + (_d * rhs._n), _d * rhs._d );
}
Rational Rational :: operator - ( const Rational & rhs ) const {
return Rational( (_n * rhs._d) - (_d * rhs._n), _d * rhs._d );
}
Rational Rational :: operator * ( const Rational & rhs ) const {
return Rational( (_n * rhs._n), (_d * rhs._d) );
}
Rational Rational :: operator / ( const Rational & rhs ) const {
return Rational( (_n * rhs._d), (_d * rhs._n) );
}
Rational :: operator std::string() const {
return string();
}
std::string Rational::string() const {
return std::string{ c_str() };
}
const char * Rational :: c_str() const {
if ( !_buf ) {
_buf = new char[_bufsize]();
}
snprintf(_buf, _bufsize, "%d/%d", _n, _d);
return _buf;
}
int main() {
Rational a = 8; // 7/1
Rational b( 5, 3 ); // 5/3
Rational c = b; // copy constructor
Rational d; // default constructor
printf("a is: %s\n", a.c_str());
printf("b is: %s\n", b.c_str());
printf("c is: %s\n", c.c_str());
printf("d is: %s\n", d.c_str());
printf("%s + %s is %s\n", a.c_str(), b.c_str(), Rational(a + b).c_str());
return 0;
}