C++运算符重载处理复数

该博客介绍了复数的四则运算,并提供了C++类`Complex`来实现复数的加、减、乘、除操作。通过示例代码展示了如何输入、输出复数以及进行复数运算,是理解复数运算和C++编程实践的好资源。

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

复数的四则运算公式:
1.复数的加法运算
(a+bi)+(c+di)=(a+c)+(b+d)i
2.复数的减法运算
(a+bi)+(c+di)=(a-c)+(b-d)i
3.复数的乘法运算
(a+bi)(c+di)=(ac-bd)+(bc+ad)i
4.复数的除法运算
(a+bi)/(c+di)
=(ac + bd)/(c^2 + d ^2) +((bc - ad)/(c ^2 + d ^2)) i

#include <iostream>
using namespace std;
class Complex
{
private:
    double real;
    double image;
public:
    Complex(const Complex& complex) :real{ complex.real }, image{ complex.image } {

    }
    Complex(double Real = 0, double Image = 0) :real{ Real }, image{ Image } {

    }
    //TODO
    friend istream& operator >> (istream& in, Complex& z)
    {
        in >> z.real >> z.image;
        return in;
    }
    friend ostream& operator << (ostream& out, const Complex& z)
    {
        out << "(";
        if (z.real >= 1e-7 || (z.real < 1e-7 && z.real > -1e-7))
            out.unsetf(std::ios::showpos);
        out << z.real;
        out.setf(std::ios::showpos);
        out << z.image;
        out << "i)";
        return out;
    }

    friend Complex operator+(const Complex& A, const Complex& B)
    {
        Complex C;
        C.real = A.real + B.real;
        C.image = A.image + B.image;
        return C;
    }

    friend Complex operator-(const Complex& A, const Complex& B)
    {
        Complex C;
        C.real = A.real - B.real;
        C.image = A.image - B.image;
        return C;
    }

    friend Complex operator*(const Complex& A, const Complex& B)
    {
        Complex C;
        C.real = A.real * B.real - A.image * B.image;
        C.image = A.real * B.image + A.image * B.real;
        return C;
    }

    friend Complex operator/(const Complex& A, const Complex& B)
    {
        Complex C;

        C.real = (A.real * B.real + A.image * B.image)  / (B.real * B.real + B.image * B.image);
        C.image = (A.image * B.real - A.real * B.image) / (B.real * B.real + B.image * B.image);
        return C;
    }

    Complex operator+(const Complex c)
    {
        return Complex(this->real + c.real, this->image + c.image);
    }

    Complex operator-(const Complex c)
    {
        return Complex(this->real - c.real, this->image - c.image);
    }
    Complex operator*(const Complex c)
    {
        return Complex(this->real * c.real - this->image * c.image,
            this->real * c.image + this->image * c.real);
    }
    Complex operator/(const Complex c)
    {
        return Complex((this->real * c.real + this->image * c.image) / (c.real * c.real + c.image * c.image),
            (this->image * c.real - this->real * c.image) / (c.real * c.real + c.image * c.image));
    }
};


int main() {
    Complex z1, z2;
    cin >> z1;
    cin >> z2;
    cout << z1 << " " << z2 << endl;
    cout << z1 + z2 << endl;
    cout << z1 - z2 << endl;
    cout << z1 * z2 << endl;
    cout << z1 / z2 << endl;
    return 0;
}

参考链接:https://blog.youkuaiyun.com/weixin_42004975/article/details/111589053

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值