* (程式頭部注釋開始)
* 程式的版權和版本聲明部分
* Copyright (c) 2011, 煙台大學計算機學院學生
* All rights reserved.
* 檔案名稱:
* 作 者: 馮珍珍
* 完成日期: 2012 年 4 月 10 日
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述: 實作分數類中的運算符重載,在分數類中可以完成分數的加減乘除(運算後再化簡)、求反、比較(6種關系)的運算。
* 程式輸出:
* 程式頭部的注釋結束
*/
//實作分數類中的運算符重載,在分數類中可以完成分數的加減乘除(運算後再化簡)、求反、比較(6種關系)的運算。
#include<iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//構造函數及運算符重載的函數聲明
CFraction(int m=0 ,int d=0):nume(m),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); //兩個分數相除,結果要化簡
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);
};
//求最大公約數的函數
int gcd(int x,int y)
{
int r;
while(y!=0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
// 分數化簡
void CFraction::simplify() //化簡(使分子分母沒有公因子)
{
int c=gcd(nume,deno);
nume=nume/c;
deno=deno/c;
}
//顯示分數
void CFraction::display()
{
cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}
// 分數相加
CFraction CFraction::operator+(const CFraction &c)
{
CFraction c1;
c1.nume=nume*c.deno+c.nume*deno; //當分母不同時,進行分子分母分别相乘并相加求出分數相加後的分子
c1.deno=deno*c.deno; //求出相加後的分母
c1.simplify(); //調用simplify進行分數化簡
return c1;
}
// 分數相減
CFraction CFraction:: operator-(const CFraction &c)
{
CFraction c1;
c1.nume=nume*c.deno-c.nume*deno; //當分母不同時,進行分子分母分别相乘并相減求出分數相減後的分子
c1.deno=deno*c.deno; //求出相減後的分母
c1.simplify(); //調用simplify進行分數化簡
return c1;
}
// 分數相乘 (分子分母分别相乘)
CFraction CFraction:: operator*(const CFraction &c)
{
CFraction c1;
c1.nume=nume*c.nume;
c1.deno=deno*c.deno;
c1.simplify();
return c1;
}
// 分數相除
CFraction CFraction:: operator/(const CFraction &c)
{
CFraction c1;
if (!c.nume) return *this;
c1.nume=nume*c.deno;
c1.deno=deno*c.nume;
c1.simplify();
return c1;
}
// 分數取正号
CFraction CFraction:: operator+()
{
return *this;
}
// 分數取負号
CFraction CFraction:: operator-()
{
CFraction c;
c.nume=-nume;
c.deno=deno;
return c;
}
// 分數比較大小
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&&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 a(1,3),b(-5,10),c;
// cout<<"兩個分數為:a=1/3 ,b=-5/10"<<endl;
cout<<"兩個分數分别為:"<<endl;
a.display();
b.display();
c=a+b;
cout<<"分數相加為:"<<"a+b=";
c.display();
c=a-b;
cout<<"分數相減為:"<<"a-b=";
c.display();
c=a*b;
cout<<"分數相乘為:"<<"a*b=";
c.display();
c=a/b;
cout<<"分數相除為:"<<"a/b=";
c.display();
c=-c;
cout<<"c的相反數為:" ;
c.display();
a.display();
b.display();
if (a>b) cout<<"a>b"<<endl;
if (a<b) cout<<"a<b"<<endl;
if (a==b)cout<<"a=b"<<endl;
cout<<endl;
system("pause");
return 0;
}
小結:運算符的重載這次這周進行了大量的應用,這次任務過程中出現了一個小錯誤,構造函數定義時沒有給變量賦初值,導緻出現後面函數定義時所有的CFraction c;出現no appropriate default constructor available錯誤,百度結果顯示(C++中,當你自己寫了有參數的構造函數的時候,編譯器就不會再自動提供無參數的預設構造函數,但是給參數指派後就表示一個預設的構造函數,是以可以定義類對象)也就是說定義類對象是,類的構造函數必須有一個預設的才可以;