COMPLEX.h
1 /* 2 typedef struct 3 { 4 float RE; //实部 5 float IM; //虚部 6 }Complex; 7 */ 8 typedef struct complex * Complex; 9 10 Complex COMPLEXinit(float, float);11 float Re(Complex);12 float Im(Complex);13 Complex COMPLEXmult(Complex, Complex);
COMPLEX.c
1 #include "COMPLEX.h" 2 3 struct complex 4 { 5 float RE; //实部 6 float IM; //虚部 7 }; 8 9 Complex COMPLEXinit(float RE, float IM)10 {11 /*12 Complex t;13 t.RE=RE;14 t.IM=IM;15 return t;16 */17 18 Complex t=malloc(sizeof *t);19 t->RE=RE;20 t->IM=IM;21 return t;22 }23 float Re(Complex z)24 {25 return z->RE;26 }27 float Im(Complex z)28 {29 return z->IM;30 }31 Complex COMPLEXmult(Complex a, Complex b)32 {33 /*34 Complex t;35 t.RE=a.RE*b.RE-a.IM*b.IM;36 t.IM=a.RE*b.IM+a.IM*b.RE;37 38 //a实部乘b实部-a虚部乘b虚部39 //a实部乘b虚部+a虚部乘b实部40 return t;*/41 42 return COMPLEXinit(Re(a)*Re(b)-Im(a)*Im(b),43 Re(a)*Im(b)+Im(a)*Re(b));44 }
main.c
1 #include2 #include 3 #include "COMPLEX.h" 4 5 #define PI 3.141592625 6 7 int main(void) 8 { 9 int N;10 printf("输入一个参数:");11 scanf("%d", &N);12 getchar();13 14 Complex t, x;15 printf("%dth complex roots of unity\n", N);16 for(int i=0; i