实验5-1 使用函数计算两个复数之积(10 分)

实验5-1 使用函数计算两个复数之积(10 分)

若两个复数分别为:$c_1=x_1+{y_1}i$和$c_2=x_2+{y^2}i$,则它们的乘积为 $c_1×c_2=(x_1\times{x_2}−y_1\times{y_2})+(x_1\times{y_2}+x_2\times{y1})i$。

本题要求实现一个函数计算两个复数之积。

函数接口定义:

1
2
double result_real, result_imag;
void complex_prod( double x1, double y1, double x2, double y2 );

其中用户传入的参数为两个复数x1+y1i和x2+y2i;函数complex_prod应将计算结果的实部存放在全局变量result_real中、虚部存放在全局变量result_imag中。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h> 

double result_real, result_imag;
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_prod(real1, imag1, real2, imag2);
printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);

return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1
2
1 2
-2 -3

输出样例:

1
product of complex is (4.000000)+(-7.000000)i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
double result_real,result_imag;
void complex_prod(double real1,double imag1,double real2,double imag2);
int main(void)
{
double imag1,imag2,real1,real2;

printf("Enter 1st complex number(real and imaginary):");
scanf("%lf%lf",&real1,&imag1);
printf("Enter 2nd complex number(real and imaginary):");
scanf("%lf%lf",&real2,&imag2);
complex_prod(real1,imag1,real2,imag2);
printf("product of complex is %f+%fi\n",result_real,result_imag);

return 0;
}
void complex_prod(double x1,double y1,double x2,double y2)
{
result_real=x1*x2-y1*y2;
result_imag=x1*y2+x2*y1;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×