[cpp]
/*
* 程序的版權和版本聲明部分
* Copyright (c)2012, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 4 月 19 日
* 版本號: v1.0
* 輸入描述:無
* 問題描述:
* 程序輸出:
*/
#include<iostream>
#include<Cmath>
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(int nu,int de):nume(nu),deno(de){}
CFraction operator+(CFraction &c2);
CFraction operator-(CFraction &c2);
CFraction operator*(CFraction &c2);
CFraction operator/(CFraction &c2);
CFraction operator~();
bool operator > (CFraction &c2);
bool operator < (CFraction &c2);
bool operator == (CFraction &c2);
void simplify();
void display();
};
CFraction CFraction::operator+(CFraction &c2)
{
return CFraction(nume*c2.deno+deno*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator-(CFraction &c2)
{
return CFraction(nume*c2.deno-deno*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator*(CFraction &c2)
{
return CFraction(nume*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator/(CFraction &c2)
{
return CFraction(nume*c2.deno,deno*c2.nume);
}
CFraction CFraction::operator~()
{
return CFraction(deno,nume);
}
bool CFraction::operator > (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno>0)
return true;
else
return false;
}
bool CFraction::operator < (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno<0)
return true;
else
return false;
}
bool CFraction::operator == (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno==0)
return true;
else
return false;
}
// 分數化簡,使分子分母沒有公因子
void CFraction::simplify()
{
int n=gcd(deno, nume);
deno/=n; // 化簡
nume/=n;
}
// 求m,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:: display()
{
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction c1(3,8),c2(7,16),c;
cout<<"c1為:";
c1.display();
cout<<"c2為:";
c2.display();
cout<<"下面比較兩個數大小:\n";
if (c1>c2) cout<<"c1>c2"<<endl;
if (c1<c2) cout<<"c1<c2"<<endl;
if (c1==c2) cout<<"c1==c2"<<endl;
c=c1+c2;
cout<<"c1+c2=";
c.simplify();
c.display();
c=c1-c2;
cout<<"c1-c2=";
c.simplify();
c.display();
c=c1*c2;
cout<<"c1*c2=";
c.simplify();
c.display();
c=c1/c2;
cout<<"c1/c2=";
c.simplify();
c.display();
cout<<endl;
return 0;
}
/*
* 程序的版權和版本聲明部分
* Copyright (c)2012, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 4 月 19 日
* 版本號: v1.0
* 輸入描述:無
* 問題描述:
* 程序輸出:
*/
#include<iostream>
#include<Cmath>
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(int nu,int de):nume(nu),deno(de){}
CFraction operator+(CFraction &c2);
CFraction operator-(CFraction &c2);
CFraction operator*(CFraction &c2);
CFraction operator/(CFraction &c2);
CFraction operator~();
bool operator > (CFraction &c2);
bool operator < (CFraction &c2);
bool operator == (CFraction &c2);
void simplify();
void display();
};
CFraction CFraction::operator+(CFraction &c2)
{
return CFraction(nume*c2.deno+deno*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator-(CFraction &c2)
{
return CFraction(nume*c2.deno-deno*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator*(CFraction &c2)
{
return CFraction(nume*c2.nume,deno*c2.deno);
}
CFraction CFraction::operator/(CFraction &c2)
{
return CFraction(nume*c2.deno,deno*c2.nume);
}
CFraction CFraction::operator~()
{
return CFraction(deno,nume);
}
bool CFraction::operator > (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno>0)
return true;
else
return false;
}
bool CFraction::operator < (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno<0)
return true;
else
return false;
}
bool CFraction::operator == (CFraction &c2)
{
if(nume*c2.deno-deno*c2.nume,deno*c2.deno==0)
return true;
else
return false;
}
// 分數化簡,使分子分母沒有公因子
void CFraction::simplify()
{
int n=gcd(deno, nume);
deno/=n; // 化簡
nume/=n;
}
// 求m,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:: display()
{
cout<<nume<<"/"<<deno<<endl;
}
int main()
{
CFraction c1(3,8),c2(7,16),c;
cout<<"c1為:";
c1.display();
cout<<"c2為:";
c2.display();
cout<<"下面比較兩個數大小:\n";
if (c1>c2) cout<<"c1>c2"<<endl;
if (c1<c2) cout<<"c1<c2"<<endl;
if (c1==c2) cout<<"c1==c2"<<endl;
c=c1+c2;
cout<<"c1+c2=";
c.simplify();
c.display();
c=c1-c2;
cout<<"c1-c2=";
c.simplify();
c.display();
c=c1*c2;
cout<<"c1*c2=";
c.simplify();
c.display();
c=c1/c2;
cout<<"c1/c2=";
c.simplify();
c.display();
cout<<endl;
return 0;
}
運行結果: