Java作业 --- 复数的加减乘除运算

老师布置了一项作业,实现复数的加减乘除运算,本来想偷个懒,到C站搜个现成代码交上去,然后找了很多找不到心仪的,最后自己写了一个。。。。。。。。。。。。。。。。。。。。。。。。。内容仅供参考。
运行的话会先提示你先选择加减乘除运算 输入汉字 加减乘除 或者字符’+’ ‘-’ ‘*’ '/'都可以。
如果输入别的会让重新输入。
接着提示你输入第一个数的实部和虚部 第二个数的实部和虚部 这里考虑了用户可能输入的不是数字(try/catch )。
当然如果除以零也考虑到了。
代码方面我认为大部分可能出现的问题都考虑到了,人性化方面我觉得做的也不错。希望喜欢的给个三连支持一下。
别问我为什么很多方法没用到,问就是idea自己生成的。

import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author 小绿龙
 * @date 2022/5/1 - 19:59
 */
public class Test extends PluralCalculation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Plural p1 = new Plural();
        Plural p2 = new Plural();
        Plural result = new Plural();

        while (true){
            Boolean b = true;
            System.out.println("请输入加/ 减 /乘 /除: ");
            String s1 = scanner.nextLine();
            switch (s1){
                case "加":
                case "+":
                    PluralCalculation.data(p1,p2);
                    result = PluralCalculation.addition(p1, p2);
                    break;
                case "减":
                case "-":
                    PluralCalculation.data(p1,p2);
                    result = PluralCalculation.subtraction(p1,p2);
                    break;
                case "乘":
                case "*":
                    PluralCalculation.data(p1,p2);
                    result = PluralCalculation.multiplication(p1,p2);
                    break;
                case "除":
                case "/":
                    PluralCalculation.data(p1,p2);
                    if(p2.getReal_part() == 0 && p2.getImaginary_part() == 0){
                        System.out.println("除以0???");
                        b = false;
                        break;
                    }
                    result = PluralCalculation.division(p1,p2);
                    break;
                default:
                    b = false;
                    break;
            }
            if(b){
                System.out.print("结果是:");
                System.out.println(result);
                break;
            }
        }
    }
}
class Plural{
    private double real_part;
    private double imaginary_part;

    public Plural() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Plural plural = (Plural) o;
        return Double.compare(plural.real_part, real_part) == 0 && Double.compare(plural.imaginary_part, imaginary_part) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(real_part, imaginary_part);
    }

    public Plural(double real_part, double imaginary_part) {
        this.real_part = real_part;
        this.imaginary_part = imaginary_part;
    }

    public double getReal_part() {
        return real_part;
    }
    @Override
    public String toString() {
        if(imaginary_part < 0){
            return "计算结果为:" + real_part + imaginary_part + "i";
        }
        return "计算结果为:" + real_part + " + " + imaginary_part + "i";
    }

    public void setReal_part(double real_part) {
        this.real_part = real_part;
    }

    public double getImaginary_part() {
        return imaginary_part;
    }

    public void setImaginary_part(double imaginary_part) {
        this.imaginary_part = imaginary_part;
    }
}
class PluralCalculation{
    //给p1,p2赋值
    public static void data(Plural p1,Plural p2){
        Scanner scanner;
        while (true){
            scanner = new Scanner(System.in);
            System.out.println("请输入第一个数字的实部:");
            try {
                p1.setReal_part(scanner.nextDouble());
                break;
            } catch (InputMismatchException e) {
                System.out.println("检查你输入的是数字吗?");
            }
        }
        while (true){
            scanner = new Scanner(System.in);
            System.out.println("请输入第一个数字的虚部:");
            try {
                p1.setImaginary_part(scanner.nextDouble());
                break;
            } catch (InputMismatchException e) {
                System.out.println("检查你输入的是数字吗?");
            }
        }
        while (true){
            scanner = new Scanner(System.in);
            System.out.println("请输入第二个数字的实部:");
            try {
                p2.setReal_part(scanner.nextDouble());
                break;
            } catch (InputMismatchException e) {
                System.out.println("检查你输入的是数字吗?");
            }
        }
        while (true){
            scanner = new Scanner(System.in);
            System.out.println("请输入第二个数字的虚部:");
            try {
                p2.setImaginary_part(scanner.nextDouble());
                break;
            } catch (InputMismatchException e) {
                System.out.println("检查你输入的是数字吗?");
            }
        }

    }
    //加法
    public static Plural addition(Plural p1,Plural p2){
        Plural result = new Plural();
        double a = p1.getReal_part();
        double b = p1.getImaginary_part();
        double c = p2.getReal_part();
        double d = p2.getImaginary_part();
        //实部
        result.setReal_part(a + c);
        //虚部
        result.setImaginary_part(b + d);
        return result;
    }
    //减法
    public static Plural subtraction(Plural p1,Plural p2){
        Plural result = new Plural();
        double a = p1.getReal_part();
        double b = p1.getImaginary_part();
        double c = p2.getReal_part();
        double d = p2.getImaginary_part();
        //实部
        result.setReal_part(a - c);
        //虚部
        result.setImaginary_part(b - d);

        return result;
    }
    //乘法
    public static Plural multiplication(Plural p1,Plural p2){
        Plural result = new Plural();
        double a = p1.getReal_part();
        double b = p1.getImaginary_part();
        double c = p2.getReal_part();
        double d = p2.getImaginary_part();
        //实部
        result.setReal_part((a * c) - (b * d));
        //虚部
        result.setImaginary_part((b * c) + (a * d));

        return result;
    }
    //除法
    public static Plural division(Plural p1,Plural p2){
        Plural result = new Plural();
        double a = p1.getReal_part();
        double b = p1.getImaginary_part();
        double c = p2.getReal_part();
        double d = p2.getImaginary_part();
        //实部
        result.setReal_part(((a * c) + (b * d)) / ((c * c) + (d * d)));
        //虚部
        result.setReal_part(((b * c) + (a * d)) / ((c * c) + (d * d)));

        return result;
    }
}

你可能感兴趣的:(简单基础,java)