1. 程式人生 > >第八週 專案三(1) 分數類中的運算子過載

第八週 專案三(1) 分數類中的運算子過載

問題及程式碼:

/*
* Copyright (c) 2015, 煙臺大學計算機學院
* All rights reserved.
*檔名稱:test.cpp
* 作    者:曾建強
* 完成日期:2015年5月14日
* 版 本 號: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();
    void display();
    CFraction operator+(const CFraction &c);  //兩個分數相加,結果要化簡
    CFraction operator-(const CFraction &c);  //兩個分數相減,結果要化簡
    CFraction operator*(const CFraction &c);  //兩個分數相乘,結果要化簡
    CFraction 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);
    bool operator<=(const CFraction &c);
};

// 分數化簡
void CFraction::simplify()
{
    int m,n,r;
    m=fabs(deno);             //此處存在bug,請參看評論1樓
    n=fabs(nume);
    while(r=m%n)  // 求m,n的最大公約數
    {
        m=n;
        n=r;
    }
    deno/=n;     // 化簡
    nume/=n;
    if (deno<0)  // 將分母轉化為正數
    {
        deno=-deno;
        nume=-nume;
    }
}

//顯示分數
void CFraction::display()
{
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}

// 分數相加
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;
}

// 分數比較大小
bool CFraction::operator>(const CFraction &c)
{
    int this_nume,c_nume,common_deno;
    this_nume=nume*c.deno;        // 計算分數通分後的分子,同分母為deno*c.deno
    c_nume=c.nume*deno;
    common_deno=deno*c.deno;
    //if (this_nume>c_nume) return true; 無法應對common_deno<0的情形
    //下面的語句更簡練的一種寫法if ((this_nume-c_nume)*common_deno>0) return true;
    if ((this_nume>c_nume&&common_deno>0)||(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(1,3),y(-5,10),s;
    cout<<"分數x=1/3      y=-5/10"<<endl;
    s=x+y;
    cout<<"x+y=";
    s.display();
    s=x-y;
    cout<<"x-y=";
    s.display();
    s=x*y;
    cout<<"x*y=";
    s.display();
    s=x/y;
    cout<<"x/y=";
    s.display();

    x.display();
    if (x>y) cout<<"大於"<<endl;
    if (x<y) cout<<"小於"<<endl;
    if (x==y) cout<<"等於"<<endl;
    y.display();
    cout<<endl;
    return 0;
}

執行結果: