1. 程式人生 > >分數類中的運算子過載(續)

分數類中的運算子過載(續)

問題及程式碼:

/*
 * Copyright (c) 2014, 煙臺大學計算機學院
 * All rights reserved.
 * 檔名稱:test.cpp
 * 作    者:李盈盈
 * 完成日期:2015年 05 月 10 日
 * 版 本 號:v1.0
 *
 * 問題描述:定義分數的一目運算子+和-,分別代表分數取正和求反,將~過載為分數的求倒數運算。
 * 輸入描述:輸入兩個分數。
 * 程式輸出:按要求輸出。
 */
#include <iostream>
#include <Cmath>
using namespace std;
class CFraction
{
private:
    int nume;
    int deno;
public:
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
    void simplify();
    friend istream &operator>>(istream &in,CFraction &x);
    friend ostream &operator<<(ostream &out,CFraction x);

    CFraction operator+(const CFraction &c);
    CFraction operator-(const CFraction &c);
    CFraction operator*(const CFraction &c);
    CFraction operator/(const CFraction &c);
    CFraction operator+();
    CFraction operator-();
    CFraction operator~();
    bool operator>(const CFraction &c);
    bool operator<(const CFraction &c);
    bool operator==(const CFraction &c);
    bool operator!=(const CFraction &c);
    bool operator>=(const CFraction &c);
    bool operator<=(const CFraction &c);
};
void CFraction::simplify()
{
    int m,n,r;
    n=fabs(deno);
    m=fabs(nume);
    while((r=m%n)!=0)
    {
        m=n;
        n=r;
    }
    deno/=n;
    nume/=n;
    if (deno<0)
    {
        deno=-deno;
        nume=-nume;
    }
}
istream &operator>>(istream &in,CFraction &x)
{
    char ch;
    while(1)
    {
        cin>>x.nume>>ch>>x.deno;
        if (x.deno==0)
            cerr<<"分母為0, 請重新輸入\n";
        else if(ch!='/')
            cerr<<"格式錯誤(形如m/n)! 請重新輸入\n";
        else
            break;
    }
    return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
    cout<<x.nume<<'/'<<x.deno;
    return cout;
}
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno+c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction:: operator-(const CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno-c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction:: operator*(const CFraction &c)
{
    CFraction t;
    t.nume=nume*c.nume;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction:: operator/(const CFraction &c)
{
    CFraction t;
    if (!c.nume) return *this;
    t.nume=nume*c.deno;
    t.deno=deno*c.nume;
    t.simplify();
    return t;
}
CFraction CFraction:: operator+()
{
    return *this;
}
CFraction CFraction:: operator-()
{
    CFraction x;
    x.nume=-nume;
    x.deno=deno;
    return x;
}
CFraction CFraction:: operator~()
{
    CFraction x;
    x.nume=deno;
    x.deno=nume;
    if(x.deno<0)
    {
        x.deno=-x.deno;
        x.nume=-x.nume;
    }
    return x;
}
bool CFraction::operator>(const CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume-c_nume)*common_deno>0) return true;
    return false;
}
bool CFraction::operator<(const CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((this_nume-c_nume)*common_deno<0) return true;
    return false;
}
bool CFraction::operator==(const CFraction &c)
{
    if (*this!=c) return false;
    return true;
}
bool CFraction::operator!=(const CFraction &c)
{
    if (*this>c || *this<c) return true;
    return false;
}
bool CFraction::operator>=(const CFraction &c)
{
    if (*this<c) return false;
    return true;
}
bool CFraction::operator<=(const CFraction &c)
{
    if (*this>c) return false;
    return true;
}

int main()
{
    CFraction x,y,s;
    cout<<"輸入x: ";
    cin>>x;
    cout<<"輸入y: ";
    cin>>y;
    s=+x+y;
    cout<<"+x+y="<<s<<endl;
    s=x-y;
    cout<<"x-y="<<s<<endl;
    s=x*y;
    cout<<"x*y="<<s<<endl;
    s=x/y;
    cout<<"x/y="<<s<<endl;
    cout<<"-x="<<-x<<endl;
    cout<<"+x="<<+x<<endl;
    cout<<"x的倒數: "<<~x<<endl;
    cout<<x;
    if (x>y) cout<<"大於";
    if (x<y) cout<<"小於";
    if (x==y) cout<<"等於";
    cout<<y<<endl;
    return 0;
}

執行結果: