聲明Complex類,成員變量包括實部和虛部,成員方法包括實現由字符串構造復數、復數加法、減法,字符串描述、比較相等等操作


package Myjava;
//import java.lang.

//修復一小點輸入不當導致崩潰
import java.util.Scanner;

public class Complex {
    double real ,virt;
    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        var in = new Scanner(System.in);
        Complex matf = new Complex();
        Complex mats = new Complex();
        System.out.print("請輸入第一個復數:");
        String input = in.nextLine();
        matf.toComplex(input);
        System.out.print("請輸入第二個復數:");
        input = in.nextLine();
        mats.toComplex(input);
        Complex temp = new Complex();
        temp.copy(matf);
        System.out.println("模分別為:" + matf.mod() + "  " + mats.mod());
        if(matf.compareMod(mats)) {
            System.out.println("兩數模相等");
        }else {
            System.out.println("兩式模不等");
        }
        if(matf.compareSame(mats)) {
            System.out.println("兩數相等");
        }else {
            System.out.println("兩式不等");
        }
        temp.copy(matf);
        temp.add(mats);
        System.out.println("兩式之和:" + temp.toString());
        temp.copy(matf);
        temp.sub(mats);
        System.out.println("(1):" + matf.toString() + "減 (2):" + mats.toString() + "值為:" + temp.toString());
        temp.copy(mats);
        temp.sub(matf);
        System.out.println("(2):" + mats.toString() + "減 (1):" + matf.toString() + "值為:" + temp.toString());
    }
    public void copy(Complex value) {
        this.real = value.real;
        this.virt = value.virt;
    }
    
    public String toString() {
        String flag = "+";
        if (this.virt < 0) {
            flag = "";
        }
        return (this.real + flag + this.virt + "i");
    }
    
    public void print() {
        String flag = "+";
        if (this.virt < 0) {
            flag = "";
        }
        System.out.println(this.real + flag + this.virt + "i");
    }
    
    public void toComplex(String str) {
        this.real = 0;
        this.virt = 0;
        str = str.replaceAll(" ","");
        //System.out.println("old" + str);
        str = str.toLowerCase();
        //System.out.println("old" + str);
        String temp = str;
        int indexSub = temp.indexOf("-", 1);
        int indexAdd = temp.indexOf("+", 1);
        if(indexSub != -1 || indexAdd != -1) {
            //有兩個部分
            if (indexSub > indexAdd) {
                indexAdd = indexSub;
            }
            //分離虛實兩部分
            str = temp.substring(0,indexAdd);
            temp = temp.substring(indexAdd,temp.length());
            
        }else {
            //只有一個部分
            temp = "0";
        }
        String strs[] = {str,temp};
        //System.out.println("STRS:" + temp + str);
        for (String retval: strs) {
            if((retval.length() == 2 && retval.charAt(1) == 'i') ) {
                retval = retval.charAt(0) + "1i";
            }
            if ((retval.length() == 1 && retval.charAt(0) == 'i')) {
                retval =  "1i";
            }
            if (retval.charAt(retval.length() - 1) == 'i') {
                this.virt = virt + Double.valueOf(retval.substring(0,retval.length() - 1));
            }else {
                this.real = real + Double.valueOf(retval);
            }
        }
        //System.out.print("mat:" + real + "+" + virt + 'i');
    }
    
    
    public void add(Complex value) {
        this.real += value.real;
        this.virt += value.virt;
    }
    
    public void sub(Complex value) {
        this.real -= value.real;
        this.virt -= value.virt;
    }
    
    public boolean compareMod(Complex value) {
        if (this.mod() != value.mod()) {
            return false;
        }
        return true;
    }

    public boolean compareSame(Complex value) {
        if (this.mod() != value.mod()) {
            return false;
        }else if(this.real != value.real){
            return false;
        }
        return true;
    }
    
    public float mod() {
        return (float) Math.sqrt(this.real * real + virt * virt);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM