复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i2=−1;也可以写成极坐标下的指数形式 (R×e^(Pi)),其中 R 是复数模,P 是辐角,i 是虚数单位,其等价于三角形式 R(cos(P)+isin(P))。
现给定两个复数的 R 和 P,要求输出两数乘积的常规形式。
输入格式:
输入在一行中依次给出两个复数的 R1, P1, R2, P2,数字间以空格分隔。
输出格式:
在一行中按照 A+Bi
的格式输出两数乘积的常规形式,实部和虚部均保留 2 位小数。注意:如果 B
是负数,则应该写成 A-|B|i
的形式。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
double R1, P1, R2, P2;
scanf("%lf %lf %lf %lf", &R1, &P1, &R2, &P2);
double a1, b1, a2, b2, x, y;
a1 = R1 * cos(P1);
b1 = R1 * sin(P1);
a2 = R2 * cos(P2);
b2 = R2 * sin(P2);
x = a1 * a2 - b1 * b2;
y = a1 * b2 + b1 * a2;
if (fabs(x) < 0.01)
{
x = 0;
}
if (fabs(y) < 0.01)
{
y = 0;
}
if (y >= 0)
{
printf("%.2lf+%.2lfi", x, y);
}
else
printf("%.2lf%.2lfi", x, y);
system("pause");
return 0;
}