【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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值