/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2015年4月29日
*版本号:v1.0
*
*问题描述:利用运算符重载进行分数的大小比较和计算
*/
#include <iostream>
using namespace std;
class cFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
cFraction (int nu=0, int de=0);
void set(int nu=0, int de=0);
void input();
void display();
//构造函数及运算符重载的函数声明
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+(int &i,cFraction &c2);
friend cFraction operator-(int &i,cFraction &c2);
friend cFraction operator*(int &i,cFraction &c2);
friend cFraction operator/(int &i,cFraction &c2);
friend cFraction operator+(cFraction &c1,int &i);
friend cFraction operator-(cFraction &c1,int &i);
friend cFraction operator*(cFraction &c1,int &i);
friend cFraction operator/(cFraction &c1,int &i);
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);
};
cFraction::cFraction (int nu, int de)
{
nume=nu;
deno=de;
}
void cFraction::set(int nu, int de)
{
nume=nu;
deno=de;
}
void cFraction::input()
{
int m,n;
char s;
cout<<"按照 m/n 的样例输入:";
cin>>m>>s>>n;
while(s!='/'||n==0)
{
cout<<"输入格式错误或分母为0,请重新输入!"<<endl;
continue;
}
set(m,n);
}
void cFraction::display()
{
int x,y,g;
x=nume;
y=deno;
if (x<y)
{
g=x;
x=y;
y=g;
}
while(g=x%y) // 求m,n的最大公约数
{
x=y;
y=g;
}
set(nume/y,deno/y);
cout<<deno<<"/"<<nume<<endl;
}
//构造函数及运算符重载的函数声明
cFraction operator+(cFraction &c1,cFraction &c2)
{
if(c1.nume!=c2.nume)
return cFraction(c1.deno*c2.nume+c2.deno*c1.nume,c1.nume*c2.nume);
else
return cFraction(c1.deno+c2.deno,c1.nucFraction::me);
}
cFraction operator-(cFraction &c1,cFraction &c2)
{
if(c1.nume!=c2.nume)
return cFraction(c1.deno*c2.nume-c2.deno*c1.nume,c1.nume*c2.nume);
else
return cFraction(c1.deno-c2.deno,c1.nume);
}
cFraction operator*(cFraction &c1,cFraction &c2)
{
return cFraction(c1.deno*c2.deno,c1.nume*c2.nume);
}
cFraction operator/(cFraction &c1,cFraction &c2)
{
return cFraction(c1.deno*c2.nume,c1.nume*c2.deno);
}
cFraction operator+(int &i,cFraction &c2)
{
return cFraction (c2.deno+i*c2.nume,c2.nume);
}
cFraction operator-(int &i,cFraction &c2)
{
return cFraction (i*c2.nume-c2.deno,c2.nume);
}
cFraction operator*(int &i,cFraction &c2)
{
return cFraction(i*c2.deno,c2.nume);
}
cFraction operator/(int &i,cFraction &c2)
{
return cFraction(i*c2.nume,c2.deno);
}
cFraction operator+(cFraction &c1,int &i)
{
return cFraction (c1.deno+i*c1.nume,c1.nume);
}
cFraction operator-(cFraction &c1,int &i)
{
return cFraction (c1.deno-i*c1.nume,c1.nume);
}
cFraction operator*(cFraction &c1,int &i)
{
return cFraction(i*c1.deno,c1.nume);
}
cFraction operator/(cFraction &c1,int &i)
{
return cFraction(c1.deno,i*c1.nume);
}
bool cFraction::operator>(const cFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
if((this_nume-c_nume)*common_deno>0) return true;
else 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;
if((this_nume-c_nume)*common_deno<0) return true;
else return false;
}
bool cFraction::operator==(const cFraction &c)
{
if(!(*this>c||*this<c)) return true;
else return false;
}
bool cFraction::operator!=(const cFraction &c)
{
if(*this>c||*this<c) return true;
else return false;
}
bool cFraction::operator>=(const cFraction &c)
{
if(*this<c) return false;
else return true;
}
bool cFraction::operator<=(const cFraction &c)
{
if(*this>c) return true;
else return false;
}
int main()
{
cFraction x(1,3),y(-5,10),s;
cout<<"分数x=1/3 y=-5/10"<<endl;
s=x+y;
cout<<"x+y=";
s.display();
s=x-y;
cout<<"x-y=";
s.display();
s=x*y;
cout<<"x*y=";
s.display();
s=x/y;
cout<<"x/y=";
s.display();
cout<<endl;
x.display();
if (x>y) cout<<"大于"<<endl;
if (x<y) cout<<"小于"<<endl;
if (x==y) cout<<"等于"<<endl;
y.display();
cout<<endl;
return 0;
}
分数大小比较不太熟悉,似懂非懂。。。。