C++的一道编程问题,请各位大侠指教
已知一个有理数类Zrf_Ratio,实现如下的操作符重载形式:friendstd::ostream&operator<<(std::ostream&,constzrf_R...
已知一个有理数类Zrf_Ratio,实现如下的操作符重载形式:
friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
friend std::istream& operator>>(std::istream&, zrf_Ratio&);
friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);
friend bool 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==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) ;
return 0;
}
运行结果:
zrf is:1/7; ssh is:2/5
(zrf==ssh) is:0; (zrf<ssh) is:1
我是初学者,各位大侠帮个忙 展开
friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
friend std::istream& operator>>(std::istream&, zrf_Ratio&);
friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);
friend bool 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==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) ;
return 0;
}
运行结果:
zrf is:1/7; ssh is:2/5
(zrf==ssh) is:0; (zrf<ssh) is:1
我是初学者,各位大侠帮个忙 展开
3个回答
展开全部
//这道题目作出来了,和楼主给的运行结果完全相符
//呵呵,楼主这次题目我全给出可编译的代码,要给分哦!
//只要将代码拷贝就可以了,我已经编译运行成功!
#include <iostream>
class zrf_Ratio;
std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
std::istream& operator>>(std::istream&, zrf_Ratio&);
bool operator==(const zrf_Ratio&, const zrf_Ratio&);
bool operator<(const zrf_Ratio&, const zrf_Ratio&);
class zrf_Ratio
{
private:
//一些友元运算符的重载
friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
friend std::istream& operator>>(std::istream&, zrf_Ratio&);
friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);
friend bool operator<(const zrf_Ratio&, const zrf_Ratio&);
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;
}
};
std::ostream &operator<<(std::ostream &stream,const zrf_Ratio& op)
{
stream<<op.top<<"/"<<op.bottom;
return stream;
}
std::istream& operator>>(std::istream &stream, zrf_Ratio& op)
{
stream>>op.top>>op.bottom;
return stream;
}
bool operator==(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return ((op1.top==op2.top)&&(op1.bottom==op2.bottom));
}
bool operator<(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return ((op1.top*op2.bottom)<(op2.top*op1.bottom));
}
int main()
{
zrf_Ratio zrf(1,7),ssh(26,65);
std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
std::cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) ;
return 0;
}
//呵呵,楼主这次题目我全给出可编译的代码,要给分哦!
//只要将代码拷贝就可以了,我已经编译运行成功!
#include <iostream>
class zrf_Ratio;
std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
std::istream& operator>>(std::istream&, zrf_Ratio&);
bool operator==(const zrf_Ratio&, const zrf_Ratio&);
bool operator<(const zrf_Ratio&, const zrf_Ratio&);
class zrf_Ratio
{
private:
//一些友元运算符的重载
friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);
friend std::istream& operator>>(std::istream&, zrf_Ratio&);
friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);
friend bool operator<(const zrf_Ratio&, const zrf_Ratio&);
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;
}
};
std::ostream &operator<<(std::ostream &stream,const zrf_Ratio& op)
{
stream<<op.top<<"/"<<op.bottom;
return stream;
}
std::istream& operator>>(std::istream &stream, zrf_Ratio& op)
{
stream>>op.top>>op.bottom;
return stream;
}
bool operator==(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return ((op1.top==op2.top)&&(op1.bottom==op2.bottom));
}
bool operator<(const zrf_Ratio&op1, const zrf_Ratio&op2)
{
return ((op1.top*op2.bottom)<(op2.top*op1.bottom));
}
int main()
{
zrf_Ratio zrf(1,7),ssh(26,65);
std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;
std::cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) ;
return 0;
}
展开全部
//又搞定了一道题目,呵呵
//这个我编译运行了,与你给的运行结果一模一样
//你可以直接拷贝过去使用了。
#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;
}
//这个我编译运行了,与你给的运行结果一模一样
//你可以直接拷贝过去使用了。
#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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询