山甘 2023-04-11 17:59 采纳率: 77.8%
浏览 49
已结题

c++定义一个复数类,实现加减乘

题目要求如图:

img

我的代码如下:


“标头.h”

#pragma once
#include<iostream>
using namespace std;
class complex
{
public:
    complex(double r, double i);
    int operator==(complex c);

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

    friend ostream& operator<<(ostream&, complex&);
    friend istream& operator>>(istream&, complex&);
private:
    double real;
    double imag;
};
complex::complex(double r = 0.0, double i = 0.0)
{
    real = r;
    imag = i;
}
int complex::operator==(complex c)
{
    if (real == c.real && imag == c.imag)
        return 1;
    else
        return 0;
}

//complex operator+(complex& c1, complex& c2)
//{
//    return
//        complex(c1.real + c2.real, c1.imag + c2.imag);
//}
//complex operator-(complex& c1, complex& c2)
//{
//    return
//        complex(c1.real - c2.real, c1.imag - c2.imag);
//}

ostream& operator<<(ostream& output, complex& c)
{
    output << "(" << c.real << "+" << c.imag << "i)";
    return output;
}
istream& operator>>(istream& input, complex& c)
{
    cout << "input real part and imaginary part of Complex number:";
    input >> c.real >> c.imag;
    return input;
}

“源.cpp”

#include"标头.h"
int main()
{
    complex c1, c2;
    cin >> c1 >> c2;
    cout << "(1+1i)(2+2i)(2+4i)测试复数的+、-、*以及==运算:" << endl;
    cout << c1 << "与" << c2 << "是否相等:";
    if (c1 == c2)
        cout << 1 << endl;
    else
        cout << 0 << endl;
    
}   

我的运行结果如图:

img

题目要求中

img

img

(2+4i)”没有输入显示,我就不太清楚该怎么把输入的(2+2i)与这个(2+4i)相加减乘了。
(也可能是我读题理解有误,感谢指正)
目前卡到这个(2+4i)上了。
非常感谢!

  • 写回答

1条回答 默认 最新

  • 社区专家-Monster-XH 2023-04-12 16:55
    关注

    按照这个看自己能不能改改,就是输出不太一样

    #pragma once
    #include<iostream>
    using namespace std;
    class Complex
    {
    public:
        Complex(double real = 0, double imag = 0);
        Complex(const Complex &c);
        ~Complex();
        Complex operator+(const Complex &c);
        Complex operator-(const Complex &c);
        Complex operator*(const Complex &c);
        bool operator==(const Complex &c);
        operator double();
        friend ostream &operator<<(ostream &out, const Complex &c);
        friend istream &operator>>(istream &in, Complex &c);
        double fsabs();
        Complex fsqrt();
    private:
    
        double real;
        double imag;
    };  
    #include "Complex.h"
    #include<cmath>
    #include<iostream>
    using namespace std;
    Complex::Complex(double real, double imag)
    {
        this->real = real;
        this->imag = imag;
    }
    Complex::Complex(const Complex &c)
    {
        this->real = c.real;
        this->imag = c.imag;
    }
    Complex::~Complex()
    {
    
    }
    Complex Complex::operator+(const Complex &c)
    {
        Complex temp;
        temp.real = this->real + c.real;
        temp.imag = this->imag + c.imag;
        return temp;
    }
    Complex Complex::operator-(const Complex &c)
    {
        Complex temp;
        temp.real = this->real - c.real;
        temp.imag = this->imag - c.imag;
        return temp;
    }
    Complex Complex::operator*(const Complex &c)
    {
        Complex temp;
        temp.real = this->real * c.real - this->imag * c.imag;
        temp.imag = this->real * c.imag + this->imag * c.real;
        return temp;
    }
    bool Complex::operator==(const Complex &c)
    {
        if (this->real == c.real && this->imag == c.imag)
            return true;
        else
            return false;
    }
    Complex::operator double()
    {
        return this->real;
    }
    ostream &operator<<(ostream &out, const Complex &c)
    {
        out << "(" << c.real << "+" << c.imag << "i)";
        return out;
    }
    istream &operator>>(istream &in, Complex &c)
    {
        in >> c.real >> c.imag;
        return in;
    }
    double Complex::fsabs()
    {
        return sqrt(this->real * this->real + this->imag * this->imag);
    }
    Complex Complex::fsqrt()
    {
        Complex temp;
        temp.real = sqrt(this->real * this->real + this->imag * this->imag) * cos(atan(this->imag / this->real) / 2);
        temp.imag = sqrt(this->real * this->real + this->imag * this->imag) * sin(atan(this->imag / this->real) / 2);
        return temp;
    }
    int main()
    {
        Complex c1, c2;
        cout << "input real part and imaginary part of Complex number: ";
        cin >> c1;
        cout << "input real part and imaginary part of Complex number: ";
        cin >> c2;
        cout << c1 << c2 << c1 * c2 << "测试复数的+、-、*以及==运算:" << endl;
        cout << c1 << "与" << c2 << "是否相等:" << (c1 == c2) << endl;
        cout << c1 << "+" << c2 << "=" << c1 + c2 << endl;
        cout << c2 << "-" << c1 << "=" << c2 - c1 << endl;
        cout << c1 << "*" << c2 << "=" << c1 * c2 << endl;
        cout << "测试复数的类型转换:" << endl;
        cout << "Complex(2.5)+" << c1 * c2 << "=" << Complex(2.5) + c1 * c2 << endl;
        cout << "(double)" << c1 * c2 << "=" << (double)(c1 * c2) << endl;
        cout << "测试复数的模和复数平方根:" << endl;
        cout << c1 * c2 << "的模是:" << (c1 * c2).fsabs() << ";" << c1 * c2 << "的平方根是:" << (c1 * c2).fsqrt() << endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月13日
  • 已采纳回答 4月12日
  • 修改了问题 4月12日
  • 创建了问题 4月11日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”