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

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

問題及程式碼:

/*
*Copyright (c)2014,煙臺大學計算機與控制工程學院
*All rights reserved.
*檔名稱: 分數類中的運算子過載  .cpp
*作    者:白雲飛
*完成日期:2015年4月25日
*版 本 號:v1.0
*
*問題描述:實現分數類中的運算子過載,在分數類中可以完成分數的加減乘除(運算後再化簡)、比較(6種關係)的運算。
*程式輸入:分子和分母
*程式輸出:計算結果
*/
#include <iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int gcd(int m, int n);
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1);   //建構函式,初始化用
    CFraction operator+( CFraction &c);
    CFraction operator-( CFraction &c);
    CFraction operator*( CFraction &c);
    CFraction operator/( CFraction &c);
    bool operator>(CFraction &c);
    bool operator<(CFraction &c);
    bool operator>=(CFraction &c);
    bool operator<=(CFraction &c);
    bool operator==(CFraction &c);
    bool operator!=(CFraction &c);
    void input();               //按照"nu/de"的格式,如"5/2"的形式輸入
    void simplify();            //化簡(使分子分母沒有公因子)
    void output();
};
CFraction::CFraction(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cerr<<"輸入錯誤!";
        exit(0);
    }
}
CFraction CFraction::operator+( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno+c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator-( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno-c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator*( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.nume;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator/( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno;
    t.deno=deno*c.nume;
    t.simplify();
    return t;

}
bool CFraction::operator>(CFraction &c)
{
    int nume1,nume2;
    nume1=nume*c.deno;
    nume2=c.nume*deno;
    if(nume1-nume2>0) return true;
    else return false;
}
bool CFraction::operator<(CFraction &c)
{
    int nume1,nume2;
    nume1=nume*c.deno;
    nume2=c.nume*deno;
    if(nume1-nume2<0) return true;
    else return false;
}
bool CFraction::operator>=(CFraction &c)
{
    return !(*this<c);
}
bool CFraction::operator<=(CFraction &c)
{
    return !(*this>c);
}
bool CFraction::operator==(CFraction &c)
{
    if(*this!=c) return false;
    else return true;
}
bool CFraction::operator!=(CFraction &c)
{
    if (*this>c || *this<c) return true;
    return false;
}
void CFraction::input()
{
    int nu,de;
    char m;
    cout<<"請輸入b分數(分子/分母):";
    cin>>nu>>m>>de;
    if(m!='/')
        cout<<"輸入格式錯誤";
    else if(de==0)
        cout<<"分母為零,輸入錯誤.";
    else
    {
        nume=nu;
        deno=de;
    }
}
void CFraction::simplify()
{
    int n=gcd(deno, nume);
    deno/=n;     // 化簡
    nume/=n;
}
int gcd(int m, int n) //這個函式可以定義為類的成員函式,也可以為一般函式
{
    int r;
    if (m<n)
    {
        r=m;
        m=n;
        n=r;
    }
    while(r=m%n)  // 求m,n的最大公約數
    {
        m=n;
        n=r;
    }
    return n;
}
void CFraction::output()
{
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}
int main()
{
    CFraction a(10,12),b,c;
    cout<<"a=10/12 "<<endl;
    b.input();
    if (a>b) cout<<"a>b"<<endl;
    if (a<b) cout<<"a<b"<<endl;
    if (a>=b) cout<<"a>=b"<<endl;
    if (a<=b) cout<<"a<=b"<<endl;
    if (a==b) cout<<"a=b"<<endl;
    if (a!=b) cout<<"a!=b"<<endl;
    c=a+b;
    cout<<"a+b=";
    c.output();
    c=a-b;
    cout<<"a-b=";
    c.output();
    c=a*b;
    cout<<"a*b=";
    c.output();
    c=a/b;
    cout<<"a/b=";
    c.output();
    return 0;
}

執行結果:


學習心得:

在寫bool型判斷a!=b的運算子過載函式時,寫的是if(*this!=c)return true;else return false;一直是錯的,後來單步到那步才知道是這錯了,後來改成(*this>c||*this<c)return true;才改對。