c语言复数四运算

//计算z=[(8+6i)(4+3i)]/[(8+6i)+(4+3i)];

代码内容部分讲解

#include
typedef struct complex{
float realpart;
float imagpart;
}complex;
//complex是定义的一个结构体类型 带*:指针变量,是指向complex类型的指针 不带*:complex的普通变量
complex assign(double real,double imag);//构造赋值
complex add(complex B,complex C);
complex minus(complex B,complex C);
complex multiply(complex B,complex C);
complex divide(complex B,complex C);

complex assign(double real,double imag){
complex A;
A.realpart=real;
A.imagpart=imag;
return A;
}
complex add(complex B,complex C){
complex A;
A.realpart=B.realpart+C.realpart;
A.imagpart=B.imagpart+C.imagpart;
return A;
}
complex multiply(complex B,complex C){complex A;
A.realpart=B.realpart*C.realpart-B.imagpart*C.imagpart;
A.imagpart=B.imagpart*C.realpart+B.realpart*C.imagpart;
return A;
}
complex divide(complex B,complex C){complex A;
A.realpart=	(B.realpart*C.realpart+B.imagpart*C.imagpart)/(C.imagpart*C.imagpart+C.realpart*C.realpart);
A.imagpart=(B.imagpart*C.realpart-C.imagpart*B.realpart)/(C.imagpart*C.imagpart+C.realpart*C.realpart);
return A;
}
int  main(){
complex z1,z2,z3,z4,z;double realpart,imagpart;
 z1= assign(8.0,6.0);//构造复数z1
printf("1     %.2lf+%.2lfi\n",z1.realpart,z1.imagpart);
 z2=assign(4.0,3.0);//构造复数z2
 printf("2     %.2lf+%.2lfi\n",z2.realpart,z2.imagpart);
 z3=add(z1,z2);//俩个复数相加
 printf("3     %.2lf+%.2lfi\n",z3.realpart,z3.imagpart);
 z=multiply(z1,z2);//俩个复数相乘
  printf("4     %.2lf+%.2lfi\n",z.realpart,z.imagpart);
 z4=divide(z,z3);//俩个复数相除
  printf("5     %.2lf+%.2lfi\n",z4.realpart,z4.imagpart);
} //%.2lf+%.2lfi    .2表示小数点后俩位,lf表示double类型

此代码借鉴なるnaruuuu

#include
typedef struct complex {
	float realpart;
	float imagpart;
}complex;
//complex是定义的一个结构体类型 带*:指针变量,是指向complex类型的指针 不带*:complex的普通变量
complex assign(complex &A, float real, float imag);//构造赋值
complex add(complex &A, complex B, complex C);
complex minus(complex &A, complex B, complex C);
complex multiply(complex &A, complex B, complex C);
complex divide(complex &A, complex B, complex C);

complex assign(complex &A, float real, float imag) {

	A.realpart = real;
	A.imagpart = imag;

}
complex add(complex &A, complex B, complex C) {

	A.realpart = B.realpart + C.realpart;
	A.imagpart = B.imagpart + C.imagpart;


}
complex multiply(complex &A, complex B, complex C) {

	A.realpart = B.realpart*C.realpart - B.imagpart*C.imagpart;
	A.imagpart = B.imagpart*C.realpart + B.realpart*C.imagpart;

}
complex divide(complex &A, complex B, complex C) {

	A.realpart = (B.realpart*C.realpart + B.imagpart*C.imagpart) / (C.imagpart*C.imagpart + C.realpart*C.realpart);
	A.imagpart = (B.imagpart*C.realpart - C.imagpart*B.realpart) / (C.imagpart*C.imagpart + C.realpart*C.realpart);

}
int  main() {
	complex z1, z2, z3, z4, z; float realpart, imagpart;
	//z=[(8+6i)(4+3i)]/[(8+6i)+(4+3i)];
	assign(z1, 8.0, 6.0);//构造复数z1
	printf("1     %.2f+%.2fi\n", z1.realpart, z1.imagpart);
	assign(z2, 4.0, 3.0);//构造复数z2
	printf("2     %.2f+%.2fi\n", z2.realpart, z2.imagpart);
	add(z3, z1, z2);//俩个复数相加
	printf("3     %.2f+%.2fi\n", z3.realpart, z3.imagpart);
	multiply(z, z1, z2);//俩个复数相乘
	printf("4     %.2f+%.2fi\n", z.realpart, z.imagpart);
	divide(z4, z, z3);//俩个复数相除
	printf("5     %.2f+%.2fi\n", z4.realpart, z4.imagpart);
	
}

用法借鉴WTomster

#include
typedef struct complex {
	double realpart;
	double imagpart;
}complex;
//complex是定义的一个结构体类型 带*:指针变量,是指向complex类型的指针 不带*:complex的普通变量
complex assign(complex *A,double real, double imag);//构造赋值
complex add(complex *A,complex * B, complex *C);
complex minus(complex *A,complex *B, complex *C);
complex multiply(complex *A,complex* B, complex *C);
complex divide(complex *A,complex *B, complex *C);

complex assign(complex *A,double real, double imag) {
	
	A->realpart = real;
	A->imagpart = imag;
	printf("%.2lf+%.2lfi\n", A->realpart, A->imagpart);
	
}
complex add(complex *A,complex *B, complex *C) {
    
	A->realpart = B->realpart + C->realpart;
	A->imagpart = B->imagpart + C->imagpart;
	printf("%.2lf+%.2lfi\n",   A->realpart, A->imagpart);
	//->用来 指向结构体子数据的指针,用来取子数据 
}


complex multiply(complex *A,complex *B, complex* C) {
	
	A->realpart = B->realpart*C->realpart - B->imagpart*C->imagpart;
	A->imagpart = B->imagpart*C->realpart + B->realpart*C->imagpart;
	printf("%.2lf+%.2lfi\n", A->realpart, A->imagpart);
}
complex divide(complex *A,complex *B, complex *C) {
	
	A->realpart = (B->realpart*C->realpart + B->imagpart*C->imagpart) / (C->imagpart*C->imagpart + C->realpart*C->realpart);
	A->imagpart = (B->imagpart*C->realpart - C->imagpart*B->realpart) / (C->imagpart*C->imagpart + C->realpart*C->realpart);
	printf("%.2lf+%.2lfi\n", A->realpart, A->imagpart);
}
int  main() {
complex	z1, z2,z3,z4, z; 
	//z=[(8+6i)(4+3i)]/[(8+6i)+(4+3i)];
assign(&z1,8.0, 6.0);//构造复数z1
assign(&z2,4.0, 3.0);//构造复数z2
add(&z3,&z1, &z2);//俩个复数相加

//printf("mian    %.2lf+%.2lfi\n",   z3.realpart, z3.imagpart);
//.前面的对象为结构类型的变量  ->左边应该是指向该结构体的指针 
multiply(&z,&z1, &z2);//俩个复数相乘
//printf("main    %.2lf+%.2lfi\n",   z.realpart, z.imagpart);
divide(&z4,&z, &z3);//俩个复数相除*/
//printf("main   %.2lf+%.2lfi\n",   z4.realpart, z4.imagpart);
return 0;
}
        

你可能感兴趣的:(数据结构)