问题描述
已知一个有理数类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&);
测试
测试时主程序会输入四个整数a, b, c, d,表示两个分数a/b和c/d。要求输出最简分数以及两个分数相等和大小的比较结果。
样例输入
1 7 26 25
样例输出
zrf is:1/7; ssh is:26/25
(zrf==ssh) is:0; (zrf<ssh) is:1
蓝桥一道让人一脸懵逼的神题,一共改了4次代码把我提交的代码写在下面你们就知道我有多绝望了:
第一次:
#include<iostream>
using namespace std;
class zrf_Ratio {
public :
int a,b;
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&);
};
ostream& operator<<(ostream &zout,const zrf_Ratio &z) {
int x,y;
x=z.a;
y=z.b;
int tmp;
if(x<y) {
tmp=x;
x=y;
y=tmp;
}
do {
tmp=x%y;
x=y;
y=tmp;
} while(tmp!=0);
zout<<" is:"<<z.a/x<<"/"<<z.b/x;
return zout;
}
istream& operator>>(istream &zin,zrf_Ratio& z) {
int x,y;
zin>>x>>y;
z.a=x;
z.b=y;
return zin;
}
bool operator==(const zrf_Ratio& a, const zrf_Ratio& b) {
int ax,ay,bx,by;
ax=a.a;
ay=a.b;
bx=b.a;
by=b.b;
return ax*by==bx*ay;
}
bool operator<(const zrf_Ratio& a, const zrf_Ratio& b) {
int ax,ay,bx,by;
ax=a.a;
ay=a.b;
bx=b.a;
by=b.b;
return (ax*by-bx*ay)*(ay*by)<0;
}
main() {
zrf_Ratio zrf,ssh;
cin>>zrf>>ssh;
cout<<"zrf"<<zrf<<"; ssh"<<ssh<<endl;
cout<<"(zrf==ssh) is:";
if(zrf==ssh) cout<<"1 ";
else cout<<"0 ";
cout<<"(zrf<ssh) is:";
if(zrf<ssh) cout<<"1"<<endl;
else cout<<"0"<<endl;
}
他居然告诉我编译出错?喵喵喵?啥玩意?仔细一看,他只需提交构造函数部分,其他的他给补充。。。行,你是大爷,我把其他的都删了,再交一次.
第二次:
ostream& operator<<(ostream &zout,const zrf_Ratio &z){
int x,y;
x=z.num;
y=z.den;
int tmp;
if(x<y){
tmp=x;
x=y;
y=tmp;
}
do{
tmp=x%y;
x=y;
y=tmp;
}while(tmp!=0);
zout<<z.num/x<<"/"<<z.den/x;
return zout;
}
istream& operator>>(istream &zin,zrf_Ratio& z){
int x,y;
zin>>x>>y;
z.num=x;
z.den=y;
return zin;
}
bool operator==(const zrf_Ratio& a, const zrf_Ratio& b){
int ax,ay,bx,by;
ax=a.num;
ay=a.den;
bx=b.num;
by=b.den;
return ax*by==bx*ay;
}
bool operator<(const zrf_Ratio& a, const zrf_Ratio& b){
int ax,ay,bx,by;
ax=a.num;
ay=a.den;
bx=b.num;
by=b.den;
return (ax*by-bx*ay)*(ay*by)<0;
}
提示我运行出错?喵喵喵???
再定睛一看。。。MMP啊,他连化为最简分数都不用你做,它自带一个reduce()函数去化为最简分数。
第三次:
ostream& operator<<(ostream& os, const zrf_Ratio& zrf_Ratio) {
os << zrf_Ratio.num << "/" << zrf_Ratio.den;
return os;
}
istream& operator>>(istream& in, zrf_Ratio& zrf_Ratio) {
in >> zrf_Ratio.num >> zrf_Ratio.den;
return in;
}
bool operator==(const zrf_Ratio& zrf_Ratio1, const zrf_Ratio& zrf_Ratio2) {
if (zrf_Ratio1.num == zrf_Ratio2.num &&
zrf_Ratio1.den == zrf_Ratio2.den) {
return 1;
}
return 0;
}
bool operator<(const zrf_Ratio& zrf_Ratio1, const zrf_Ratio& zrf_Ratio2) {
if (zrf_Ratio1.num * 1.0 / zrf_Ratio1.den <
zrf_Ratio2.num * 1.0 / zrf_Ratio2.den) {
return 1;
}
return 0;
}
可算是过了,脑壳疼,圈钱杯无愧于蓝桥杯之名
——————/´ ¯/)
—————–/—-/
—————-/—-/
———–/´¯/’–’/´¯`·_
———-/’/–/—-/—–/¨¯
——–(’(———- ¯~/’–’)
———\————-’—–/
———-’’————_-·´
————\———–(
————-\———–