/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 臧鹏
* 完成日期: 2012 年 4 月 16 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:实现分数类中的<<和>>运算符重载,实现分数的输入和输出
* 程序头部的注释结束
*/
#include<iostream>
using namespace std;
int gcd(int x,int y) ;//声明为普通函数
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Set(int nu=0,int de=1); //置值,改变值时用
CFraction operator+(CFraction &t);
CFraction operator-(CFraction &t);
CFraction operator*(CFraction &t);
CFraction operator/(CFraction &t);
friend CFraction operator-(CFraction &t);//取反
bool operator>(CFraction &t);
bool operator<(CFraction &t);
bool operator>=(CFraction &t);
bool operator<=(CFraction &t);
bool operator==(CFraction &t);
bool operator!=(CFraction &t);
void Simplify(int n); //化简(使分子分母没有公因子)
friend istream & operator >>(istream &in,CFraction &t);
friend ostream & operator <<(ostream &out,CFraction &t);
};
CFraction::CFraction(int nu,int de)
{
nume = nu;
deno = de;
}
void CFraction::Set(int nu,int de) //置值,改变值时用
{
if(de !=0)
{
nume = nu;
deno = de;
}
else
{
cout<<"分母不能为零"<<endl;
exit(0);
}
nume = nu;
deno = de;
}
void CFraction::Simplify(int n) //化简(使分子分母没有公因子)
{
n = gcd(nume,deno);
nume = nume/n;
deno = deno/n;
}
int gcd(int x,int y) //求公约数的函数
{
int r;
while( y!= 0)
{
r = x%y;
x = y;
y = r;
}
return x;
}
CFraction CFraction:: operator+(CFraction &t)
{
CFraction cf;
if(deno==t.deno) //分母相同时比较分子
{
cf.deno=deno;
cf.nume=nume+t.nume;
}
else //否则同分比较分子
{
cf.deno=deno*t.deno;
cf.nume=nume*t.deno+deno*t.nume;
}
return cf;
}
CFraction CFraction:: operator-(CFraction &t)
{
CFraction cf;
if(deno==t.deno)
{
cf.deno=deno;
cf.nume=nume-t.nume;
}
else
{
cf.deno=deno*t.deno;
cf.nume=t.deno*nume-t.nume*deno;
}
return cf;
}
CFraction CFraction:: operator*(CFraction &t)
{
CFraction cf;
cf.deno = deno*t.deno;
cf.nume = nume*t.nume;
return cf;
}
CFraction CFraction:: operator/(CFraction &t)
{
CFraction cf;
cf.deno = deno*t.nume;
cf.nume = nume*t.deno;
return cf;
}
CFraction operator-(CFraction &t)//取反
{
CFraction cf;
cf.deno = -t.deno;
cf.nume = t.nume;
return(cf);
}
bool CFraction::operator>(CFraction &t)
{
if(nume*t.deno>t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator<(CFraction &t)
{
if(nume*t.deno<t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator>=(CFraction &t)
{
if(nume*t.deno>t.nume*deno||nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator<=(CFraction &t)
{
if(nume*t.deno<t.nume*deno||nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator==(CFraction &t)
{
if(nume*t.deno==t.nume*deno)
return true;
else
return false;
}
bool CFraction::operator!=(CFraction &t)
{
if(!operator==(t))
return true;
else
return false;
}
istream & operator >>(istream &in,CFraction &t)
{
char a;
while(1)
{
cout<<"请输入分数(形式 x/x):"<<endl;
cin>>t.nume>>a>>t.deno;
if(a != '/'||t.deno ==0)//注意符号不对和分母为0的情况
{
cout<<"格式不正确,请重新输入:"<<endl;
continue;
}
else break;
}
return cin;
}
ostream & operator <<(ostream &out,CFraction &t)//在输出时进行化简
{
if(t.nume ==0)
{
cout<<"0"<<endl;
}
else
{
int m = gcd(t.nume,t.deno);
cout<<(t.nume/m)<<'/'<<(t.deno/m)<<endl;
}
return out;
}
int main ()
{
CFraction cf1,cf2,cf3;
cin>>cf1;
cout<<"cf1 = "<<cf1;
cin>>cf2;
cout<<"cf2 = "<<cf2;
cout<<"cf1*cf2= "<<cf1*cf2;
cout<<"cf1-cf2= "<<cf1-cf2;
cout<<"cf1+cf2= "<<cf1+cf2;
cout<<"cf1/cf2= "<<cf1/cf2;
cout<<"-cf1 = "<<-cf1;
if (cf1>cf2) cout<<"cf1>cf2"<<endl;
if (cf1<cf2) cout<<"cf1<cf2"<<endl;
if (cf1==cf2) cout<<"cf1=cf2"<<endl;
if (cf1!=cf2) cout<<"cf1≠cf2"<<endl;
if (cf1>=cf2) cout<<"cf1≥cf2"<<endl;
if (cf1<=cf2) cout<<"cf1≤cf2"<<endl;
system("pause");
return 0;
}
经验积累:
程序中的 gcd() 求最大公约数的函数要声明为一般函数,因为程序中的友元函数和成员函数都要用到,再就是输入函数需要进行认真的判断。