複數結構定義如下:
struct Complex
{
float real;
float image;
};
計算複數函數原型爲:struct Complex complex_prod(struct Complex c1, struct Complex c2)
在主函數中輸入兩個複數,利用該函數,計算兩個複數的乘積;
代碼如下:
void chapter1::testComplex()
{
struct Complex c1, c2, result;
//scanf_s("%f",&c1.real);
printf_s("請輸入兩個複數");
scanf_s("%f %f %f %f",&c1.real,&c1.image,&c2.real,&c2.image);
result = complex_prod(c1, c2);
printf_s("(%f+%fi)*(%f+%fi) = %f + %fi",c1.real,c1.image,c2.real,c2.image,result.real,result.image);
}
Complex chapter1::complex_prod(Complex c1, Complex c2)
{
struct Complex temp;
temp.real = c1.real*c2.real - c1.image*c2.image;
temp.image = c1.real*c2.image + c1.image *c2.real;
return temp;
}