6-4 复数类重载加法、减法和乘法运算符 (10 分)

以下定义了一个复数类及其部分实现,现要求将类的构造函数以及运算符+、- 和 * 函数重载补充完整。

复数类定义:
在这里描述复数类定义。具体如下:

class complex {
    public:
        complex(float r=0,float i=0);                   // 构造函数
        complex operator+(const complex &op2) const;    //重载运算符 +
        complex operator-(const complex &op2) const;    //重载运算符 -
        complex operator*(const complex &op2) const;    //重载运算符 *
        void display() const;                           // 按数学写法输出复数
    private:
        float real;
        float imag;
};

裁判测试程序样例:
在这里给出复数类测试的例子。例如:

#include <iostream>
using namespace std;
class complex {
    public:
        complex(float r=0,float i=0);                   // 构造函数
        complex operator+(const complex &op2) const;    //重载运算符 +
        complex operator-(const complex &op2) const;    //重载运算符 -
        complex operator*(const complex &op2) const;    //重载运算符 *
        void display() const;                           // 按数学写法输出复数
    private:
        float real;
        float imag;
};


/* ------------------- 请在这里填写答案--------------------  */


void complex::display() const {
    if(real&&imag)
        if(imag==1||imag==-1)
            cout<<real<<(imag>0?"+":"-")<<"i"<<endl;
        else
            cout<<real<<(imag>0?"+":"")<<imag<<"i"<<endl;
    else if(real)
        cout<<real<<endl;
    else if (imag)
        if(imag==1||imag==-1)
            cout<<(imag>0?"":"-")<<"i"<<endl;
        else
            cout<<imag<<"i"<<endl;
    else
        cout<<0<<endl;
}

int main() {
    double real,imag;
    complex c,d,e;

    cin>>real>>imag;
    complex c1(real,imag);
    cin>>real>>imag;
    complex c2(real,imag);

    c=c1+c2;
    d=c1-c2;
    e=c1*c2;
    c.display() ;
    d.display() ;
    e.display();

    return 0;
}

输入样例:
在这里给出一组输入。例如:

2 3
-4 -5

输出样例:
在这里给出相应的输出。例如:

-2-2i
6+8i
7-22i

答案如下:

complex :: complex(float r, float i) {
    real = r;
    imag = i;
}
complex  complex :: operator+(const complex &op2) const {
    complex Complex;
    Complex.real = op2.real + real;
    Complex.imag = op2.imag + imag;
    return Complex;
}
complex  complex :: operator-(const complex &op2) const {
    complex Complex;
    Complex.real = real - op2.real ;
    Complex.imag =  imag - op2.imag;
    return Complex;
}
complex  complex :: operator*(const complex &op2) const {
    complex Complex;
    Complex.real = real * op2.real - imag * op2.imag;
    Complex.imag =  imag * op2.real + real * op2.imag;
    return Complex;
}
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值