【PTA 题目详解】 例题5-7 计算2个复数之和与之积

题目

分别输入2个复数的实部与虚部,用函数实现计算2个复数之和与之积。若2个复数分别为:c1=x1+(y1)i, c2=x2+(y2)i,则:

c1+c2 = (x1+x2) + (y1+y2)i
c1*c2 = (x1*x2-y1*y2) + (x1*y2+x2*y1)i

输入格式

输入在一行中给出4个实数,分别代表c1和c2的实部和虚部。

输出格式:

在两行中分别输出c1+c2和c1*c2的计算结果。

输入样例

1 1 -2 3

输出样例

addition of complex is -1.000000+4.000000i
product of complex is -5.000000+1.000000i

分析

难点:如何用函数返回计算结果(实部+虚部两个结果)
可以用指针返回,也可以用结构体。当然,也可用全局变量作中转,这是最简单的方法。

复数的运算实际上就是实部和虚部的运算,比如相加就是实部和虚部分别相加,相乘、相除的公式可以百度找到。

有两个值得注意的地方:

  1. 如果使用printf("%lf+%lf", 实部, 虚部);的方式输出,当虚部为负数时,会输出0.000000+-1.000000i,看着非常奇怪,也不符合数学里的写法。
    注意!如果你只是为了写这道题,那么这就是题目想要的效果!不需要改!改了反而会错!
  2. 负零”的问题:
    请看以下代码
    int main()
    {
        printf("%lf+%lf", 0, -1.0 * 0);
    	return 0;
    }
    
    输出为 0.000000+-0.000000
    原来 0 也是有正负的!

如何解决?

  1. 我们可以自己判断,如果虚部为正,正常输出,如果为负,就用printf("%lf-%lf", 实部, 绝对值(虚部))
    但是这里有个更好的方法,利用 printf 的格式设置可以省去这个判断。
    要输出一个六位小数的 double 数字,原来是这么写的:printf("%.6lf", 数据)
    现在我们改成:printf("%+.6lf", 数据)%后面加一个+,意思是总是输出数据的符号,如果是正数就输出一个正号,负数就输出一个负号。
  2. 解决“负零”很简单,-0 == +0总是为真,要除掉负零,只需要判断数字是否为 0,若是,返回一个正零即可。具体可以看下面的代码。

参考代码

为了更加优雅,下面用结构体实现:

#include <stdio.h>

//定义结构体
struct compl
{
	double real; //实部
	double imag; //虚部
};

//检查并修改 -0 为 +0
// 参数:
//     *compl - 要检查的复数 & 修改完毕的结果
// 返回值:
//     修改完毕的结果
struct compl checkNegZero(struct compl *compl)
{
	//-0 等于 +0
	//只写一个 0 就是 +0
	compl->real = compl->real == 0 ? 0 : compl->real;
	compl->imag = compl->imag == 0 ? 0 : compl->imag;
	return *compl;
}

//求复数的相反数
struct compl compl_neg(struct compl a)
{
	struct compl result;
	result.real = -a.real;
	result.imag = -a.imag;
	return checkNegZero(&result);
}

//求两个复数的和
struct compl compl_add(struct compl a, struct compl b)
{
	struct compl result;
	result.real = a.real + b.real;
	result.imag = a.imag + b.imag;
	return checkNegZero(&result);
}

//求两个复数的差
struct compl compl_sub(struct compl a, struct compl b)
{
	return compl_add(a, compl_neg(b));
}

//求两个复数的积
struct compl compl_mul(struct compl a, struct compl b)
{
	struct compl result;
	result.real = a.real * b.real - a.imag * b.imag;
	result.imag = a.real * b.imag + b.real * a.imag;
	return checkNegZero(&result);
}

int main()
{
	struct compl a, b, result1, result2;
	scanf("%lf%lf%lf%lf", &a.real, &a.imag, &b.real, &b.imag);
	result1 = compl_add(a, b);
	result2 = compl_mul(a, b);
	printf("addition of complex is %.6lf+%.6lfi\n", result1.real, result1.imag);
	printf("product of complex is %.6lf+%.6lfi\n", result2.real, result2.imag);
	return 0;
}
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下函数计算2个复数之和与之积: def add_complex(c1, c2): # 计算实部虚部的和 real = c1.real + c2.real imag = c1.imag + c2.imag # 返回新的复数 return complex(real, imag) def multiply_complex(c1, c2): # 计算实部虚部的积 real = c1.real * c2.real - c1.imag * c2.imag imag = c1.real * c2.imag + c1.imag * c2.real # 返回新的复数 return complex(real, imag) 其中,c1和c2分别表示2个复数,real和imag分别表示复数实部虚部。add_complex函数计算2个复数之和,multiply_complex函数计算2个复数之积。可以将输入实部虚部转换为复数类型,然后调用这2个函数进行计算。例如: x1 = 1 y1 = 2 x2 = 3 y2 = 4 c1 = complex(x1, y1) c2 = complex(x2, y2) sum_c = add_complex(c1, c2) product_c = multiply_complex(c1, c2) print("两个复数之和为:", sum_c) print("两个复数之积为:", product_c) 输出结果为: 两个复数之和为: (4+6j) 两个复数之积为: (-5+10j) ### 回答2: 复数是数学中的一个重要概念,它由实部虚部组成。在计算中,我们需要使用一些特殊的运算符来对复数进行加减乘除等运算。下面我们来简单介绍一下复数的基本运算及其实现方法。 首先,我们需要知道复数是由实数和虚数相加得到的。例如,一个复数c就可以表示为c=x+yi,其中x为实部,y为虚部。如果存在另一个复数c'=x'+y'i,那么它们之间的加法和乘法的结果分别为: c+c'=(x+x')+(y+y')i,即复数实部相加,虚部相加。 c×c'=(xx'-yy')+(xy'+yx')i,即复数实部相乘减虚部相乘。 利用这些规则,我们可以写出相应的函数计算两个复数之和和之积。例如,我们可以定义一个名为"complex_calculator"的函数实现这个功能,其具体代码如下: def complex_calculator(x1, y1, x2, y2): # 首先计算两个复数实部虚部的和 real_part = x1 + x2 imag_part = y1 + y2 # 然后计算两个复数之积 mul_real = x1 * x2 - y1 * y2 mul_imag = x1 * y2 + x2 * y1 # 最终返回结果 return (real_part, imag_part, mul_real, mul_imag) 在这个函数中,我们传入了4个参数:x1、y1、x2和y2分别表示两个复数实部虚部。在函数的内部,我们首先计算了两个复数实部虚部的和,然后又分别计算了它们之间的乘积。最终,我们将这两个结果作为一个元组进行返回。 这样,我们就可以用这个函数计算任意两个复数的和与积了。例如,如果两个复数分别为c1=3+4i和c2=5+6i,那么我们就可以通过调用complex_calculator(3, 4, 5, 6)来获取它们的和与积的结果了。其中,返回值为元组,分别表示两个复数的和与积的实部虚部。 ### 回答3: 复数是由实部虚部构成的数字,通常表示为a+bi的形式,其中a为实部,b为虚部,i为虚数单位,满足i^2=-1。我们可以通过创建一个复数类,实现两个复数之和与积的计算。 具体实现方法如下: 1. 创建一个名为Complex的类,其中包含实部虚部的属性,以及计算两个复数之和和积的方法。 2. 定义类的构造函数,用于初始化实部虚部属性。 3. 定义加法运算符(+)的重载方法,该方法接收另一个Complex类型的对象作为参数,并返回一个新的Complex对象,表示两个复数的和。 4. 定义乘法运算符(*)的重载方法,该方法接收另一个Complex类型的对象作为参数,并返回一个新的Complex对象,表示两个复数的积。 下面是一个可能的实现: ``` class Complex: def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary def __add__(self, other): return Complex(self.real + other.real, self.imaginary + other.imaginary) def __mul__(self, other): real_part = self.real * other.real - self.imaginary * other.imaginary imaginary_part = self.real * other.imaginary + self.imaginary * other.real return Complex(real_part, imaginary_part) ``` 通过上述实现,我们可以创建两个Complex对象c1和c2,并通过使用+和*运算符,计算它们之间的和与积。 例如,对于两个复数c1=1.5+2i、c2=0.5+1.5i,我们可以进行如下计算: ``` c1 = Complex(1.5, 2) c2 = Complex(0.5, 1.5) c_sum = c1 + c2 c_product = c1 * c2 print("Sum of c1 and c2:", c_sum.real, "+", c_sum.imaginary, "i") print("Product of c1 and c2:", c_product.real, "+", c_product.imaginary, "i") ``` 输出结果为: ``` Sum of c1 and c2: 2.0 + 3.5 i Product of c1 and c2: -1.25 + 3.25 i ``` 这表明,两个复数的和为2.0 + 3.5i,积为-1.25 + 3.25i。 综上所述,通过定义一个复数类并实现加法和乘法运算符的重载,我们可以实现复数的加法和乘法运算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值