柿子QAQ 发表于 2021-10-17 22:12:25

已有 1 人购买  本主题需向作者支付 5 鱼币 才能浏览 购买主题

柿子QAQ 发表于 2021-10-17 22:26:22

来人呀

此方星河 发表于 2021-10-17 22:29:45

为啥你求助还要我们向你支付鱼币呢?

a327904410 发表于 2021-10-17 22:34:53

你求助于人还要人给你钱的吗{:10_249:}

人造人 发表于 2021-10-17 23:21:51

柿子QAQ 发表于 2021-10-17 22:26
来人呀

怎么?没过半个小时就等不及了?
别人给你写代码也是需要时间的呀?
看看这个吧
提问的智慧

这题目很简单吧?你为什么不会?
没完全按照题目要求写,正好借此机会复习一下 C++ 的 用户定义字面值(User-defined literals)
代码只是给你参考,不是给你直接交差了事的

还有,你悬赏弄成付费了


#include <iostream>

class complex_t {
public:
    complex_t(int real = 0, int imag = 0): real(real), imag(imag) {}
    complex_t(const complex_t &rhs): real(rhs.real), imag(rhs.imag) {}
    complex_t operator+(const complex_t &rhs) const {
      return complex_t(this->real + rhs.real, this->imag + rhs.imag);
    }
    friend std::ostream &operator<<(std::ostream &os, const complex_t &rhs);
private:
    int real, imag;
};

complex_t operator""_i(unsigned long long imag) {
    return complex_t(0, imag);
}

complex_t operator+(unsigned long long lhs, const complex_t &rhs) {
    return complex_t(lhs, 0) + rhs;
}

std::ostream &operator<<(std::ostream &os, const complex_t &rhs) {
    os << "(" << rhs.real << "," << rhs.imag << "i)";
    return os;
}

int main() {
    complex_t c1 = 20+40_i;
    complex_t c2 = 0+0_i;
    complex_t c3 = c1;
    std::cout << c1 << std::endl;
    std::cout << c2 << std::endl;
    std::cout << c3 << std::endl;
    return 0;
}

sunyt 发表于 2021-10-19 11:07:56

你这个付费可太秀了{:7_137:}

扼言abc 发表于 2021-10-19 22:25:58

???

柿子QAQ 发表于 2021-10-25 09:10:16

抱歉抱歉,设置错了真的抱歉
页: [1]
查看完整版本: 求助代码,小白想参考一下