4-3 使用函數計算兩個複數之積 (10分)

 

若兩個複數分別爲:c1=x1+y1ic_1=x_1 + y_1 ic1=x1+y1ic2=x2+y2ic_2=x_2 + y_2 ic2=x2+y2i,則它們的乘積爲 c1×c2=(x1x2−y1y2)+(x1y2+x2y1)ic_1 \times c_2 = (x_1 x_2 - y_1 y_2) + (x_1 y_2 + x_2 y_1)ic1×c2=(x1x2y1y2)+(x1y2+x2y1)i

本題要求實現一個函數計算兩個複數之積。

函數接口定義:

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

其中用戶傳入的參數爲兩個複數x1+y1iiix2+y2iii;函數complex_prod應將計算結果的實部存放在全局變量result_real中、虛部存放在全局變量result_imag中。

裁判測試程序樣例:

#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
-2 -3

輸出樣例:

product of complex is (4.000000)+(-7.000000)i



void complex_prod( double x1, double y1, double x2, double y2 )
{
	result_real = x1*x2 - y1*y2;
	result_imag = x1*y2 + x2*y1;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章