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

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

/*
 * Copyright (c) 2014, 煙臺大學計算機學院
 * All rights reserved.
 * 檔名稱:test.cpp
 * 作    者:李曉凱
 * 完成日期:2015年 4 月 29 日
 * 版 本 號:v1.0
 *
 * 問題描述:實現分數中的運算子過載,在分數類中可以完成和分數的加減乘除(運算後化簡),比較(6中關係)的運算,。

 * 輸入描述:
 * 程式輸出:運算結果

 */

#include <iostream>
#include <Cmath>
using namespace std;
int gcd(int a,int b);
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int n=0,int d=1):nume(n),deno(d) {}
    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 n;
    n=gcd(nume,deno);
    deno/=n;
    nume/=n;
    if (deno<0)
    {
        deno=-deno;
        nume=-nume;
    }
}

int gcd(int a,int b)
{
    int m;
    while(b!=0)
    {
        m=a%b;
        a=b;
        b=m;
    }
    return a;
}
void CFraction::display()
{
    cout<<"("<<nume<<"/"<<deno<<")";
}

CFraction CFraction::operator + (const CFraction &c)
{
    CFraction d;
    d.nume=nume*c.deno+c.nume*deno;
    d.deno=deno*c.deno;
    d.simplify();
    return d;
}

CFraction CFraction:: operator - (const CFraction &c)
{
    CFraction d;
    d.nume=nume*c.deno-c.nume*deno;
    d.deno=deno*c.deno;
    d.simplify();
    return d;
}

CFraction CFraction:: operator * (const CFraction &c)
{
    CFraction d;
    d.nume=nume*c.nume;
    d.deno=deno*c.deno;
    d.simplify();
    return d;
}

CFraction CFraction:: operator / (const CFraction &c)
{
    CFraction d;
    if (c.nume==0)
        return *this;
    d.nume=nume*c.deno;
    d.deno=deno*c.nume;
    d.simplify();
    return d;
}

bool CFraction::operator > (const CFraction &c)
{
    int c1_nume,c2_nume,common_deno;
    c1_nume=nume*c.deno;
    c2_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((c1_nume-c2_nume)*common_deno>0)
        return true;
    return false;
}

bool CFraction::operator<(const CFraction &c)
{
    int c1_nume,c2_nume,common_deno;
    c1_nume=nume*c.deno;
    c2_nume=c.nume*deno;
    common_deno=deno*c.deno;
    if ((c1_nume-c2_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 c1(1,3),c2(-5,10),s;
    cout<<"分數:"<<endl<<"c1=";
    c1.display();
    cout<<endl;
    cout<<"c2=";
    c2.display();
    cout<<endl<<endl;
    s=c1+c2;
    cout<<"c1+c2=";
    s.display();
    cout<<endl;
    s=c1-c2;
    cout<<"c1-c2=";
    s.display();
    cout<<endl;
    s=c1*c2;
    cout<<"c1*c2=";
    s.display();
    cout<<endl;
    s=c1/c2;
    cout<<"c1/c2=";
    s.display();
    cout<<endl<<endl;
    c1.display();
    if (c1>c2) cout<<"大於";
    if (c1<c2) cout<<"小於";
    if (c1==c2) cout<<"等於";
    c2.display();
    cout<<endl;
    return 0;
}