|  | 
 
 发表于 2021-10-17 23:21:51
|
显示全部楼层 
| 怎么?没过半个小时就等不及了?
 别人给你写代码也是需要时间的呀?
 看看这个吧
 提问的智慧
 
 这题目很简单吧?你为什么不会?
 没完全按照题目要求写,正好借此机会复习一下 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;
}
 | 
 |