1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-27 14:36:19 +00:00
202401-programming-assignments/【实践课内】12.函数3/6-1 使用函数计算两个复数之和与之积.md

60 lines
1.7 KiB
Markdown

# 6-1 使用函数计算两个复数之和与之积
若两个复数分别为:`complex1 = real1 + imag1 i` 和 `complex2 = real2 + imag2 i`,则
它们的和为:`complex1 + complex2 = (real1 + real2) + (imag1 + imag2) i`
它们的乘积为:`complex1 * complex2 = (real1 * real2 - imag1 * imag2) + (real1 * imag2 + real2 * imag1) i`
本题要求实现两个函数分别计算两个复数之和、两个复数之积。
### 函数接口定义:
```c++
void complex_add (double real1, double imag1, double real2, double imag2);
void complex_prod (double real1, double imag1, double real2, double imag2);
```
其中用户传入的参数为两个复数`real1 + imag1 i` 和`real2 + imag2 i`;函数`complex_add`和函数`complex_prod`应将计算结果的实部存放在全局变量result_real中、虚部存放在全局变量result_imag中。
### 裁判测试程序样例:
```c++
#include <stdio.h>
double result_real, result_imag;
void complex_add(double real1, double imag1, double real2, double imag2);
void complex_prod(double x1, double y1, double x2, double y2);
int main(void)
{
double imag1, imag2, real1, real2;
scanf("%lf %lf", &real1, &imag1);
scanf("%lf %lf", &real2, &imag2);
complex_add(real1, imag1, real2, imag2);
printf("addition of complex is (%f)+(%f)i\n", result_real, result_imag);
complex_prod(real1, imag1, real2, imag2);
printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);
return 0;
}
/* 请在这里填写答案 */
```
### 输入样例:
```in
1 1
-2 3
```
### 输出样例:
```out
addition of complex is (-1.000000)+(4.000000)i
product of complex is (-5.000000)+(1.000000)i
```