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

悬赏问题

  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序