简体   繁体   中英

how to get output of complex number with 'i' (imaginary value ) in the output

here is my code 4 fft calc using bluestein algorithm in c++

#include <iostream>
#include <cmath>
#include <complex>
#define pi 3.14
using namespace std;


int main()
{
    int n, k, N;



    std::complex<double> y, z, sum = 0,  x[] = { 1, 2, 2, 1 };
    int i = sqrt(-1);
    N = 4;

    for (k = 0; k <= N - 1; k++)
    {
        y = exp(pi*k*N*k*i);

        for (n = 0; n <= N - 1; n++)
        {
            z = exp((pi*(k*k - 2 * k*n*i)) / N);
            sum += (x[n] *  z);
        }

        x[k] = (sum * y);

         cout << "The fft of x[n] is =  " << x[k] << endl;
    }
    return 0;
}

i need out put as {6, -1-i, 0, -1+j} when v run in visual studio 2013. if possible plz help me to declare input array generally.

the << operator is overladed like this http://en.cppreference.com/w/cpp/numeric/complex/operator_ltltgtgt

however you can just use this to get desired output:

cout << "The fft of x[n] is =  " << x[k].real() << "+"<< x[k].imag()<< "i" << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM