实验9-4 计算两个复数之积 (15分)

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

函数接口定义:

struct complex multiply(struct complex x, struct complex y);

其中struct complex是复数结构体,其定义如下:

struct complex{
    int real;
    int imag;
};

裁判测试程序样例:

#include <stdio.h>

struct complex{
    int real;
    int imag;
};

struct complex multiply(struct complex x, struct complex y);

int main()
{
    struct complex product, x, y;

    scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
    product = multiply(x, y);
    printf("(%d+%di) * (%d+%di) = %d + %di\n", 
            x.real, x.imag, y.real, y.imag, product.real, product.imag);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38i
题目集全集传送门

struct complex multiply(struct complex x, struct complex y)
{
    struct complex result;
    result.real = x.real * y.real - x.imag * y.imag;
    result.imag = x.real * y.imag + x.imag * y.real;
    return result;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java复数类的实现及运算实验总结: 1. 复数类的实现 为了表示复数,我们可以创建一个名为Complex的类。该类应该有两个实例变量:一个表示实部,另一个表示虚部。我们还需要实现一些方法来执行基本的数学运算,如加法、减法、乘法和除法。 下面是一个简单的Java复数类的实现: ```java public class Complex { private double real; private double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public Complex add(Complex other) { double newReal = this.real + other.real; double newImaginary = this.imaginary + other.imaginary; return new Complex(newReal, newImaginary); } public Complex subtract(Complex other) { double newReal = this.real - other.real; double newImaginary = this.imaginary - other.imaginary; return new Complex(newReal, newImaginary); } public Complex multiply(Complex other) { double newReal = this.real * other.real - this.imaginary * other.imaginary; double newImaginary = this.real * other.imaginary + this.imaginary * other.real; return new Complex(newReal, newImaginary); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double newReal = (this.real * other.real + this.imaginary * other.imaginary) / denominator; double newImaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator; return new Complex(newReal, newImaginary); } @Override public String toString() { return String.format("%.2f + %.2fi", real, imaginary); } } ``` 2. 复数运算实验 我们可以编写一些简单的测试来验证我们的复数类是否运行良好。例如,我们可以创建两个复数并将它们相加: ```java public static void main(String[] args) { Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex result = c1.add(c2); System.out.println(result); } ``` 输出结果应该为“4.00 + 6.00i”。 我们还可以测试减法、乘法和除法: ```java public static void main(String[] args) { Complex c1 = new Complex(1, 2); Complex c2 = new Complex(3, 4); Complex addResult = c1.add(c2); Complex subtractResult = c1.subtract(c2); Complex multiplyResult = c1.multiply(c2); Complex divideResult = c1.divide(c2); System.out.println("Addition: " + addResult); System.out.println("Subtraction: " + subtractResult); System.out.println("Multiplication: " + multiplyResult); System.out.println("Division: " + divideResult); } ``` 输出结果应该为: ``` Addition: 4.00 + 6.00i Subtraction: -2.00 - 2.00i Multiplication: -5.00 + 10.00i Division: 0.44 + 0.08i ``` 3. 总结 我们已经成功地实现了一个Java复数类,并测试了它的基本数学运算。通过创建一个复数类,我们可以更轻松地进行复数计算,并且可以更容易地将复数与其他代码集成。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Re:从零开始的代码生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值