山甘 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日

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证