C++ 重载运算符实现复数运算

本文介绍如何使用C++通过成员函数和友元函数重载运算符,实现复数的加减乘除运算。提供了完整的代码示例,包括复数类定义、运算符重载实现以及输出操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、重载运算符为成员函数

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex();
    Complex(float real, float image);

    virtual ~Complex();

    Complex operator+(const Complex &c) const;
    Complex operator-(const Complex &c) const;
    Complex operator*(const Complex &c) const;
    Complex operator/(const Complex &c) const;

    friend ostream & operator<<(ostream &os, const Complex &c);

private:
    float m_real;
    float m_image;
};

#endif // COMPLEX_H

Complex.cpp

#include "Complex.h"

Complex::Complex()
{

}

Complex::Complex(float real, float image)
{
    m_real = real;
    m_image = image;
}

//重载+运算符
Complex Complex::operator+(const Complex &c) const
{
    Complex temp;
    temp.m_real = this->m_real + c.m_real;
    temp.m_image = this->m_image + c.m_image;

    return temp;
}

//重载-运算符
Complex Complex::operator-(const Complex &c) const
{
    Complex temp;
    temp.m_real = this->m_real - c.m_real;
    temp.m_image = this->m_image - c.m_image;

    return temp;
}

//重载*运算符
Complex Complex::operator*(const Complex &c) const
{
    Complex temp;
    temp.m_real = (this->m_real * c.m_real) - (this->m_image * c.m_image);
    temp.m_image =(this->m_image * c.m_real) + (this->m_real * c.m_image);

    return temp;
}

//重载/运算符
Complex Complex::operator/(const Complex &c) const
{
    Complex temp;
    temp.m_real = (this->m_real * c.m_real + this->m_image * c.m_image) /
                  (c.m_real * c.m_real + c.m_image * c.m_image);

    temp.m_image =(this->m_image * c.m_real - this->m_real * c.m_image) /
                  (c.m_real * c.m_real + c.m_image * c.m_image);

    return temp;
}

//重载<<运算符
ostream & operator<<(ostream &os, const Complex &c)
{
    os << c.m_real << "+" << c.m_image << "i" << endl;
    return os;
}

Complex::~Complex()
{

}

main.cpp

#include "Complex.h"

int main()
{
    Complex a(1, 2);
    Complex b(2, 3);
    Complex c = a + b;
    cout << c;

    c = a - b;
    cout << c;

    c = a * b;
    cout << c;

    c = a / b;
    cout << c;

    return 0;
}

输出:

3+5i
-1+-1i
-4+7i
0.615385+0.0769231i

2、重载运算符为友元函数

Complex.h

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex();
    Complex(float real, float image);

    virtual ~Complex();

    friend Complex operator+(const Complex &c1, const Complex &c2);
    friend Complex operator-(const Complex &c1, const Complex &c2);
    friend Complex operator*(const Complex &c1, const Complex &c2);
    friend Complex operator/(const Complex &c1, const Complex &c2);

    friend ostream & operator<<(ostream &os, const Complex &c);

private:
    float m_real;
    float m_image;
};

#endif // COMPLEX_H

Complex.cpp

#include "Complex.h"

Complex::Complex()
{

}

Complex::Complex(float real, float image)
{
    m_real = real;
    m_image = image;
}

//重载+运算符
Complex operator+(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = c1.m_real + c2.m_real;
    temp.m_image = c1.m_image + c2.m_image;

    return temp;
}

//重载-运算符
Complex operator-(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = c1.m_real - c2.m_real;
    temp.m_image = c1.m_image - c2.m_image;

    return temp;
}

//重载*运算符
Complex operator*(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = (c1.m_real * c2.m_real) - (c1.m_image * c2.m_image);
    temp.m_image =(c1.m_image * c2.m_real) + (c1.m_real * c2.m_image);

    return temp;
}

//重载/运算符
Complex operator/(const Complex &c1, const Complex &c2)
{
    Complex temp;
    temp.m_real = (c1.m_real * c2.m_real + c1.m_image * c2.m_image) /
                  (c2.m_real * c2.m_real + c2.m_image * c2.m_image);

    temp.m_image =(c1.m_image * c2.m_real - c1.m_real * c2.m_image) /
                  (c2.m_real * c2.m_real + c2.m_image * c2.m_image);

    return temp;
}

//重载<<运算符
ostream & operator<<(ostream &os, const Complex &c)
{
    os << c.m_real << "+" << c.m_image << "i" << endl;
    return os;
}

Complex::~Complex()
{

}

main.cpp

#include "Complex.h"

int main()
{
    Complex a(1, 2);
    Complex b(2.0, 3);
    Complex c = a + b;
    cout << c;

    c = a - b;
    cout << c;

    c = a * b;
    cout << c;

    c = a / b;
    cout << c;

    return 0;
}

输出:

3+5i
-1+-1i
-4+7i
0.615385+0.0769231i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值