复数 一级ADT实现
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 #include <stdio.h>
2 #include <math.h>
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<N; i++)
17 {
18 float r=2.0*PI*i/N;
19 //1=e^(2n*pi*i) ?
20
21
22 t=COMPLEXinit(cos(r), sin(r));
23
24 printf("%2d %6.3f %6.3f ", i, Re(t), Im(t));
25 x=t;
26 for(int j=0; j<N-1; j++)
27 x=COMPLEXmult(t, x);
28
29 printf("%6.3f %6.3f\n", Re(x), Im(x));
30 }
31
32 return 0;
33 }
posted on 2018-05-16 11:03 MACHINE_001 阅读(711) 评论(0) 编辑 收藏 举报