00001
00002 #ifndef Rational_H
00003 #define Rational_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00021
00022 #ifdef _MSC_VER
00023 #define _CRT_SECURE_NO_WARNINGS
00024 #endif
00025
00026 #include "Musimat.h"
00027 #include <iostream>
00028 #include <stdio.h>
00029
00031
00032 class Rational
00033 {
00034 friend ostream& operator<<(ostream&, Rational&);
00035 friend istream& operator>>( istream& is, Rational& p );
00036
00037 friend Bool operator==(Integer& i, Rational& p) { Return i == p.quotient(); }
00038 friend Bool operator==(Rational& p, Integer& i) { Return p.quotient() == i; }
00039 friend Bool operator!=(Integer& i, Rational& p) { Return i != p.quotient(); }
00040 friend Bool operator!=(Rational& p, Integer& i) { Return p.quotient() != i; }
00041 friend Bool operator>=(Rational& p, Integer& r) { Return p.quotient() >= r; }
00042 friend Bool operator>=(Integer& i, Rational& p) { Return i >= p.quotient(); }
00043 friend Bool operator>(Rational& p, Integer& i) { Return p.quotient() > i; }
00044 friend Bool operator>(Integer& i, Rational& p) { Return i > p.quotient(); }
00045 friend Bool operator<=(Rational& p, Integer& i) { Return p.quotient() <= i; }
00046 friend Bool operator<=(Integer& i, Rational& p) { Return i <= p.quotient(); }
00047
00048 friend Bool operator==(Real& r, Rational& p) { Return r == p.quotient(); }
00049 friend Bool operator==(Rational& p, Real& r) { Return p.quotient() == r; }
00050 friend Bool operator!=(Real& r, Rational& p) { Return r != p.quotient(); }
00051 friend Bool operator!=(Rational& p, Real& r){ Return p.quotient() != r; }
00052 friend Bool operator>=(Rational& p, Real& r) { Return p.quotient() >= r; }
00053 friend Bool operator>=(Real& r, Rational& p) { Return r >= p.quotient(); }
00054 friend Bool operator>(Rational& p, Real& r) { Return p.quotient() > r; }
00055 friend Bool operator>(Real& r, Rational& p) { Return r > p.quotient(); }
00056 friend Bool operator<=(Rational& p, Real& r) { Return p.quotient() <= r; }
00057 friend Bool operator<=(Real& r, Rational& p) { Return r <= p.quotient(); }
00058
00059 public:
00060
00062 Rational() : m_num(0), m_den(0) { }
00063
00065 Rational(Integer i) : m_num(i), m_den(i) { }
00066
00068 Rational(Const Rational& x) : m_num(x.m_num), m_den(x.m_den) { }
00069
00072 Rational(Real x) : m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }
00073
00077 Rational(Integer num, Integer den) : m_num(num), m_den(den) {
00078 If (m_den == 0)
00079 m_den = 1;
00080 }
00081
00086 Rational(Real x, Integer& num, Integer& den) : m_num(0), m_den(0) {
00087 RealToRational( x, num, den );
00088 m_num = num;
00089 m_den = den;
00090 }
00091
00096 Rational(Rational x, Integer& num, Integer& den) : m_num(0), m_den(0) {
00097 RealToRational( x.quotient(), num, den );
00098 m_num = num;
00099 m_den = den;
00100 }
00101
00103 Rational& operator=(Const Rational& x) {
00104 m_num = x.m_num;
00105 m_den = x.m_den;
00106 Return *this;
00107 }
00108
00110 Bool operator==(Const Rational& x) Const {
00111 Return m_num == x.m_num And m_den == x.m_den;
00112 }
00113
00115 Bool operator!=(Const Rational& x) Const {
00116 Return m_num != x.m_num || m_den != x.m_den;
00117 }
00118
00120 Bool operator>=(Const Rational& x) Const {
00121 Return quotient() >= x.quotient();
00122 }
00123
00125 Bool operator>(Const Rational& x) Const {
00126 Return quotient() > x.quotient();
00127 }
00128
00130 Bool operator<(Const Rational& x) Const {
00131 Return quotient() < x.quotient();
00132 }
00133
00135 Bool operator<=(Const Rational& x) Const {
00136 Return quotient() <= x.quotient();
00137 }
00138
00140 Rational& operator+=(Const Real x) {
00141 Return( *this = Rational(quotient() + x) );
00142 }
00143
00145 Rational& operator+=(Rational x) {
00146 Return( *this = Rational(quotient() + x.quotient() ) );
00147 }
00148
00150 Rational& operator-=(Const Real x) {
00151 Return( *this = quotient() - x );
00152 }
00153
00155 Rational& operator-=(Rational x) {
00156 Return( *this = quotient() - x.quotient() );
00157 }
00158
00160 Rational& operator*=(Const Real x) {
00161 Return( *this = quotient() * x );
00162 }
00163
00165 Rational& operator*=(Rational x) {
00166 Return( *this = quotient() * x.quotient() );
00167 }
00168
00170 Rational& operator/=(Const Real x) {
00171 Return( *this = quotient() / x );
00172 }
00173
00175 Rational& operator/=(Rational x) {
00176 Return( *this = quotient() / x.quotient() );
00177 }
00178
00180 Rational operator+(Rational& x) {
00181 Return( Rational(quotient() + x.quotient()) );
00182 }
00183
00185 Rational operator-(Rational& x) {
00186 Return( Rational(quotient() - x.quotient()) );
00187 }
00188
00190 Rational operator*(Rational& x) {
00191 Return( Rational(quotient() * x.quotient()) );
00192 }
00193
00195 Rational operator/(Rational& x) {
00196 Return( Rational(quotient() / x.quotient()) );
00197 }
00198
00200 Rational operator+(Const Real x) {
00201 Return( Rational(quotient() + x) );
00202 }
00203
00205 Rational operator-(Const Real x) {
00206 Return( Rational(quotient() - x) );
00207 }
00208
00210 Rational operator*(Const Real x) {
00211 Return( Rational(quotient() * x) );
00212 }
00213
00215 Rational operator/(Const Real x) {
00216 Return( Rational(quotient() / x ) );
00217 }
00218
00221 String print(String s = 0) Const {
00222 If (s)
00223 cout << s;
00224 #ifdef _MSC_VER
00225
00226
00227 #pragma warning(push)
00228 #pragma warning(disable: 4996)
00229 sprintf(s_buf, "{%d,%d}", m_num, m_den);
00230 #pragma warning(pop)
00231 #else
00232 sprintf(s_buf, "{%d,%d}", m_num, m_den);
00233 #endif
00234 Return s_buf;
00235 }
00236
00237 private:
00238
00239
00240 Real quotient() Const { Return( Real(m_num) / Real(m_den) ); }
00241
00242 protected:
00243 Integer m_num;
00244 Integer m_den;
00245 Static char s_buf[128];
00246 };
00247
00248 #endif // Rational_H