标题:用C++实现有理数的一些操作,有些错误,麻烦高手帮改下
只看楼主
lly10120303
Rank: 2
等 级:论坛游民
帖 子:11
专家分:12
注 册:2011-3-30
结帖率:100%
已结贴  问题点数:30 回复次数:2 
用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没有定义,在类中找不到

搜索更多相关主题的帖子: private 有理数 公约数 long 
2011-05-15 14:41
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:30 
程序代码:
//定义了有理数的类和实现方法
#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)const
{
    return(num*v.den+den*v.num,den*v.den);
}
rational rational::operator-(rational v)const
{
    return(num-v.num,den-v.den);
}
rational rational::operator *(rational v)const
{
    return(num*v.num,den*v.den);
}
rational rational::operator /(rational v)const
{
    return(num*v.den,den*v.num);
}
rational rational::operator -()const
{
    return(-num,den);
}
int rational::operator <(rational v)const
{
    return(num*v.den<v.num*den);
}
int rational::operator <=(rational v)const
{
    return(num*v.den<=v.num*den);
}
int rational::operator >(rational v)const
{
    return(num*v.den>v.num*den);
}
int rational::operator >=(rational v)const
{
    return(num*v.den>=v.num*den);
}
int rational::operator !=(rational v)const
{
    return(num*v.den!=v.num*den);
}   
int rational::operator ==(rational v)const
{
    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,const 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没有定义,在类中找不到

2011-05-15 16:36
lly10120303
Rank: 2
等 级:论坛游民
帖 子:11
专家分:12
注 册:2011-3-30
得分:0 
这个程序还是有点不对,符号重载返回因该是rational类型的
2011-05-15 20:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339951-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.189686 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved