栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

复数的四则运算公式:
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 
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.csdn.net/weixin_42004975/article/details/111589053

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号