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

若两个复数分别为:c1​=x1​+y1​i和c2​=x2​+y2​i,则它们的乘积为 c1​×c2​=(x1​x2​−y1​y2​)+(x1​y2​+x2​y1​)i。

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

函数接口定义:

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中。

裁判测试程序样例:

#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

结尾无空行

思路:构造函数,为题目给出的函数设计程序

1,首先看题目给出的函数框架:void complex_prod( double x1, double y1, double x2, double y2 );  可知传入的参数分别是: x1,x2,y1,y2;

2,再看一下题目的要求:其中用户传入的参数为两个复数x1+y1i和x2+y2i;函数complex_prod应将计算结果的实部存放在全局变量result_real中、虚部存放在全局变量result_imag中。

3,题目中已经给出计算结果用的公式,即: c1​×c2​=(x1​x2​−y1​y2​)+(x1​y2​+x2​y1​)i。可知实部为x1​x2​−y1​y2,虚部为x1​y2​+x2​y1;

4,然后就可以写代码了:因为题目中已经进行了定义了全局变量:double result_real, result_imag;在自定义的函数中可以直接引用;

完整代码:

void complex_prod( double x1, double y1, double x2, double y2 ){
    result_real=x1*x2-y1*y2;
    result_imag=x1*y2+x2*y1;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们设计一个复数类时,可以考虑使用双精度浮点数作为实部和虚部的数据类型。同时,为了方便起见,我们可以重载输出运算符(<<)以输出复数对象的值。 下面是一个简单的Complex类实现,包括数据成员和成员函数的定义: ```c++ #include <iostream> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0.0, double i = 0.0): real(r), imag(i) {} // 友元函数重载乘法运算符 friend Complex operator*(const Complex& a, const Complex& b) { double r = a.real * b.real - a.imag * b.imag; double i = a.real * b.imag + a.imag * b.real; return Complex(r, i); } // 成员函数重载除法运算符 Complex operator/(const Complex& other) const { double r = (real * other.real + imag * other.imag) / (other.real * other.real + other.imag * other.imag); double i = (imag * other.real - real * other.imag) / (other.real * other.real + other.imag * other.imag); return Complex(r, i); } // 重载输出运算符 friend ostream& operator<<(ostream& out, const Complex& c) { out << c.real << " + " << c.imag << "i"; return out; } }; ``` 在上面的代码中,我们定义了一个Complex类,其中包含实部和虚部两个数据成员。我们使用了构造函数来初始化这两个数据成员。然后,我们使用友元函数重载了乘法运算符,成员函数重载了除法运算符,并重载了输出运算符以便于输出复数对象的值。 接下来,我们可以使用这个类来完成两个复数对象的乘除法。例如: ```c++ int main() { Complex a(1, 2); Complex b(2, -1); Complex c = a * b; Complex d = a / b; cout << "a * b = " << c << endl; cout << "a / b = " << d << endl; return 0; } ``` 在上面的代码中,我们定义了两个复数对象a和b,并别初始化它们的实部和虚部。然后,我们使用重载的乘法运算符计算它们的乘,并使用重载的除法运算符计算它们的商。最后,我们使用重载的输出运算符输出结果。 输出结果如下: ``` a * b = 4 + 3i a / b = 0.2 + 1.4i ``` 可以看到,我们成功地使用了Complex类完成了两个复数对象的乘除运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值