Java声明复数类Complex

声明复数类Complex,成员变量包括实部和虚部,成员方法包括实现由字符串构造复数、复数加法、减法,字符串描述、比较相等、计算复数的模等操作。
百度百科—对复数的介绍

import java.util.Scanner;
public class Complex {  //z=a+bi;
    private double real;
    private double image;

    public Complex(double real,double image){
        this.real = real;
        this.image = image;
    }
    public Complex(String s){  //字符串构造复数1+2i/2/3i/a-i/a+i
        s = s.trim();  //去除多余空格
        int op = -1;  //存储运算符的位置
        int i_index = -1;  //存储i的位置
        int len = s.length();
        String a = "";  //实部
        String b = "";  //虚部
        if (s!=null&&!"".equals(s)){
            for (int i=0;i<len;i++){
                if (s.charAt(i) == '+'||s.charAt(i) == '-')  op = i;  //记录实部虚部连接号
                if (s.charAt(i) == 'i')  i_index = i;  //记录虚部后面的i
            }
            if(i_index==-1){  //不含i,只有实部【不能加上op的判断,实部有可能为-】
                a = s;
                b = "0";
            }else if (op==-1&&i_index!=-1){
                if (i_index==0){  //i
                    a = "0";
                    b = "1";
                }else{  //2i
                    a = "0";
                    b = s.substring(0,i_index);
                }
            }else if (op==0&&i_index!=1){  //-i
                    if (i_index==1){
                        a = "0";
                        b = "-1";
                    }else{
                        a = "0";
                        b = s.substring(0,i_index);
                    }
            }else if (i_index - op==1){  //虚部系数为1或-1
                a = s.substring(0,op);
                b = s.charAt(op) + "1";
                if ("".equals(a)) a = "0";
            }else if (i_index!=-1&&op!=-1){  //含i,不含op,实部为正且不含实部
                a = s.substring(0,op);
                b = s.substring(op,i_index);
                if ("".equals(a)) a = "0";
                if ("".equals(b)) b = "0";
            }//由于将实部与虚部分离的时候,将各自前面的正负号也一并加到了各自的字符串中,所以在使用Double.valueOf()将字符串转化为double类型的时候,
            Set(Double.valueOf(a),Double.valueOf(b));//已经考虑的正负号,故在此没有对正负做情况区分
        }
    }
    
    public void Set(double a,double b){
        this.real = a;
        this.image = b;
    }
    
    public String Add(Complex another){  //复数加法
        double add_x = this.real + another.real;
        double add_y = this.image + another.image;
        return new Complex(add_x,add_y).toString();
    }
    
    public String Sub(Complex another){  //复数减法
        double sub_x = this.real - another.real;
        double sub_y = this.image - another.image;
        return new Complex(sub_x,sub_y).toString();
    }
    
    public void Model(Complex another){  //复数的模
        double x = this.real - another.real;
        double y = this.image - another.image;
        System.out.println("复数的模长为:"+String.format("%.2f",Math.sqrt(x*x+y*y)));
    }
    
    public String toString(){  //字符串描述
        if (this.getReal()==0&&this.getImage()==0){
            return "z = 0.0";
        }
        if (this.getReal() == 0){  //只有虚部
            return "z = "+this.getImage()+"i";
        }
        if (this.getImage() == 0){  //只有实部
            return "z = "+String.valueOf(this.getReal());
        }
        if (this.getImage() < 0){  //虚部为负
            return "z = "+this.getReal()+"+("+this.getImage()+"i)";
        }
        if (this.getImage() > 0){  //虚部为正
            return "z = "+this.getReal()+"+"+this.getImage()+"i";
        }
        return null;
    }
    
    public boolean equals(Complex another){  //比较相等
        return this==another||another!=null&&this.real==another.real&&this.image==another.image;
    }
    
    public double getReal() {
        return real;
    }
    
    public double getImage() {
        return image;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个复数:");
        String s1 = sc.next();
        System.out.println("请输入第二个复数:");
        String s2 = sc.next();
        Complex z1 = new Complex(s1);
        Complex z2 = new Complex(s2);
        while(true){
            System.out.println("1.计算加法\t2.计算减法\t3.计算模长\t4.比较是否相等\t5.exit");
            int choice = sc.nextInt();
            switch (choice){
                case 1:System.out.println(z1.Add(z2));break;
                case 2:System.out.println(z1.Sub(z2));break;
                case 3:z1.Model(z2);break;
                case 4:
                {
                    if (z1.equals(z2)) System.out.println("Yes");
                    else System.out.println("No");
                    break;
                }
                case 5:return ;
            }
        }
    }
}

你可能感兴趣的:(Java【大一下】)