夫为剑者

示之以虚 开之以利 后之以发 先之以至

常用链接

统计

积分与排名

最新评论

2006.08.07

ㅏ ,ㅑ ,ㅓ ,ㅕ,ㅗ,ㅛ ,ㅡ,ㅣ
reflector,terrific,vendor,ferryboat.
#ifndef RATIONAL_H_
#define RATIONAL_H_

#include <iostream.h>
#include <stdlib.h>

class Rational //有理数类
{
private:
long num,den;//分子分母
Rational(long number,long denom);
void Standordize();//将分母转换为正数
long gcd(long m,long n) const;//最大公约数
public:
Rational(int number=0,long denom=1);
Rational(double x);//double类型转换为有理数

friend istream& operator >>(istream& istr,Rational &x);
friend ostream& operator <<(ostream& ostr,const Rational &x);

Rational operator +(Rational v) const;
Rational operator -(Rational v) const;
Rational operator *(Rational v) const;
Rational operator /(Rational v) const;

Rational operator -() const;

int operator <(Rational v) const;
int operator >(Rational v) const;
int operator <=(Rational v) const;
int operator >=(Rational v) const;
int operator ==(Rational v) const;
int operator !=(Rational v) const;

operator double() const;//有理数转换为double

long GetNumberator() const;
long GetDenominator() const;
void Reduce();//最简形式
};

#endif



#include "Rational.h"

long Rational::gcd(long m,long n) const
{
long r;
if (m==0 || n==0)
exit(1);
while (r = m % n)
{
m = n;
n = r;
}
return n;
}

void Rational::Standordize()
{
if (den < 0)
{
den = -den;
num = -num;
}
}

Rational::Rational(int number/* =0 */,long denom/* =1 */)
{
if (denom == 0)
exit(1);
num = number;
den = denom;
Standordize();
}

Rational::Rational(long number,long denom):num(number),den(denom)
{
if (den == 0)
exit(1);
}

Rational::Rational(double x)
{
double var1,var2;
var1 = 100000000L * x;
var2 = 10000000L * x;
num = long(var1-var2);
den = 90000000L;
Reduce();
}

void Rational::Reduce()
{
long r = gcd(num,den);
num /= r;
den /= r;
}

long Rational::GetDenominator() const
{
return den;
}

long Rational::GetNumberator() const
{
return num;
}

Rational Rational::operator +(Rational v) const
{
return Rational(num*v.GetDenominator()+v.GetNumberator()*den,den*v.GetDenominator());
}

Rational Rational::operator -(Rational v) const
{
return Rational(num*v.GetDenominator()-v.GetNumberator()*den,den*v.GetDenominator());
}
Rational Rational::operator *(Rational v) const
{
return Rational(num*v.GetNumberator(),den*v.GetDenominator());
}
Rational Rational::operator /(Rational v) const
{
return Rational(num*v.GetDenominator(),den*v.GetNumberator());
}

Rational Rational::operator -() const
{
return Rational(-num,den);
}

int Rational::operator <(Rational v) const
{
return den*v.GetDenominator()>0 ? num*v.GetDenominator()<v.GetNumberator()*den : num*v.GetDenominator()>v.GetNumberator()*den;
}

int Rational::operator >(Rational v) const
{
return den*v.GetDenominator()>0 ? num*v.GetDenominator()>v.GetNumberator()*den : num*v.GetDenominator()<v.GetNumberator()*den;
}

int Rational::operator <=(Rational v) const
{
return den*v.GetDenominator()>0 ? num*v.GetDenominator()<=v.GetNumberator()*den : num*v.GetDenominator()>=v.GetNumberator()*den;
}

int Rational::operator >=(Rational v) const
{
return den*v.GetDenominator()>0 ? num*v.GetDenominator()>=v.GetNumberator()*den : num*v.GetDenominator()<=v.GetNumberator()*den;
}

int Rational::operator ==(Rational v) const
{
return num*v.GetDenominator()==v.GetNumberator()*den;
}

int Rational::operator !=(Rational v) const
{
return num*v.GetDenominator()!=v.GetNumberator()*den;
}

Rational::operator double() const
{
return double(num)/den;
}

istream& operator >>(istream& istr,Rational &x)
{
char c;
istr>>x.num>>c>>x.den;
if (x.den == 0)
{
exit(1);
}
return istr;
}

ostream& operator <<(ostream& ostr,const Rational &x)
{
ostr<<"the num is:"<<x.num<<endl;
ostr<<"the den is:"<<x.den<<endl;
return ostr;
}

posted on 2006-08-08 10:06 vivip 阅读(153) 评论(0)  编辑 收藏 引用 所属分类: 自我监督


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理