IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 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" + ")");
    }
}

在这里插入图片描述

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-09-24 10:37:42  更:2021-09-24 10:38:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 22:47:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码