1 #include<stdio.h> 2 #include<math.h> 3 struct Complex { 4 double a;//实部 5 double b;//虚部 6 }; 7 void initComplex(double x, double y) { 8 printf("%.2f+%.2f*i\n", x, y); 9 } 10 void add(struct Complex complex1, struct Complex complex2) { 11 double sumA = complex1.a + complex2.a; 12 double sumB = complex1.b + complex2.b; 13 printf("两复数的和是:\n"); 14 initComplex(sumA, sumB); 15 } 16 void minus(struct Complex complex1, struct Complex complex2) { 17 double minusA = complex1.a - complex2.a; 18 double minusB = complex1.b - complex2.b; 19 printf("两复数的差是:\n"); 20 initComplex(minusA, minusB); 21 } 22 void multiply(struct Complex complex1, struct Complex complex2) { 23 double mulA = complex1.a * complex2.a - complex1.b * complex2.b; 24 double mulB = complex1.b * complex2.a + complex1.a * complex2.b; 25 printf("两复数的积是:\n"); 26 initComplex(mulA, mulB); 27 //复数乘法公式 z1=a+bi,z2=c+di,z1*z2=(ac-bd)+(bc+ad)i 28 } 29 void division(struct Complex complex1, struct Complex complex2) { 30 double diviA = (complex1.a * complex2.a + complex1.b * complex2.b) / (pow(complex2.a, 2.0) + pow(complex2.b, 2.0)); 31 double diviB = (complex1.b * complex2.a - complex1.a * complex2.b) / (pow(complex2.a, 2.0) + pow(complex2.b, 2.0)); 32 printf("两复数的商是:\n"); 33 initComplex(diviA, diviB); 34 //复数乘法公式 z1=a+bi,z2=c+di,z1/z2=(ac+bd)/(c^2+d^2)+( (bc-ad)/(c^2+d^2) )i 35 } 36 void main() { 37 struct Complex complex1; 38 struct Complex complex2; 39 //complex1.a = 1; 40 //complex1.b = 2; 41 printf("请输入第一个复数的实部:\n"); 42 scanf("%lf", &complex1.a); 43 printf("请输入第一个复数的虚部:\n"); 44 scanf("%lf", &complex1.b); 45 printf("得到第一个复数:"); 46 initComplex(complex1.a, complex1.b); 47 48 printf("请输入第二个复数的实部:\n"); 49 scanf("%lf", &complex2.a); 50 printf("请输入第二个复数的虚部:\n"); 51 scanf("%lf", &complex2.b); 52 printf("得到第二个复数:"); 53 initComplex(complex2.a, complex2.b); 54 55 add(complex1, complex2); 56 minus(complex1, complex2); 57 multiply(complex1, complex2); 58 division(complex1, complex2); 59 }