01./*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2013, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 文件名称:CFraction .cpp
06.* 作 者:赵冠哲
07.* 完成日期:2013年4月19日
08.* 版本号: v1.0
09.* 输入描述:
10.* 问题描述:
11.*/
#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
int gcd(int m, int n);
public:
//构造函数及运算符重载的函数声明
CFraction(int n = 1, int d = 1){if (d == 0) return;nume = n, deno = d;}
friend CFraction operator+(CFraction &c1, CFraction &c2);
friend CFraction operator-(CFraction &c1, CFraction &c2);
friend CFraction operator*(CFraction &c1, CFraction &c2);
friend CFraction operator/(CFraction &c1, CFraction &c2);
friend 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 simplify();
void display();
};
void CFraction::simplify()
{
int n=gcd(deno, nume);
deno/=n; // 化简
nume/=n;
}
int CFraction::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;
}
CFraction operator+(CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno=c1.deno*c2.deno;
c.nume=c1.nume*c2.deno+c2.nume*c1.deno;
c.simplify();
return c;
}
CFraction operator-(CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno=c1.deno*c2.deno;
c.nume=c1.nume*c2.deno-c2.nume*c1.deno;
c.simplify();
return c;
}
CFraction operator*(CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno=c1.deno*c2.deno;
c.nume=c1.nume*c2.nume;
c.simplify();
return c;
}
CFraction operator/(CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno=c1.deno*c2.nume;
c.nume= c1.nume*c2.deno;
c.simplify();
return c;
}
CFraction operator-(CFraction &c)
{
return CFraction(-c.nume,c.deno);
}
bool CFraction::operator>(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno>1)
return true;
else
return false;
}
bool CFraction::operator<(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno<1)
return true;
else
return false;
}
bool CFraction::operator>=(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno>=1)
return true;
else
return false;
}
bool CFraction::operator<=(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno<=1)
return true;
else
return false;
}
bool CFraction::operator==(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno==1)
return true;
else
return false;
}
bool CFraction::operator!=(CFraction &c)
{
CFraction cf(*this/c);
if (cf.nume/cf.deno!=1)
return true;
else
return false;
}
//重载函数的实现及用于测试的main()函数
int main()
{
CFraction c1(3, 8), c2(2, 8), c3(1, 5);
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
cout << "c3=";
c3.display();
if (c1 > c2) cout << "c1 > c2" << endl;
if (c2 < c1) cout << "c2 < c1" << endl;
if (c2 >= c3) cout << "c2 >= c3" << endl;
if (c2 <= c3) cout << "c2 <= c3" << endl;
if (c2 == c3) cout << "c2 == c3" << endl;
if (c1 != c2) cout << "c1 != c2" << endl;
cout << "c1+c2=";
(c1 + c2).display();
cout << "c1-c2=";
(c1 - c2).display();
cout << "c1*c2=";
(c1 * c2).display();
cout << "c1/c2=";
(c1 / c2).display();
c3= -c3;
c3.display();
}
运行结果: