#include <istream>
#include <limits>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "gcd.hpp"
#include "ioflags.hpp"
Go to the source code of this file.
Classes | |
class | rational< T > |
Represent a rational number (fraction) as a numerator and denominator. More... | |
class | rational< T >::zero_denominator |
Exception class if the denominator is ever zero. More... | |
Functions | |
template<class T> | |
rational< T > | operator- (rational< T > const &r) |
Negate a rational number. | |
template<class T> | |
rational< T > | absval (rational< T > const &r) |
template<class T> | |
rational< T > | operator+ (rational< T > lhs, rational< T > const &rhs) |
Addition. | |
template<class T> | |
rational< T > | operator+ (rational< T > lhs, T const &rhs) |
Addition. | |
template<class T> | |
rational< T > | operator+ (T const &lhs, rational< T > rhs) |
Addition. | |
template<class T> | |
rational< T > | operator- (rational< T > lhs, rational< T > const &rhs) |
Subtraction. | |
template<class T> | |
rational< T > | operator- (rational< T > lhs, T const &rhs) |
Subtraction. | |
template<class T> | |
rational< T > | operator- (T const &lhs, rational< T > rhs) |
Subtraction. | |
template<class T> | |
rational< T > | operator * (rational< T > lhs, rational< T > const &rhs) |
Multiplication. | |
template<class T> | |
rational< T > | operator * (rational< T > lhs, T const &rhs) |
Multiplication. | |
template<class T> | |
rational< T > | operator * (T const &lhs, rational< T > rhs) |
Multiplication. | |
template<class T> | |
rational< T > | operator/ (rational< T > lhs, rational< T > const &rhs) |
Division. | |
template<class T> | |
rational< T > | operator/ (rational< T > lhs, T const &rhs) |
Division. | |
template<class T> | |
rational< T > | operator/ (T const &lhs, rational< T > rhs) |
Division. | |
template<class T, class U> | |
bool | operator== (rational< T > const &a, rational< U > const &b) |
Equality comparison. | |
template<class T> | |
bool | operator== (rational< T > const &lhs, T rhs) |
Equality comparison. | |
template<class T> | |
bool | operator== (T lhs, rational< T > const &rhs) |
Equality comparison. | |
template<class T> | |
bool | operator< (rational< T > const &a, rational< T > const &b) |
Less-than comparison. | |
template<class T> | |
bool | operator< (rational< T > const &a, T const &b) |
Less-than comparison. | |
template<class T> | |
bool | operator< (T const &a, rational< T > const &b) |
Less-than comparison. | |
template<class T, class U> | |
bool | operator!= (rational< T > const &a, rational< U > const &b) |
Inequality comparison. | |
template<class T> | |
bool | operator!= (rational< T > const &a, T b) |
Inequality comparison. | |
template<class T> | |
bool | operator!= (T a, rational< T > const &b) |
Inequality comparison. | |
template<class T> | |
bool | operator<= (rational< T > const &a, rational< T > const &b) |
Less-than-or-equal comparison. | |
template<class T> | |
bool | operator<= (rational< T > const &a, T const &b) |
Less-than-or-equal comparison. | |
template<class T> | |
bool | operator<= (T const &a, rational< T > const &b) |
Less-than-or-equal comparison. | |
template<class T> | |
bool | operator> (rational< T > const &a, rational< T > const &b) |
Greater-than comparison. | |
template<class T> | |
bool | operator> (rational< T > const &a, T const &b) |
Greater-than comparison. | |
template<class T> | |
bool | operator> (T const &a, rational< T > const &b) |
Greater-than comparison. | |
template<class T> | |
bool | operator>= (rational< T > const &a, rational< T > const &b) |
Greater-than-or-equal comparison. | |
template<class T> | |
bool | operator>= (rational< T > const &a, T const &b) |
Greater-than-or-equal comparison. | |
template<class T> | |
bool | operator>= (T const &a, rational< T > const &b) |
Greater-than-or-equal comparison. | |
template<class T, class Char, class Traits> | |
std::basic_istream < Char, Traits > & | operator>> (std::basic_istream< Char, Traits > &in, rational< T > &rat) |
Input operator. | |
template<class T, class Char, class Traits> | |
std::basic_ostream < Char, Traits > & | operator<< (std::basic_ostream< Char, Traits > &out, rational< T > const &rat) |
Output operator. |
Definition at line 285 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00286 { 00287 using namespace std; 00288 return rational<T>(abs(r.numerator()), r.denominator()); 00289 }
bool operator!= | ( | T | a, | |
rational< T > const & | b | |||
) | [inline] |
bool operator!= | ( | rational< T > const & | a, | |
T | b | |||
) | [inline] |
bool operator!= | ( | rational< T > const & | a, | |
rational< U > const & | b | |||
) | [inline] |
Definition at line 279 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00280 { 00281 return rational<T>(-r.numerator(), r.denominator()); 00282 }
Division.
Definition at line 382 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00383 { 00384 return rational<T>(lhs * rhs.denominator(), rhs.numerator()); 00385 }
bool operator< | ( | T const & | a, | |
rational< T > const & | b | |||
) | [inline] |
Less-than comparison.
Definition at line 428 of file rational.hpp.
00429 { 00430 return a * b.denominator() < b.numerator(); 00431 }
bool operator< | ( | rational< T > const & | a, | |
T const & | b | |||
) | [inline] |
Less-than comparison.
Definition at line 421 of file rational.hpp.
00422 { 00423 return a.numerator() < b * a.denominator(); 00424 }
Less-than comparison.
Definition at line 414 of file rational.hpp.
00415 { 00416 return a.numerator() * b.denominator() < b.numerator() * a.denominator(); 00417 }
std::basic_ostream<Char, Traits>& operator<< | ( | std::basic_ostream< Char, Traits > & | out, | |
rational< T > const & | rat | |||
) | [inline] |
Output operator.
Definition at line 555 of file rational.hpp.
00556 { 00557 typename std::basic_ostream<Char, Traits>::sentry sentry(out); 00558 std::ostringstream stream; 00559 stream << rat.numerator() << '/' << rat.denominator(); 00560 out << stream.str(); 00561 return out; 00562 }
bool operator<= | ( | T const & | a, | |
rational< T > const & | b | |||
) | [inline] |
bool operator<= | ( | rational< T > const & | a, | |
T const & | b | |||
) | [inline] |
bool operator== | ( | T | lhs, | |
rational< T > const & | rhs | |||
) | [inline] |
Equality comparison.
Definition at line 406 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00407 { 00408 return rhs.denominator() == 1 and 00409 rhs.numerator() == lhs; 00410 }
bool operator== | ( | rational< T > const & | lhs, | |
T | rhs | |||
) | [inline] |
Equality comparison.
Definition at line 398 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00399 { 00400 return lhs.denominator() == 1 and 00401 lhs.numerator() == rhs; 00402 }
bool operator== | ( | rational< T > const & | a, | |
rational< U > const & | b | |||
) | [inline] |
Equality comparison.
Definition at line 390 of file rational.hpp.
References rational< T >::denominator(), and rational< T >::numerator().
00391 { 00392 return a.numerator() == b.numerator() and 00393 a.denominator() == b.denominator(); 00394 }
bool operator> | ( | T const & | a, | |
rational< T > const & | b | |||
) | [inline] |
bool operator> | ( | rational< T > const & | a, | |
T const & | b | |||
) | [inline] |
bool operator>= | ( | T const & | a, | |
rational< T > const & | b | |||
) | [inline] |
bool operator>= | ( | rational< T > const & | a, | |
T const & | b | |||
) | [inline] |
std::basic_istream<Char, Traits>& operator>> | ( | std::basic_istream< Char, Traits > & | in, | |
rational< T > & | rat | |||
) | [inline] |
Input operator.
Definition at line 519 of file rational.hpp.
00520 { 00521 typename std::basic_istream<Char, Traits>::sentry sentry(in, false); 00522 ioflags flags(in); 00523 00524 T n = T(); 00525 if (not (in >> n)) 00526 // Error reading the numerator. 00527 return in; 00528 00529 in >> std::noskipws; 00530 char sep('\0'); 00531 if (not (in >> sep)) 00532 // Error reading the separator character. 00533 return in; 00534 else if (sep != '/') 00535 { 00536 // Push sep back into the input stream, so the next input operation 00537 // will read it. 00538 in.unget(); 00539 rat = n; 00540 return in; 00541 } 00542 else 00543 { 00544 T d = T(); 00545 if (in >> d) 00546 // Successfully read numerator, separator, and denominator. 00547 rat = rational<T>(n, d); 00548 } 00549 00550 return in; 00551 }