How do I enter complex numbers in C ++?

2

I know I've asked a question like this before. It was about Python. But in C ++, how do you insert a complex number?

    
asked by anonymous 10.05.2015 / 09:02

2 answers

2

In c ++ you have the complex type. It can be used as follows:

// 1 + 2i
std::complex<double> c(1, 2);

The constructor receives two parameters:

  • The first, the real part of the number
  • The second, the imaginary part
10.05.2015 / 09:34
0

In C (it's not in the question, but it's in a tag) I do it

#include <complex.h>
#include <stdio.h>

int main(void) {
    complex double x, y, z;
    x = 3.14159 - 2*_Complex_I;
    y = 2.71828182 - _Complex_I;
    z = x - 2*y; printf("x - 2y = %f%+fi\n", creal(z), cimag(z));
}

You can see the code working on ideone .

(In C ++ I have no idea)

    
10.05.2015 / 09:34