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.
You should read the documentation for std::complex
, it will give you answers to these questions and many more.
Something like this should give you the basic idea:
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;
}
精彩评论