输入两个复数后输出乘积 C语言
时间: 2023-10-19 18:07:27 浏览: 159
以下是一个C语言程序示例,输入两个复数并输出它们的积:
```c
#include <stdio.h>
// 定义复数结构体
struct complex_number {
float real_part;
float imag_part;
};
// 计算两个复数的积
struct complex_number complex_multiply(struct complex_number c1, struct complex_number c2) {
struct complex_number result;
result.real_part = c1.real_part * c2.real_part - c1.imag_part * c2.imag_part;
result.imag_part = c1.real_part * c2.imag_part + c1.imag_part * c2.real_part;
return result;
}
int main() {
// 创建两个复数对象并初始化
struct complex_number c1, c2;
printf("Enter the first complex number (real imag): ");
scanf("%f %f", &c1.real_part, &c1.imag_part);
printf("Enter the second complex number (real imag): ");
scanf("%f %f", &c2.real_part, &c2.imag_part);
// 计算两个复数的积
struct complex_number product = complex_multiply(c1, c2);
// 输出结果
if (product.imag_part < 0) {
printf("The product of %.2f - %.2fi and %.2f - %.2fi is %.2f - %.2fi\n", c1.real_part, -c1.imag_part, c2.real_part, -c2.imag_part, product.real_part, -product.imag_part);
} else {
printf("The product of %.2f + %.2fi and %.2f + %.2fi is %.2f + %.2fi\n", c1.real_part, c1.imag_part, c2.real_part, c2.imag_part, product.real_part, product.imag_part);
}
return 0;
}
```
运行程序后,输入两个复数的实部和虚部,程序将输出它们的积。例如:
```
Enter the first complex number (real imag): 2 3
Enter the second complex number (real imag): 4 -5
The product of 2.00 + 3.00i and 4.00 -5.00i is 23.00 + -2.00i
```
阅读全文