m0_63220286 2022-05-14 15:28 采纳率: 33.3%
浏览 215

C++运算符重载问题 本题考虑对运算符进行重载

本题考虑对运算符进行重载。分别重载复数运算的+,-,*,/,=(赋值)运算符,以及比较大小的<=(复数1的模是否小于等于复数2的模)运算符,其中,比较运算符按复数的模进行比较。测试程序根据输入的mode值分别测试各个函数是否编写正确。
函数接口定义:

在这里描述函数接口:
i#include <iostream>
using namespace std;

class Complex {
    double real;
    double imag;
public:
    //构造函数
    Complex(double real=0, double imag=0);

    //operator+-*/=操作符函数
    Complex operator+(const Complex& c) const;
    Complex operator-(const Complex& c) const;
    Complex operator*(const Complex& c) const;
    Complex operator/(const Complex& c) const;
    Complex operator=(const Complex& c);

    //operator <=操作符函数
    bool operator<=(const Complex& c) const;

    //友元函数声明以帮助operator<<()函数访问Complex类的私有成员
    friend ostream& operator<<(ostream& out, const Complex& c);
};

//n个复数,按模从小到达排序
void bubble_sort(Complex[],int n);


bubble_sort函数按冒泡排序的算法对n个复数按模从小到大的顺序排序。

裁判测试程序样例

在这里给出函数被调用进行测试的例子:
int main() {
    double dReal1, dImag1, dReal2, dImag2;

    int mode;
    cin>>mode;
    cin>>dReal1>>dImag1>>dReal2>>dImag2;
    Complex c1(dReal1, dImag1);
    Complex c2(dReal2, dImag2);
    Complex c[6] = {c1,c2,c1+c2,c1-c2,c1*c2,c1/c2};
    switch(mode)
    {
        case 1: cout << c[0]<<" "<<c[1];break;
        case 2: cout << c[2];break;
        case 3: cout << c[3];break;
        case 4: cout << c[4];break;
        case 5: cout << c[5];break;
        case 6: bubble_sort(c,6);
                for(int i=0;i<6;i++)
                    cout<<c[i]<<" ";

    }

    return 0;
}
/* 请在这里填写答案 */


输入样例1
1
1 2 3 4
输出样例1
1+2i 3+4i

输入样例2
2
4 5 6 7
输出样例2
10+12i
注意:复数的输出,实部和虚部即使为0也输出

  • 写回答

2条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-14 17:34
    关注
    
    /* 运算符重载(复数为例),两种重载形式:重载为类的非静态函数(加法),重载为非成员函数(减法)
     * 实现 +,-,++,--,<< 运算符的重载
     * date: Mar,27
    **/
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    class Complex    // 复数类
    {
        public:
            Complex(double a, double b = 0.0):real(a), imag(b) {}
    
            Complex operator+ (const Complex c)    // 重载 +
            {
                double r = real + c.real;
                double v = imag + c.imag;
                return Complex(r, v);
            }
    
            Complex operator++()    // 重载前置++,没有形参
            {
                real++;
                return *this;
            }
    
            Complex operator++(int)    // 重载后置++,有 int 形参
            {
                Complex old = *this;    // 调用 this
                ++(*this);    // 调用前置++
                return old;
            }
    
            friend Complex operator-(const Complex c1, const Complex c2);    // - 重载为非成员函数
            friend Complex operator--(Complex &c);    // 重载前置--
            friend Complex operator--(Complex &c, int);    // 重载后置--
            friend ostream &operator<<(ostream &out, const Complex c);    // 重载 <<,只能重载为非成员函数
    
            void print()
            {
                if (fabs(imag - 0.0) < 10e-6)
                    cout << real << endl;
                else if (imag > 0)
                    cout << real << "+" << imag << "i" << endl;
                else
                    cout << real << imag << "i" << endl;
            }
    
        private:
            double real;
            double imag;
    };
    
    Complex operator-(const Complex c1, const Complex c2)
    {
        double r = c1.real - c2.real;
        double v = c1.imag - c2.imag;
        return Complex(r, v);    // 临时对象语法
    }
    
    Complex operator--(Complex &c)
    {
        c.real--;
        return c;
    }
    
    Complex operator--(Complex &c, int)    //
    {
        Complex old = c;
        c.real--;
        return old;
    }
    
    ostream &operator<<(ostream &out, const Complex c)
    {
        if (fabs(c.imag - 0.0) < 10e-6)    // 虚部为 0 时,直接输出实部
            cout << c.real;
        else if (c.imag > 0)
            cout << c.real << "+" << c.imag << "i";
        else
            cout << c.real << c.imag << "i";
    }
    
    int main(void)
    {
        Complex c1(0, -1.1);
        Complex c2(2.6);
        Complex c3 = c1 + c2;
        Complex c4 = c1 - c2;
    
        (c3--).print();
        cout << --c4 << endl;
        
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 5月14日

悬赏问题

  • ¥20 指导如何跑通以下两个Github代码
  • ¥15 大家知道这个后备文件怎么删吗,为啥这些文件我只看到一份,没有后备呀
  • ¥15 C++为什么这个代码没报错运行不出来啊
  • ¥15 一道ban了很多东西的pyjail题
  • ¥15 关于#r语言#的问题:如何将生成的四幅图排在一起,且对变量的赋值进行更改,让组合的图漂亮、美观@(相关搜索:森林图)
  • ¥15 C++识别堆叠物体异常
  • ¥15 微软硬件驱动认证账号申请
  • ¥15 GPT写作提示指令词
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题