//=========================================================================== // GoTools Core - SINTEF Geometry Tools Core library, version 2.0.1 // // Copyright (C) 2000-2007, 2010 SINTEF ICT, Applied Mathematics, Norway. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // Contact information: E-mail: tor.dokken@sintef.no // SINTEF ICT, Department of Applied Mathematics, // P.O. Box 124 Blindern, // 0314 Oslo, Norway. // // Other licenses are also available for this software, notably licenses // for: // - Building commercial software. // - Building software whose source code you wish to keep private. //=========================================================================== 00015 #ifndef _RATIONAL_H 00016 #define _RATIONAL_H 00017 00018 #include <iostream> 00019 #include <cstdlib> 00020 00021 namespace Go { 00022 00028 class Rational 00029 { 00030 public: 00031 00033 Rational() : p_(0), q_(1) {} 00034 00036 Rational(int p) : p_(p), q_(1) {} 00037 00039 Rational(int p, int q) : p_(p), q_(q) {} 00040 00042 Rational& operator += (const Rational& other) 00043 { 00044 p_ = p_*other.q_ + q_*other.p_; 00045 q_ = q_*other.q_; 00046 simplify(); 00047 return *this; 00048 } 00049 00051 Rational& operator -= (const Rational& other) 00052 { 00053 Rational tmp = -other; 00054 (*this) += tmp; 00055 return *this; 00056 } 00057 00059 Rational& operator *= (const Rational& other) 00060 { 00061 p_ = p_*other.p_; 00062 q_ = q_*other.q_; 00063 simplify(); 00064 return *this; 00065 } 00066 00068 Rational& operator /= (const Rational& other) 00069 { 00070 p_ = p_*other.q_; 00071 q_ = q_*other.p_; 00072 simplify(); 00073 return *this; 00074 } 00075 00077 Rational operator- () const 00078 { 00079 return Rational(-p_, q_); 00080 } 00081 00083 bool operator == (const Rational r) 00084 { 00085 return (p_ == r.p_ && q_ == r.q_); 00086 } 00087 00089 bool operator != (const Rational r) 00090 { 00091 return (p_ != r.p_ || q_ != r.q_); 00092 } 00093 00095 void write(std::ostream& os) const 00096 { 00097 os << p_ << '/' << q_; 00098 } 00099 00101 void simplify() 00102 { 00103 int n = std::min(abs(p_), abs(q_)); 00104 for (int i = 2; i <= n; ++i) { 00105 while (p_%i==0 && q_%i==0) { 00106 p_ /= i; 00107 q_ /= i; 00108 n /= i; 00109 } 00110 } 00111 } 00112 00113 private: 00114 int p_; 00115 int q_; 00116 }; 00117 00118 Rational operator + (const Rational& r1, const Rational r2) 00119 { 00120 Rational res = r1; 00121 res += r2; 00122 return res; 00123 } 00124 00125 Rational operator - (const Rational& r1, const Rational r2) 00126 { 00127 Rational res = r1; 00128 res -= r2; 00129 return res; 00130 } 00131 00132 Rational operator * (const Rational& r1, const Rational r2) 00133 { 00134 Rational res = r1; 00135 res *= r2; 00136 return res; 00137 } 00138 00139 Rational operator / (const Rational& r1, const Rational r2) 00140 { 00141 Rational res = r1; 00142 res /= r2; 00143 return res; 00144 } 00145 00146 std::ostream& operator << (std::ostream& os, const Rational& p) 00147 { 00148 p.write(os); 00149 return os; 00150 } 00151 00152 }; // end namespace Go 00153 #endif // _RATIONAL_H 00154