用C++实现有理数的一些操作,有些错误,麻烦高手帮改下
//定义了有理数的类和实现方法#include<iostream.h>
#include<stdlib.h>
class rational
{
private:
long num,den;
rational(long p,long q);
rational standardize();//将有理数转换为标准形式,即分母为整数
long gcd(long n,long d)const;//求分子分母的最大公约数
public:
rational(int num=0,long den=1);//将整数转换为有理数即3转换为有理数为3/1
rational(double x);//将双精度浮点数转换为有理数及0.5转换为有理数为1/2
//有理数的输入输出
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;
//例程
long getnum()const;
long getden()const;
void reduce();//将有理数化简
};
rational::rational(long p,long q):num(p),den(q)
{
if(den==0)
{
cerr<<"分母为0,不合法"<<endl;
exit(1);
}
}
rational rational::standardize()
{
if(den==0)
{
cerr<<"分母为0,不合法"<<endl;
exit(1);
}
rational temp;
temp=den>0?rational(num,den):rational(-num,-den);
return temp;
}
long rational::gcd(long n,long d)const
{
long t,i,j;
t=n<=d?n:d;
for(i=1;i<=t;i++)
{
if(n%i==0&&d%i==0)
j=i;
}
return j;
}
rational::rational(int n,long d):num(n),den(1)
{}
rational::rational(double x)
{
double val1,val2;
val1=100000000*x;
val2=10000000*x;
num=long(val1-val2);
den=90000000;
reduce();
}
void rational::reduce()
{
long bigdivisor,tempnum;
tempnum=num<0?-num:num;
if(num==0) den=1;
else
{
bigdivisor=gcd(tempnum,den);
if(bigdivisor>1)
{
num/=bigdivisor;
den/=bigdivisor;
}
}
}
long rational::getnum()const
{
return num;
}
long rational::getden()const
{
return den;
}
rational rational::operator+(rational v)
{
return(num*v.den+den*v.num,den*v.den);
}
rational rational::operator-(rational v)
{
return(num-v.num,den-v.den);
}
rational rational::operator *(rational v)
{
return(num*v.num,den*v.den);
}
rational rational::operator /(rational v)
{
return(num*v.den,den*v.num);
}
rational rational::operator -()
{
return(-num,den);
}
int rational::operator <(rational v)
{
return(num*v.den<v.num*den);
}
int rational::operator <=(rational v)
{
return(num*v.den<=v.num*den);
}
int rational::operator >(rational v)
{
return(num*v.den>v.num*den);
}
int rational::operator >=(rational v)
{
return(num*v.den>=v.num*den);
}
int rational::operator !=(rational v)
{
return(num*v.den!=v.num*den);
}
int rational::operator ==(rational v)
{
return(num*v.den==v.num*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)
{
cerr<<"分母为0,不合法"<<endl;
exit(1);
}
x.standardize();
return istr;
}
ostream& operator<<(ostream& ostr,rational &x)
{
ostr<<x.num<<'/'<<x.den;
return ostr;
}
//应用函数
#include"rational.h"
void main()
{
rational r1(5),r2,r3;
float f;
cout<<r1<<endl;
cout<<"输入两个有理数"<<endl;
cin>>r1>>r2;
cout<<r1+r2<<' '<<r1-r2<<' '<<r1*r2<<' '<<r1/r2<<endl;
}
//这个程序主要的错误实在运算符重载那里,说rational v没有定义,在类中找不到