码迷,mamicode.com
首页 > 编程语言 > 详细

c++ Fraction类 操作符重载练习

时间:2014-05-14 11:18:00      阅读:675      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

bubuko.com,布布扣
#include <iostream>
using namespace std;

int GreatestCommonDivisor(int m,int n) //辗转相除法求最大公约数
{
    int r;
    do  
    {   
        r = m % n;  
        m = n;  
        n = r;  
    } while ( r != 0 ); 
    return m;
}

class Fraction;                                            //
ostream& operator<<(ostream& out,const Fraction &fra);    //
istream& operator>>(istream& in,const Fraction& fra);    //此三行为满足vc6.0的编译逻辑而声明

class Fraction{
private:
    int fenzi,fenmu;                            //分子,分母
public:
    Fraction(){}
    ~Fraction(){}
    Fraction(int fenzi,int fenmu);
    Fraction operator+(const Fraction &fra)const;
    Fraction operator-(const Fraction &fra)const;
    Fraction operator*(const Fraction &fra)const;
    Fraction operator/(const Fraction &fra)const;
    friend ostream& operator<<(ostream& out,const Fraction& fra);//i/o重载必须使用友元重载
    friend istream& operator>>(istream& in,const Fraction& fra);
    void setValue(int mu,int zi);
};
Fraction::Fraction(int zi,int mu):fenzi(zi),fenmu(mu){
    if(mu==0){
        cout<<"分母不能为0!"<<endl;        
        exit(0);
    }
}
ostream& operator<<(ostream& out,const Fraction &fra){    
    if(fra.fenmu<0)
        out<<"(-"<<fra.fenzi<<"/"<<-fra.fenmu<<")";        //负分数显示
    else if(fra.fenzi<0)
        out<<"(-"<<-fra.fenzi<<"/"<<fra.fenmu<<")";
    else if(fra.fenmu==1)
        out<<fra.fenzi;                                    //整数显示
    else
        out<<fra.fenzi<<"/"<<fra.fenmu;                    //正分数显示
    return out;
}

istream& operator>>(istream& in, Fraction& fra){
    int mu,zi;
    cout<<"请输入分母与分子"<<endl;
    in>>mu>>zi;
    fra.setValue(mu,zi);
    return in;
}
void Fraction::setValue(int mu,int zi){
    fenmu=mu;
    fenzi=zi;
}
Fraction Fraction::operator+(const Fraction &fra)const{
    int a=fenzi,b=fenmu;    //▲▲▲重点▲▲▲
    //一定要使用const,因为不用的话会使上次运算的数据赋值到下次的运算l-value中
    //因为const函数无法改变类的内部变量,所以要用替身,若此处重载改用friend重载
    //则可以有效地避免此问题,因为friend重载的操作符有两个引用的参数
    a=fra.fenmu*fenzi+fra.fenzi*fenmu;
    b*=fra.fenmu;    
    int g=GreatestCommonDivisor(a,b);
    if (g!=1)
    {
        a/=g;
        b/=g;
    }
    return Fraction(a,b);
}
Fraction Fraction::operator-(const Fraction &fra)const{
    int a=fenzi,b=fenmu;
    a=fra.fenmu*fenzi-fra.fenzi*fenmu;
    b*=fra.fenmu;    
    int g=GreatestCommonDivisor(a,b);
    if (g!=1)
    {
        a/=g;
        b/=g;
    }
    return Fraction(a,b);
}
Fraction Fraction::operator*(const Fraction &fra)const{
    int a=fenzi,b=fenmu;
    a*=fra.fenzi;
    b*=fra.fenmu;
    int g=GreatestCommonDivisor(a,b);
    if (g!=1)
    {
        a/=g;
        b/=g;
    }
    return Fraction(a,b);
}
Fraction Fraction::operator/(const Fraction &fra)const{
    return *this*Fraction(fra.fenmu,fra.fenzi);
}
void main()
{
    Fraction a(1,2);
    Fraction b(1,4);
    Fraction c=a+b;
    Fraction d=a-b;
    Fraction e=b-a;
    Fraction f=a*b;
    Fraction g=a/b;
    cout<<"a=1/2"<<endl
        <<"b=1/4"<<endl
        <<"a+b="<<c<<endl
        <<"a-b="<<d<<endl
        <<"b-a="<<e<<endl
        <<"a*b="<<f<<endl
        <<"a/b="<<g<<endl;
    Fraction h;
    cin>>h;
    cout<<h<<endl;
}
bubuko.com,布布扣

 

c++ Fraction类 操作符重载练习,布布扣,bubuko.com

c++ Fraction类 操作符重载练习

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/zoffy/p/3726882.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!