计算两个复数之和与之积代码c语言
时间: 2024-01-18 07:03:11 浏览: 190
好的,以下是计算两个复数之和与之积的 c 代码:
```c
#include <stdio.h>
typedef struct {
double real;
double imag;
} Complex;
Complex add(Complex c1, Complex c2) {
Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
Complex multiply(Complex c1, Complex c2) {
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() {
Complex c1 = {1.0, 2.0};
Complex c2 = {3.0, 4.0};
Complex sum = add(c1, c2);
Complex product = multiply(c1, c2);
printf("Sum: %lf + %lf i\n", sum.real, sum.imag);
printf("Product: %lf + %lf i\n", product.real, product.imag);
return 0;
}
```
希望这个代码能对你有所帮助!现在,轮到你来提问啦!
阅读全文