山顶洞人二号 2020-05-09 18:16 采纳率: 0%
浏览 164

求大神告诉我我哪里错了

求大神告诉我我哪里错了

#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/
求大神告诉我我哪里错了

  • 写回答

1条回答 默认 最新

  • 经常有点小迷糊 2020-05-10 12:36
    关注

    请问您用的什么编译器啊

    评论

报告相同问题?

悬赏问题

  • ¥15 程序实在不会写,要秃了
  • ¥15 pycharm导入不了自己的包
  • ¥15 C#.net通过内网url地址获取文件并下载问题,浏览器postman可以正常下载,用程序不行
  • ¥15 本人本科机械,目前研一。没有深度学习基础,目前对研究生课题一片迷茫,请教各位!
  • ¥15 关于R语言单因素与多因素线性回归的平均值
  • ¥15 服务器清除BIOS之后引导不了
  • ¥15 CPLEX用OPL编写的混合整数线性优化问题。
  • ¥15 可以用EasyConnect连接实验室内网,但无法连接内网才能访问的服务器,为什么?
  • ¥15 前端预览docx文件,文件从后端传送过来。
  • ¥15 层次聚类和蛋白质相似度