Java实现复数Complex的加减乘除运算、取模、求幅角角度

简介: Java实现复数Complex的加减乘除运算、取模、求幅角角度
/**
 * @Author: Yeman
 * @Date: 2021-09-23-9:03
 * @Description:
 */
class Complex{
    private double realPart;  //复数的实部
    private double imaginaryPart;  //复数的虚部
    public Complex() { //空参构造器
    }
    public Complex(double realPart, double imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }
    //属性的get、set方法
    public double getRealPart() {
        return realPart;
    }
    public void setRealPart(double realPart) {
        this.realPart = realPart;
    }
    public double getImaginaryPart() {
        return imaginaryPart;
    }
    public void setImaginaryPart(double imaginaryPart) {
        this.imaginaryPart = imaginaryPart;
    }
    //加法运算
    public Complex add(Complex otherComplex){
        if (otherComplex != null) {
            return new Complex(this.getRealPart() + otherComplex.getRealPart(),this.getImaginaryPart() + otherComplex.getImaginaryPart());
        }else throw new RuntimeException("参与运算的对象为空!");
    }
    //减法运算
    public Complex decrease(Complex otherComplex){
        if (otherComplex != null) {
            return new Complex(this.getRealPart() - otherComplex.getRealPart(),this.getImaginaryPart() - otherComplex.getImaginaryPart());
        }else throw new RuntimeException("参与运算的对象为空!");
    }
    //乘法运算
    public Complex multiply(Complex otherComplex){
        if (otherComplex != null) {
            double newReal = this.getRealPart() * otherComplex.getRealPart() - this.getImaginaryPart() * otherComplex.getImaginaryPart();
            double newImaginary = this.getImaginaryPart() * otherComplex.getRealPart() + this.getRealPart() * otherComplex.getImaginaryPart();
            return new Complex(newReal,newImaginary);
        }else throw new RuntimeException("参与运算的对象为空!");
    }
    //除法运算
    public Complex divide(Complex otherComplex){
        if (otherComplex != null) {
            if (otherComplex.getRealPart() != 0 && otherComplex.getImaginaryPart() != 0){
                double newReal = (this.getRealPart() * otherComplex.getRealPart() + this.getImaginaryPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());
                double newImaginary = (this.getImaginaryPart() * otherComplex.getRealPart() - this.getRealPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());
                return new Complex(newReal,newImaginary);
            }else throw new RuntimeException("除数不能为0!");
        }else throw new RuntimeException("参与运算的对象为空!");
    }
    //取模
    public double delivery(){
            return Math.sqrt(this.getRealPart() * this.getRealPart() + this.getImaginaryPart() * this.getImaginaryPart());
    }
    //幅度值(角度)
    public double angle(){
        double atan;
        if (this.getRealPart() != 0) { //注意,该处double型变量若有进行其他操作,则不能以此方式判断其等于0,应该是其绝对值小于某个很小的数;而这当前情景下,其实精度问题并不影响,因此可以这样写
            atan = Math.atan(this.getImaginaryPart() / this.getRealPart());
        }else {
            if (this.getImaginaryPart() > 0) {
                atan = Math.PI / 2;
            }else if (this.getImaginaryPart() < 0){
                atan = -Math.PI / 2;
            }else atan = Math.atan(0);
        }
        return atan;
    }
}
//测试主类
public class ComplexTest {
    public static void main(String[] args) {
        Complex complex1 = new Complex(0, 5);
        Complex complex2 = new Complex(3, -3);
        //取模测试
        double delivery = complex1.delivery();
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的模为:" + delivery);
        //求角度测试
        double angle = complex1.angle();
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的角度为:" + Math.toDegrees(angle) + "°");
        //加运算
        Complex add = complex1.add(complex2);
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "+" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + add.getRealPart() + "+" + add.getImaginaryPart() + "i" + ")");
        //减运算
        Complex decrease = complex1.decrease(complex2);
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "-" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + decrease.getRealPart() + "+" + decrease.getImaginaryPart() + "i" + ")");
        //乘法运算
        Complex multiply = complex1.multiply(complex2);
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "x" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + multiply.getRealPart() + "+" + multiply.getImaginaryPart() + "i" + ")");
        //除法运算
        Complex divide = complex1.divide(complex2);
        System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "/" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + divide.getRealPart() + "+" + divide.getImaginaryPart() + "i" + ")");
    }
}


6291447ce64345689fe6a2e3746bf6c5.png




相关文章
|
6月前
|
Java
栈的简单应用(利用Stack进行四则混合运算)(JAVA)
中缀表达式转后缀表达式图解, 代码实现过程, 完整代码, 利用后缀表达式求值, 完整代码
81 0
|
6月前
|
Java
【java作业3——类的定义】复数,连续子数组,最大素数
🍒 1 定义复数类 作业内容: 定义一个复数类Complex(数据成员:a,b表示复数a+bi).并配以合适的方法完成复数对象的生成、复数的运算(加、减、乘除),然后做一个主类测试它。
51 0
|
Java
JAVA运算优先级
JAVA运算优先级
76 0
|
Java
Java不用加减乘除做加法(图文详解)
1.题目描述 2.题解 分析 具体实现
64 0
|
3月前
|
Java
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
这篇文章解释了Java中`int`基本数据类型和其包装类`Integer`之间的区别,并指出在进行`==`运算时,`Integer`会拆箱为`int`类型,然后比较它们的值是否相等。
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
|
2月前
|
Go
Golang语言基础数据类型之复数complex
这篇文章介绍了Go语言中复数(complex)数据类型的概念、表示方法和使用示例,包括complex64和complex128两种类型的复数及其在不同领域的应用场景。
57 5
|
5月前
|
Java
在Java中,你可以创建一个简单的四则运算程序来执行小学级别的加减乘除操作
【6月更文挑战第19天】Java程序实现简单四则运算,接收用户输入的两个数字和运算符,根据运算符调用相应函数进行计算。包含加、减、乘、除功能,其中除法操作检查了除数是否为零,避免运行时错误。
56 5
|
6月前
|
存储 算法 Java
Java赋值运算
Java赋值运算
39 3
|
5月前
|
算法 Java
Java数据结构与算法:位运算之与、或、异或运算
Java数据结构与算法:位运算之与、或、异或运算
|
6月前
|
存储 Java 数据安全/隐私保护
【Java探索之旅】运算符解密 位运算,移位运算
【Java探索之旅】运算符解密 位运算,移位运算
59 0

热门文章

最新文章

下一篇
无影云桌面