1051 复数乘法 (15 分) (C语言)

复数可以写成 (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 的形式。

输入样例:

2.3 3.5 5.2 0.4

结尾无空行

输出样例:

-8.68-8.23i

结尾无空行

#include<stdio.h>
#include<math.h>
int main(){
    double a,b,c,d;
    scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    double a1,b1,c1,d1;//如果为float 测试点3不通过
    a1=a*cos(b);
    b1=a*sin(b);
    c1=c*cos(d);
    d1=c*sin(d);
    double a2,b2;
    a2=a1*c1-b1*d1;
    b2=b1*c1+a1*d1;
    if(b2>=-0.005&&b2<=0) b2=0.00;
    if(a2>=-0.005&&a2<=0) a2=0.00;
    //要注意的点是输出的时候是保留两位小数的,如果 -0.001的话,最终输出的是-0.00,这很显然就是0了,正确的是应该输出0.00,
    if(b2>=0)
    printf("%.2lf+%.2lfi",a2,b2);
    else
        printf("%.2lf-%.2lfi",a2,fabs(b2));
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,我们可以使用结构体来表示复数,并进行四则运算操作。下面是一个示例代码: ```c #include <stdio.h> // 定义复数结构体 typedef struct { float real; float imag; } Complex; // 复数加法 Complex add(Complex a, Complex b) { Complex result; result.real = a.real + b.real; result.imag = a.imag + b.imag; return result; } // 复数减法 Complex subtract(Complex a, Complex b) { Complex result; result.real = a.real - b.real; result.imag = a.imag - b.imag; return result; } // 复数乘法 Complex multiply(Complex a, Complex b) { Complex result; result.real = a.real * b.real - a.imag * b.imag; result.imag = a.real * b.imag + a.imag * b.real; return result; } // 复数除法 Complex divide(Complex a, Complex b) { Complex result; float denominator = b.real * b.real + b.imag * b.imag; result.real = (a.real * b.real + a.imag * b.imag) / denominator; result.imag = (a.imag * b.real - a.real * b.imag) / denominator; return result; } int main() { // 创建两个复数对象 Complex num1, num2; // 输入第一个复数的实部和虚部 printf("Enter the real and imaginary parts of the first complex number: "); scanf("%f %f", &num1.real, &num1.imag); // 输入第二个复数的实部和虚部 printf("Enter the real and imaginary parts of the second complex number: "); scanf("%f %f", &num2.real, &num2.imag); // 进行四则运算 Complex sum = add(num1, num2); Complex difference = subtract(num1, num2); Complex product = multiply(num1, num2); Complex quotient = divide(num1, num2); // 输出结果 printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag); printf("Difference: %.2f + %.2fi\n", difference.real, difference.imag); printf("Product: %.2f + %.2fi\n", product.real, product.imag); printf("Quotient: %.2f + %.2fi\n", quotient.real, quotient.imag); return 0; } ``` 这个示例代码演示了如何定义一个复数的结构体,并进行加法、减法、乘法和除法运算。用户可以输入两个复数的实部和虚部,然后程序会输出运算结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值