马云演讲中国梦:C++的又一道编程问题,请各位大侠指教

来源:百度文库 编辑:高校问答 时间:2024/04/28 21:57:35
已知一个有理数类Zrf_Ratio,实现如下的操作符重载形式:
friend zrf_Ratio operator-(const zrf_Ratio&);
friend zrf_Ratio operator+(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator-(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator*(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator/(const zrf_Ratio&, const zrf_Ratio&);
测试用主函数:
int main()
{
zrf_Ratio zrf(1,7),ssh(26,65);
std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
std::cout<<"-zrf is:"<<-zrf;
std::cout<<"\n(zrf-ssh) is:"<<(zrf-ssh);
std::cout<<"\n(zrf+ssh) is:"<<(zrf+ssh) ;
std::cout<<"\n(zrf*ssh) is:"<<(zrf*ssh) ;
std::cout<<"\n(zrf/ssh) is:"<<(zrf/ssh) ;
return 0;
}
运行结果:
zrf is:1/7; ssh is:2/5
-zrf is:-1/7
(zrf-ssh) is:-9/35
(zrf+ssh) is:19/35
(zrf*ssh) is:2/35
(zrf/ssh) is:5/14

//又搞定了一道题目,呵呵
//这个我编译运行了,与你给的运行结果一模一样
//你可以直接拷贝过去使用了。
#include <iostream>
class zrf_Ratio;
std::ostream &operator<<(std::ostream &stream,zrf_Ratio op);
zrf_Ratio operator-(const zrf_Ratio&);
zrf_Ratio operator+(const zrf_Ratio&, const zrf_Ratio&);
zrf_Ratio operator-(const zrf_Ratio&, const zrf_Ratio&);
zrf_Ratio operator*(const zrf_Ratio&, const zrf_Ratio&);
zrf_Ratio operator/(const zrf_Ratio&, const zrf_Ratio&);
class zrf_Ratio
{
private:
//一些友元运算符的重载
friend zrf_Ratio operator-(const zrf_Ratio&);
friend zrf_Ratio operator+(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator-(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator*(const zrf_Ratio&, const zrf_Ratio&);
friend zrf_Ratio operator/(const zrf_Ratio&, const zrf_Ratio&);

friend std::ostream &operator<<(std::ostream &stream,zrf_Ratio op);
int top;
int bottom;
int show_b(int n,int m) //求最大公约数
{
int temp,r;
if(n<m)
{
temp=n;
n=m;
m=temp;
}
while(m)
{
r=n%m;
n=m;
m=r;
}
return n;
}
public:
zrf_Ratio(){};//缺省构造函数
zrf_Ratio(int itop,int ibottom):top(itop),bottom(ibottom)//构造函数
{
go_easy();
}
//化简函数
void go_easy()
{
int op1(top),op2(bottom);
if(op1<0)
op1=-op1;
if(op2<0)
op2=-op2;
int temp=show_b(op1,op2);
top/=temp;
bottom/=temp;
}
zrf_Ratio& operator=(const zrf_Ratio &op)
{
top=op.top;
bottom=op.bottom;
return *this;
}
};
zrf_Ratio operator-(const zrf_Ratio&op)
{
return zrf_Ratio(-op.top,op.bottom);
}

zrf_Ratio operator+(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return zrf_Ratio(op1.top*op2.bottom+op2.top*op1.bottom,op1.bottom*op2.bottom);
}
zrf_Ratio operator-(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return zrf_Ratio(op1.top*op2.bottom-op2.top*op1.bottom,op1.bottom*op2.bottom);
}
zrf_Ratio operator*(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return zrf_Ratio(op1.top*op2.top,op1.bottom*op2.bottom);
}
zrf_Ratio operator/(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return zrf_Ratio(op1.top*op2.bottom,op1.bottom*op2.top);
}

std::ostream &operator<<(std::ostream &stream,zrf_Ratio op)
{
stream<<op.top<<"/"<<op.bottom;
return stream;
}
int main()
{
zrf_Ratio zrf(1,7),ssh(26,65);
std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
std::cout<<"-zrf is:"<<-zrf;
std::cout<<"\n(zrf-ssh) is:"<<(zrf-ssh);
std::cout<<"\n(zrf+ssh) is:"<<(zrf+ssh) ;
std::cout<<"\n(zrf*ssh) is:"<<(zrf*ssh) ;
std::cout<<"\n(zrf/ssh) is:"<<(zrf/ssh) ;

return 0;

}