TNB Library
TnbFraction.h
[詳解]
1#pragma once
11#include "TnbComparable.h"
12
13
14
15namespace TNB
16{
17
18
19
32template<typename TYP = int>
33class CFractionT : public IComparableT<CFractionT<TYP> >
34{
35public:
36
42 CFractionT(TYP num, TYP den) : m_numerator(num), m_denominator(den)
43 {
44 }
45
51 void Set(TYP num, TYP den)
52 {
53 m_numerator = num;
54 m_denominator = den;
55 }
56
63 {
64 m_numerator = other.m_numerator;
65 m_denominator = other.m_denominator;
66 return *this;
67 }
68
76 virtual INT_PTR Compare(const CFractionT& t) const
77 {
78 TYP t1 = m_numerator * t.m_denominator;
79 TYP t2 = t.m_numerator * m_denominator;
80 if ( t1 == t2 )
81 {
82 return 0;
83 }
84 return (t1 > t2) ? 1 : -1;
85 }
86
93 {
94 if ( m_denominator == other.m_denominator )
95 {
96 m_numerator += other.m_numerator;
97 }
98 else
99 {
100 m_denominator *= other.m_denominator;
101 m_numerator *= other.m_denominator;
102 m_numerator += other.m_numerator * other.m_denominator;
103 }
104 return LeastCommonDivisor();
105 }
106
113 {
114 if ( m_denominator == other.m_denominator )
115 {
116 m_numerator -= other.m_numerator;
117 }
118 else
119 {
120 m_denominator *= other.m_denominator;
121 m_numerator *= other.m_denominator;
122 m_numerator -= other.m_numerator * other.m_denominator;
123 }
124 return LeastCommonDivisor();
125 }
126
133 {
134 m_numerator *= other.m_numerator;
135 m_denominator *= other.m_denominator;
136 return LeastCommonDivisor();
137 }
138
145 {
146 m_numerator *= val;
147 return LeastCommonDivisor();
148 }
149
156 {
157 m_numerator *= other.m_denominator;
158 m_denominator *= other.m_numerator;
159 return LeastCommonDivisor();
160 }
161
168 {
169 m_denominator *= val;
170 return LeastCommonDivisor();
171 }
172
178 {
179 m_numerator = -m_numerator;
180 return *this;
181 }
182
188 CFractionT operator+(const CFractionT& other) const
189 {
190 CFractionT r = *this;
191 return (r += other);
192 }
193
199 CFractionT operator-(const CFractionT& other) const
200 {
201 CFractionT r = *this;
202 return (r -= other);
203 }
204
210 CFractionT operator*(const CFractionT& other) const
211 {
212 CFractionT r = *this;
213 return (r *= other);
214 }
215
221 CFractionT operator*(TYP val) const
222 {
223 CFractionT r = *this;
224 return (r *= val);
225 }
226
232 CFractionT operator/(const CFractionT& other) const
233 {
234 CFractionT r = *this;
235 return (r /= other);
236 }
237
243 CFractionT operator/(TYP val) const
244 {
245 CFractionT r = *this;
246 return (r /= val);
247 }
248
254 TYP GetNumerator(void) const
255 {
256 return m_numerator;
257 }
258
264 TYP GetDenominator(void) const
265 {
266 return m_denominator;
267 }
268
274 double Get(void) const
275 {
276 double r = m_numerator;
277 return r / m_denominator;
278 }
279
286 {
287 TYP r = ms_gcd(m_numerator, m_denominator); //分子と分母の最小公倍数
288 if ( r == 0 )
289 {
290 ; // 何もしない
291 }
292 else
293 {
294 m_numerator /= r;
295 m_denominator /= r;
296 }
297 return *this;
298 }
299
300private:
301
303 static TYP ms_gcd(TYP m, TYP n)
304 {
305 if ( 0 == m || 0 == n )
306 {
307 return 0; // 引数に0がある場合は0を返す
308 }
309 if ( m < n )
310 {
311 Swap(m, n);
312 }
313 // ユークリッドの方法
314 while ( m != n )
315 {
316 (m > n) ? (m -= n) : (n -= m);
317 }
318 return m;
319 }
320
322 static TYP ms_lcm(TYP m, TYP n)
323 {
324 if ( 0 == m || 0 == n )
325 {
326 return 0; // 引数に0がある場合は0を返す
327 }
328 if ( m < n )
329 {
330 Swap(m, n);
331 }
332 return ((m / ms_gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
333 }
334
335 TYP m_numerator;
336 TYP m_denominator;
337 friend class CFractionTest;
338};
339
340
341
350
351
352
353}; // TNB
比較機能関係のヘッダ
分数管理.
Definition: TnbFraction.h:34
TYP GetDenominator(void) const
[取得] 分母取得.
Definition: TnbFraction.h:264
CFractionT operator/(TYP val) const
[計算] 除算.
Definition: TnbFraction.h:243
CFractionT operator+(const CFractionT &other) const
[計算] 加算.
Definition: TnbFraction.h:188
virtual INT_PTR Compare(const CFractionT &t) const
[確認] 比較
Definition: TnbFraction.h:76
CFractionT operator*(const CFractionT &other) const
[計算] 乗算.
Definition: TnbFraction.h:210
CFractionT & operator/=(const CFractionT &other)
[代入] 除算代入.
Definition: TnbFraction.h:155
CFractionT operator-(const CFractionT &other) const
[計算] 減算.
Definition: TnbFraction.h:199
CFractionT & LeastCommonDivisor(void)
[変換] 最小公倍.
Definition: TnbFraction.h:285
CFractionT & operator*=(TYP val)
[代入] 乗算代入.
Definition: TnbFraction.h:144
CFractionT & operator=(const CFractionT &other)
[代入] 代入.
Definition: TnbFraction.h:62
TYP GetNumerator(void) const
[取得] 分子取得.
Definition: TnbFraction.h:254
CFractionT & operator/=(TYP val)
[代入] 除算代入.
Definition: TnbFraction.h:167
CFractionT operator*(TYP val) const
[計算] 乗算.
Definition: TnbFraction.h:221
void Set(TYP num, TYP den)
[設定] 値設定.
Definition: TnbFraction.h:51
CFractionT & operator+=(const CFractionT &other)
[代入] 加算代入.
Definition: TnbFraction.h:92
CFractionT & operator-(void)
[設定] マイナス化.
Definition: TnbFraction.h:177
CFractionT operator/(const CFractionT &other) const
[計算] 除算.
Definition: TnbFraction.h:232
double Get(void) const
[取得] 実数取得.
Definition: TnbFraction.h:274
CFractionT(TYP num, TYP den)
コンストラクタ
Definition: TnbFraction.h:42
CFractionT & operator-=(const CFractionT &other)
[代入] 減算代入.
Definition: TnbFraction.h:112
CFractionT & operator*=(const CFractionT &other)
[代入] 乗算代入.
Definition: TnbFraction.h:132
void Swap(T &t1, T &t2)
[変換] スワッパー.
Definition: TnbDef.h:963
CFractionT< int > CFraction
分数管理.
Definition: TnbFraction.h:349
TNB Library
Definition: TnbDoxyTitle.txt:2
比較機能インターフェース.
Definition: TnbComparable.h:54