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)上了。
非常感谢!

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

#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;
}