|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 cyshhm 于 2013-11-19 22:15 编辑
#include<iostream>
#include<string>
class yls//有理数的计算
{
int a,b;
yls jhfs(const yls &sr);
public:
yls();
yls(int j);
yls(int j,int k);
yls operator+(const yls &sr);
yls operator-(const yls &sr);
yls operator*(const yls &sr);
yls operator/(const yls &sr);
std::ostream& operator<<(std::ostream& oo,const yls &sr);//这里我就是想把输出流操作符重载在类中的 为了就是明白 输出流重载的参数的真正目的
};
std::ostream& yls::operator<<(std::ostream& oo,const yls &sr)
{
oo<<sr.a<<"/"<<sr.b;
return oo;
}
yls yls::jhfs(const yls &sr)
{
int i,j,k;
yls c;
i=sr.a;
j=sr.b;
if(j<0)
{
c.a=-i;
c.b=j=-j;
}
else
{
c.a=i;
c.b=j;
}
i=i>0?i:-i;
while(i)
{
k=j%i;
j=i;
i=k;
}
c.a/=j;
c.b/=j;
return c;
}
yls yls::operator+(const yls &sr)
{
yls c;
c.a=b*sr.a + a*sr.b;
c.b=sr.b*b;
return c;
}
yls yls::operator-(const yls &sr)
{
yls c;
c.a=sr.a*b-a*sr.b;
c.b=sr.b*b;
return c;
}
yls yls::operator*(const yls &sr)
{
yls c;
c.a=sr.a*a;
c.b=sr.b*b;
return c;
}
yls yls::operator/(const yls &sr)
{
yls c;
c.a=sr.a*b;
c.b=sr.b*a;
return c;
}
yls::yls(int j,int k)
{
this->a=j;
this->b=k;
}
yls::yls()
{
this->a=0;
this->b=1;
}
yls::yls(int j)
{
this->a=j;
this->b=1;
}
|
|