求大神告诉我我哪里错了
#include
#include
using namespace std;
class Fraction {
private:
int x;
int y;
public:
Fraction();
Fraction(int x, int y);
int get();
bool operator<(const Fraction&);
bool operator<=(const Fraction&);
bool operator>(const Fraction&);
bool operator>=(const Fraction&);
bool operator!=(const Fraction&);
friend std::stringstream& operator >>(std::stringstream& os, Fraction& rhs);
friend std::istream& operator >>(std::istream& is, Fraction& rhs);
friend std::ostream& operator>>(std::ostream& rs, Fraction& lhs);
};
Fraction::Fraction() {
this->x = 0;
this->y = 0;
}
Fraction::Fraction(int x, int y) {
this->x = x;
this->y = y;
}
int Fraction::get() {
return x/y;
}
std::ostream& operator<<(std::ostream& rs, Fraction& lhs) {
return rs;
}
std::stringstream& operator<<(std::stringstream& os, Fraction& rhs) {
os << rhs.get();
return os;
}
std::istream& operator>>(std::istream& is, Fraction& rhs) {
int a, b;
is >> a >> b;
to_string(a);
to_string(b);
rhs = Fraction(a, b);
return is;
}
bool Fraction::operator<(const Fraction& rhs) {
if ((x/y) < (rhs.x/rhs.y)) return true;
else return false;
}
bool Fraction::operator<=(const Fraction& rhs) {
if ((x/y) < (rhs.x/rhs.y) || (x/y) ==(rhs.x/rhs.y)) return true;
else return false;
}
bool Fraction::operator>(const Fraction& rhs) {
if ((x/y) > (rhs.x/rhs.y)) return true;
else return false;
}
bool Fraction::operator>=(const Fraction& rhs) {
if ((x/y) > (rhs.x/rhs.y) || (x/y) == (rhs.x/rhs.y)) return true;
else return false;
}
bool Fraction::operator!=(const Fraction& rhs) {
if ((x/y) != (rhs.x/rhs.y)) return true;
else return false;
}
int main() {
Fraction a, b;
cin >> a >> b;
cout << a << " " << b;
if (a < b)cout << "a
if (a > b)cout << "a>b" << endl;
if (a <= b)cout << "a<=b" << endl;
if (a >= b)cout << "a>=b" << endl;
if (a != b)cout << "a!=b" << endl;
return 0;
}
/*本关任务:
设计分数类Fraction
用于表示“1/2”这样的分数,具体要求如下:类中包含:两个int型数据域,分别表示分子和分母,以及若干成员函数,例如:构造函数等。
注意:该类的数据域均为私有(private),该类的成员函数均为公共(public)。
以成员函数形式重载关系运算符
使得分数之间可以直接比较大小。通过对关系运算符(>, >=, <, <=, ==, !=)进行重载,实现分数之间的直接比较。
例如,对于 Fraction r1(1,2),r2(2,3); 能使用r1 > r2 等形式直接进行分数之间的比较。
以全局函数形式重载流插入和流提取运算符
实现直接对分数对象进行输入输出的目的,方便以字符串的形式输出分数。并且,将流插入流提取运算符函数设置成分数类Fraction的友元函数。
例如,对于 Fraction r1(3,4); 能使用cout << r1 << endl; 的形式直接以字符串形式输出3/4;
例如,对于Fraction r2; 能使用cin >> r2; 的形式直接以字符串的形式输入分数,例如:从键盘输入1/2,则分数对象r2的分子和分母分别被设置成1和2。
相关知识
为了完成本关任务,你需要掌握:1.类的设计,2.关系运算符的重载,3.流插入和流提取运算符的重载。
编程要求
根据提示,在右侧编辑器补充代码,设计分数类,重载关系运算符和流插入流提取运算符。
测试说明
平台会对你编写的代码进行测试:
测试输入:
1/2 1/3
预期输出:
1/2 1/3
a > b
a >= b
a != b/
求大神告诉我我哪里错了