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

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

問題及程式碼:

/*
* Copyright (c) 2015, 煙臺大學計算機學院
* All rights reserved.
* 檔名稱:Project3.cpp
* 作    者:李楠
* 完成日期:2015年4月24日
* 版 本 號:v1.0
*
* 問題描述:(2)在(1)的基礎上,實現分數類中的物件和整型數的四則運算。分數類中的物件可以和整型數進行四則運算,且運算符合交換律。
             例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同樣,可以完成i+a, 45+a, a*27, 5/a等各種運算。

* 程式輸入:略
* 程式輸出:略
*/
#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);   //建構函式,初始化用
    void set(int nu=0,int de=1);    //置值,改變值時用
    void input();               //按照"nu/de"的格式,如"5/2"的形式輸入
    void simplify();            //化簡(使分子分母沒有公因子)
    void amplify(int n);        //放大n倍,如2/3放大5倍為10/3
    void output(int style=0);   //輸出:以8/6為例,style為0時,原樣輸出8/6;
                            //style為1時,輸出化簡後形式4/3;
                            //style為2時,輸出1(1/3)形式,表示一又三分之一;
                            //style為3時,用小數形式輸出,如1.3333;
                            //預設方式0
    CFraction operator+(const CFraction &c2);
    CFraction operator-(const CFraction &c2);
    CFraction operator*(const CFraction &c2);
    CFraction operator/(const CFraction &c2);
    CFraction operator+(const int a);
    CFraction operator-(const int a);
    CFraction operator*(const int a);
    CFraction operator/(const int a);
    friend CFraction operator+(const int a,const CFraction &c2);
    friend CFraction operator-(const int a,const CFraction &c2);
    friend CFraction operator*(const int a,const CFraction &c2);
    friend CFraction operator/(const int a,const CFraction &c2);
    bool operator > (CFraction &c1);
    bool operator < (CFraction &c1);
    bool operator >= (CFraction &c1);
    bool operator <= (CFraction &c1);
    bool operator == (CFraction &c1);
    bool operator != (CFraction &c1);
};
CFraction::CFraction(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cout<<"分母不能為0,初始化失敗!"<<endl;
        exit(0);
    }
}

void CFraction::set(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cout<<"分母不能置為0,更改失敗!"<<endl;
        exit(0);
    }
}

void CFraction::input()
{
    char p;
    cin>>nume>>p>>deno;
    if(p!='/')
    {
        cout<<"輸入不符合格式!"<<endl;
        exit(0);
    }
    else if(deno==0)
    {
        cout<<"分母不能為0,輸入失敗!"<<endl;
        exit(0);
    }
}

int gcd(int m,int n)
{
    int g;
    if(n==0)
        g=m;
    else
        g=gcd(n,m%n);
    return g;
}
void CFraction::simplify()
{
    int l;
    l=gcd(nume,deno);
    nume=nume/l;
    deno=deno/l;
}

void CFraction::amplify(int n)
{
    nume=nume*n;
}

void CFraction::output(int style)
{
    int a;
    float b;
    if(style==0)
        cout<<nume<<'/'<<deno<<endl;
    else if(style==1)
    {
        simplify();
        cout<<nume<<'/'<<deno<<endl;
    }
    else if(style==2)
    {
       a=nume/deno;
       cout<<a<<'('<<nume%deno<<'/'<<deno<<')'<<endl;
    }
    else if(style==3)
    {
        b=float(nume)/float(deno);
        cout<<b<<endl;
    }
    else
        cout<<"沒有此種形式!"<<endl;
}
CFraction CFraction::operator+(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno+c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator-(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno-c2.nume*deno;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator*(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.nume;
    c.deno=deno*c2.deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator/(const CFraction &c2)
{
    CFraction c;
    c.nume=nume*c2.deno;
    c.deno=deno*c2.nume;
    c.simplify();
    return c;
}
bool CFraction::operator > (CFraction &c1)
{
    CFraction c;
    c=*this-c1;
    if(c.nume/c.deno>0)
        return true;
    else
        return false;
}
bool CFraction::operator < (CFraction &c1)
{
    CFraction c;
    c=*this-c1;
    if(c.nume/c.deno<0)
        return true;
    else
        return false;
}
bool CFraction::operator >= (CFraction &c1)
{
    return !(*this<c1);
}
bool CFraction::operator <= (CFraction &c1)
{
    return !(*this>c1);
}
bool CFraction::operator == (CFraction &c1)
{
    if (*this!=c1)
        return false;
    else
        return true;
}
bool CFraction::operator != (CFraction &c1)
{
    if (*this>c1||*this<c1)
        return true;
    else
        return false;
}
CFraction CFraction::operator+(const int a)
{
    CFraction c;
    c.nume=nume+deno*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator-(const int a)
{
    CFraction c;
    c.nume=nume-deno*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator*(const int a)
{
    CFraction c;
    c.nume=nume*a;
    c.deno=deno;
    c.simplify();
    return c;
}
CFraction CFraction::operator/(const int a)
{
    CFraction c;
    c.nume=nume;
    c.deno=deno*a;
    c.simplify();
    return c;
}
CFraction operator+(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.nume+c2.deno*a;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator-(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.deno*a-c2.nume;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator*(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.nume*a;
    c.deno=c2.deno;
    c.simplify();
    return c;
}
CFraction operator/(const int a,const CFraction &c2)
{
    CFraction c;
    c.nume=c2.deno*a;
    c.deno=c2.nume;
    c.simplify();
    return c;
}
int main()
{
    CFraction cf;
    cout<<"預設值輸出:";
    cf.output();
    cout<<"改變值後輸出:";
    cf.set(6,4);
    cf.output(1);
    cout<<"輸入:";
    cf.input();
    cout<<"原樣輸出:";
    cf.output(0);
    cout<<"放大3倍:";
    cf.amplify(3);
    cf.output(0);
    cout<<"化簡形式:";
    cf.output(1);
    cout<<"真分數形式:";
    cf.output(2);
    cout<<"小數形式:";
    cf.output(3);
    CFraction x(4,5),y(-2,6);
    CFraction p;
    cout<<"分數:";
    cout<<"x=";
    x.output();
    cout<<"     y=";
    y.output();
    p=x+y;
    cout<<"x+y=";
    p.output();
    p=x-y;
    cout<<"x-y=";
    p.output();
    p=x*y;
    cout<<"x*y=";
    p.output();
    p=x/y;
    cout<<"x/y=";
    p.output();
    p=x*3;
    p=3*y;
    p=3+x;
    p=y+3;
    p=10-x;
    p=y-6;
    p=y/10;
    p=4/x;
    if (x>y) cout<<"x大於y"<<endl;
    if (x<y) cout<<"x小於y"<<endl;
    if (x==y) cout<<"x等於y"<<endl;
    if (x>=y) cout<<"x大於等於y"<<endl;
    if (x<=y) cout<<"x小於等於y"<<endl;
    if (x!=y) cout<<"x不等於y"<<endl;
    cout<<endl;
    return 0;
}


執行結果:

知識點總結:

有一部分沒有用output輸出,因為老賀說得好嘛~用單步執行測試更方便!而且涉及到二目的就要用友元函數了。

學習心得:

最開始寫分數類雛形的時候就想著如果將分數加減乘除該怎麼辦呢,現在一點點地解決了~很嗨森~