0

may you tell me how to write a complex variable in C++ code please?

I do have separate real part psi_rl and imaginary part psi_im. Now I have to write psi = psi_rl + i psi_im. Do you have any idea how to accomplish this task?

Thanks.

4

2 回答 2

3

您应该阅读 的文档std::complex,它将为您提供这些问题的答案等等。

于 2011-03-12T19:25:52.987 回答
1

像这样的东西应该给你基本的想法:

class complex
{
public:
    double real;
    double imag;
    complex(double real, double imag): real(real), imag(imag) {};
    complex operator+(complex c) { return complex(this->real+c.real, this->imag+c.imag); };
};

int main(int argc, char* argv[])
{
    complex a(1,2);
    complex b(-3,6);
    complex c = a+b;
    return 0;
}
于 2011-03-12T19:47:57.447 回答