m0_62626383 2022-04-18 10:21 采纳率: 40%
浏览 61
已结题

我想浅问一些简单的问题

img


这种重载大于号的代码,但是它题目要求又有三种情况,还有小于情况的该怎么编写代码呢?我是不是得再重载个小于符号来表示啊,Dev++上报错说没有“>>",我也不懂什么意思。

  • 写回答

3条回答 默认 最新

  • 这次真没糖 2022-04-18 13:08
    关注

    在重载>写相关逻辑就行了

    #include <iostream>
    using namespace std;
    
    class Complex {
    private:
    public:
        int real;
        int imag;
        Complex(int r, int i)
            : real(r), imag(i){};
        int operator>(Complex c);
    };
    int Complex::operator>(Complex complex) {
        int res1 = real * real + imag * imag;
        int res2 = complex.real * complex.real + complex.imag * complex.imag;
        if (res1 > res2)
            return 1;
        else if (res1 == res2)
            return 0;
        return -1;
    }
    int main() {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
    
        Complex complex1(a, b), complex2(c, d);
        cout << (complex1 > complex2) << endl;
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 修改了问题 4月18日
  • 创建了问题 4月18日

悬赏问题

  • ¥30 AVL fire DVI中的Design Explorer里面的Run的Status为什么总是Terminated?哪里出问题了?需要修改哪里?
  • ¥15 有二开IOT平台code的来搞钱了。
  • ¥15 求lingo语言编程以下内容
  • ¥15 cannot import name '_MissingValues' from 'sklearn.utils._param_validation'引用smoke
  • ¥15 shiro springboot集成问题
  • ¥15 求PHP跨站免登录技术
  • ¥15 AVL fire DVI中的Design Explorer里面的Objectives /Constraints的scale应该怎么设置
  • ¥15 qml如何绘制三维笛卡尔坐标系并向其中添加折线?
  • ¥15 treelib库有读取树数据方法吗?
  • ¥15 咨询一个PYTHON的问题