设两个复数分别为(a+bi)和(c+di),则它们加减乘除后的结果如下: (a+bi)+(c+di)=(a+c)+(b+d)i, (a+bi)-(c+di)=(a-c)+(b-d)i, (a+bi)*(c+di)=(ac-bd)+(ad+bc)i, (a+bi)/(c+di)=(ac+bd)/(c2+d2) +((bc-ad)/(c2+d2))i
代码实现:
#include<stdio.h>
#include<math.h>
typedef struct{
float realpart;
float imagpart;
}complex;
void assign(complex *A,float real,float imag)
{
A->realpart = real;
A->imagpart = imag;
}
void add(complex *A,complex *B,complex *C)
{
C->realpart = A->realpart + B->realpart;
C->imagpart = A->imagpart + B->imagpart;
}
void minus(complex *A,complex *B,complex *D)
{
D->realpart = A->realpart - B->realpart;
D->imagpart = A->imagpart - B->imagpart;
}
void multiply(complex *A,complex *B,complex *F)
{
F->realpart = (A->realpart * B->realpart) - (A->imagpart * B->imagpart);
F->imagpart = (A->realpart * B->imagpart) + (A->imagpart * B->realpart);
}
void divide(complex *A,complex *B,complex *G)
{
G->realpart = ((A->realpart * B->realpart) + (A->imagpart * B->imagpart))
/ ((B->realpart * B->realpart) + (B->imagpart * B->imagpart));
G->imagpart = ((A->imagpart * B->realpart) - (A->realpart * B->imagpart))
/ ((B->realpart * B->realpart) + (B->imagpart * B->imagpart));
}
void Getreal(complex *a)
{
printf("realpart = %.2f\n",a->realpart);
}
void Getimag(complex *a)
{
printf("imagpart = %.2f\n",a->imagpart);
}
int main()
{
float realpart,imagpart;
complex a1,a2,a3,a4,a5,a6;
assign(&a1,8.0,6.0);
assign(&a2,4.0,3.0);
add(&a1,&a2,&a3);
minus(&a1,&a2,&a4);
multiply(&a1,&a2,&a5);
divide(&a1,&a2,&a6);
printf("第一个复数为:%0.2f+%0.2fi\n",a1.realpart,a1.imagpart);
printf("第一个复数的实部和虚部分别为:\n");
Getreal(&a1);
Getimag(&a1);
printf("第二个复数为:%0.2f+%0.2fi\n",a2.realpart,a2.imagpart);
printf("第二个复数的实部和虚部分别为:\n");
Getreal(&a2);
Getimag(&a2);
printf("相加后复数为:%0.2f+%0.2fi\n",a3.realpart,a3.imagpart);
printf("两个复数相加后的实部和虚部分别为:\n");
Getreal(&a3);
Getimag(&a3);
printf("相减后复数为:%0.2f+%0.2fi\n",a4.realpart,a4.imagpart);
printf("两个复数相减后的实部和虚部分别为:\n");
Getreal(&a4);
Getimag(&a4);
printf("相乘后复数为:%0.2f+%0.2fi\n",a5.realpart,a5.imagpart);
printf("两个复数相乘后的实部和虚部分别为:\n");
Getreal(&a5);
Getimag(&a5);
printf("相除后复数为:%0.2f+%0.2fi\n",a6.realpart,a6.imagpart);
printf("两个复数相除后的实部和虚部分别为:\n");
Getreal(&a6);
Getimag(&a6);
return 0;
}
|