COMPLEX.h

 /*
typedef struct
{
float RE; //实部
float IM; //虚部
}Complex;
*/
typedef struct complex * Complex; Complex COMPLEXinit(float, float);
float Re(Complex);
float Im(Complex);
Complex COMPLEXmult(Complex, Complex);

COMPLEX.c

 #include "COMPLEX.h"

 struct complex
{
float RE; //实部
float IM; //虚部
}; Complex COMPLEXinit(float RE, float IM)
{
/*
Complex t;
t.RE=RE;
t.IM=IM;
return t;
*/ Complex t=malloc(sizeof *t);
t->RE=RE;
t->IM=IM;
return t;
}
float Re(Complex z)
{
return z->RE;
}
float Im(Complex z)
{
return z->IM;
}
Complex COMPLEXmult(Complex a, Complex b)
{
/*
Complex t;
t.RE=a.RE*b.RE-a.IM*b.IM;
t.IM=a.RE*b.IM+a.IM*b.RE; //a实部乘b实部-a虚部乘b虚部
//a实部乘b虚部+a虚部乘b实部
return t;*/ return COMPLEXinit(Re(a)*Re(b)-Im(a)*Im(b),
Re(a)*Im(b)+Im(a)*Re(b));
}

main.c

 #include <stdio.h>
#include <math.h>
#include "COMPLEX.h" #define PI 3.141592625 int main(void)
{
int N;
printf("输入一个参数:");
scanf("%d", &N);
getchar(); Complex t, x;
printf("%dth complex roots of unity\n", N);
for(int i=; i<N; i++)
{
float r=2.0*PI*i/N;
//1=e^(2n*pi*i) ? t=COMPLEXinit(cos(r), sin(r)); printf("%2d %6.3f %6.3f ", i, Re(t), Im(t));
x=t;
for(int j=; j<N-; j++)
x=COMPLEXmult(t, x); printf("%6.3f %6.3f\n", Re(x), Im(x));
} return ;
}
05-25 20:18