仅使用类和两个变量将两个复数相加C ++

时间:2018-10-21 01:52:22

标签: c++ class

我的问题很小,但是我对课堂的理解使我无法接受。我想做的是将两个复数加在一起,例如将两个复数的虚部相加并存储该值,然后将它们的实数加在一起。我认为声明两个变量并将其保留在add方法内的唯一方法是实例化另一个Imaginary对象。我不知道该怎么做。

#include <iostream>
using namespace std;

class Imaginary
{
public:
    Imaginary(float = 0.0, float = 0.0);
    void add(float, float);
    void subtract(float, float);
    void multiply();
    void print();
    void cube();

private:
    float real, imag;
};
Imaginary::Imaginary(float r, float i)
{

    real = r;
    imag = i;
}

void Imaginary::add(float sumreal, float sumimag)

这笔款项只是一个占位符。我不确定如何实例化另一组实数和imag变量以添加它们。

{
    sum = real + imag;

我想要类似sumimag = a.imag + imag;
的东西 以及sumreal = a.real + real;不确定如何在方法中执行此操作。

}

void Imaginary::subtract(float differencereal, float differenceimag)
{
    difference = real - imag;
}

int main()
{
    float sumreal, sumimag, differencereal, differenceimag;
    Imaginary i(1,2);
    i.add(sumreal, sumimag);
    i.subtract(differencereal, differenceimag);
}

4 个答案:

答案 0 :(得分:1)

我不确定您是否正在寻找它,但是...

#include <iostream>

class Complex
{
public:
    Complex(float real = 0.f, float imaginary = 0.f) : r{ real }, i{ imaginary } {}
    Complex add(float real, float imaginary) {
        return Complex(r + real, i + imaginary);
    }
    Complex add(Complex const &other) {
        return Complex(r + other.r, i + other.i);
    }
    Complex subtract(float real, float imaginary) {
        return Complex(r - real, i - imaginary);
    }
    Complex subtract(Complex const &other) {
        return Complex(r - other.r, i - other.i);
    }

private:
    float r, i;
};

int main()
{
    Complex a{ 1, 3 };
    Complex b = a.add(2, 7);

    Complex c{ 4, 2 };
    Complex d = b.subtract(c);
}

答案 1 :(得分:0)

您想覆盖该类的+和-运算符,以便可以彼此添加和减去两个Imaginary实例。

这里的示例完全满足您的要求:https://www.cprogramming.com/tutorial/operator_overloading.html

答案 2 :(得分:0)

这是解决过载的一种方法。

#include <iostream>
#include <math.h>

using namespace std;

class Complex
{
public:
    Complex(double= 0.0, double= 0.0);

    Complex operator+(const Complex &c);
    //    Complex operator-(Complex c);
    //    Complex operator/(Complex c);
    //    Complex operator*(Complex c);
    //
    void print();

    std::string to_string();
    // void cube();

private:
    double real, imaginary;
};

std::string Complex::to_string()
{
    return (std::to_string(real) + (imaginary < 0 ? " - " : " + ") + std::to_string(fabs(imaginary)) + "i");
}
Complex::Complex(double realArg, double imaginaryArg)
{
    real = realArg;
    imaginary = imaginaryArg;
}

Complex Complex::operator+(const Complex &c)
{
    Complex result;
    result.real = real + c.real;
    result.imaginary = imaginary + c.imaginary;
    return result;
}

void Complex::print()
{
    cout << "Value of Complex number: " << real << (imaginary < 0 ? " - " : " + ") << fabs(imaginary) << "i" << endl;
}

int main()
{
    Complex complex1(1, 1);
    Complex complex2(2, 2);
    Complex complexSum;

    complexSum = complex1 + complex2;

    cout << complex1.to_string() << " + " << complex2.to_string() << " = " << complexSum.to_string() << endl;

    complexSum.print();
    return 0;
}

输出:

1.000000 + 1.000000i + 2.000000 + 2.000000i = 3.000000 + 3.000000i
Value of Complex number: 3 + 3i

答案 3 :(得分:0)

我的建议是不要在标头std::complex<>中找到<complex>,而在标准库中use the existing class for this,并且可以在现代编译器和库中使用。