计算两个复数之积c语言
时间: 2024-05-27 08:05:06 浏览: 28
以下是一个计算两个复数之积的C语言示例程序:
```c
#include <stdio.h>
// 定义复数结构体
struct Complex {
float real;
float imag;
};
// 计算两个复数之积
struct Complex complex_prod(struct Complex c1, struct Complex c2) {
struct Complex result;
result.real = c1.real * c2.real - c1.imag * c2.imag;
result.imag = c1.real * c2.imag + c1.imag * c2.real;
return result;
}
int main() {
struct Complex c1, c2, product;
// 输入第一个复数的实部和虚部
printf("Enter the real and imaginary parts of the first complex number: ");
scanf("%f %f", &c1.real, &c1.imag);
// 输入第二个复数的实部和虚部
printf("Enter the real and imaginary parts of the second complex number: ");
scanf("%f %f", &c2.real, &c2.imag);
// 计算两个复数之积
product = complex_prod(c1, c2);
// 输出结果
printf("Product of the two complex numbers = %.2f + %.2fi\n", product.real, product.imag);
return 0;
}
```