java中的quadratics方法显示复杂的根

时间:2015-07-19 19:08:44

标签: java

每个人都这样,我写了一个程序,解决了二次方法(一个双根和两个双根这似乎有效,但不知怎的,我可以解决复杂的根源?任何帮助。

import javax.swing.JOptionPane;
public class QuadMethods {

    static double a=0;
    static double b=0;
    static double c=0;

    public static void main(String[] args) {

        getUserInput(); // gets  values into a, b, and c
        double disc = getTheDiscriminant(a,b,c);
        if (disc ==0) {
            displayDoubledRealRoot(a,b,c);
            //} else if (disc < 0) {
            //displayComplexConjugates();
        } else if (disc > 0) {
            displayUnequalRealRoots(a,b,c);
            //} else {
            //System.out.println("Help! Arithmetic has failed!");
            //
        }

    }
    public static void displayUnequalRealRoots(double a, double b, double c) {
        double disc = getTheDiscriminant(a,b,c);
        double r1 = (-b+Math.sqrt(disc)/ (2*a));
        double r2 = (-b-Math.sqrt(disc)/ (2*a));
        String s = "two roots " + r1 + " and " + r2;
        JOptionPane.showMessageDialog(null, s);

    }
    public static void displayDoubledRealRoot(double a, double b, double c) {
        String s = "One doubled root = " + (-b/(2*a));
        JOptionPane.showMessageDialog(null, s);
    }
    public static double getTheDiscriminant(double a, double b, double c) {
        return b*b - 4*a*c;
    }
    public static void getUserInput() {
        JOptionPane.showMessageDialog(null, "Getting coeffecients for ax^2+bx+c=0");
        a = getANumber("a");
        b = getANumber("b");
        c = getANumber("c");
    }
    public static double getANumber(String p) {

        boolean iDontHaveANumberYet = true;
        double r = 0;
        do {
            try {
                String aStr = JOptionPane.showInputDialog("Enter \"" + p + "\"");
                r = Double.parseDouble(aStr);
                iDontHaveANumberYet = false;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Hey, I can't deal with that.  Must enter legal number.");
            }
        } while (iDontHaveANumberYet);
        return r;
    }

}

1 个答案:

答案 0 :(得分:0)

解决方案:

import javax.swing.JOptionPane;
public class QuadMethods {

    static double a=0;
    static double b=0;
    static double c=0;

    public static void main(String[] args) {

        getUserInput(); // gets  values into a, b, and c
        double disc = getTheDiscriminant(a,b,c);
        if (disc ==0) {
            displayDoubledRealRoot(a,b,c);
        } else if (disc > 0) {
            displayUnequalRealRoots(a,b,c);
        } else {
            // New method
            displayComplexRoots(a,b,c);
        }
    }

    public static void displayUnequalRealRoots(double a, double b, double c) {
        double disc = getTheDiscriminant(a,b,c);
        double r1 = (-b+Math.sqrt(disc)/ (2*a));
        double r2 = (-b-Math.sqrt(disc)/ (2*a));
        String s = "two roots " + r1 + " and " + r2;
        JOptionPane.showMessageDialog(null, s);
    }

    public static void displayDoubledRealRoot(double a, double b, double c) {
        String s = "One doubled root = " + (-b/(2*a));
        JOptionPane.showMessageDialog(null, s);
    }

    public static double getTheDiscriminant(double a, double b, double c) {
        return b*b - 4*a*c;
    }

    // New method
    public static void displayComplexRoots(double a, double b, double c) {
        double disc = 4 * a * c - b * b;
        double dobleA = 2 * a;
        String s = "two roots (" + (-b/dobleA) + "+" + (Math.sqrt(disc)/dobleA) + "i) and ("+ (-b/dobleA) + "" + (-Math.sqrt(disc)/dobleA) + "i)";
        JOptionPane.showMessageDialog(null, s);
    }

    public static void getUserInput() {
        JOptionPane.showMessageDialog(null, "Getting coeffecients for ax^2+bx+c=0");
        a = getANumber("a");
        b = getANumber("b");
        c = getANumber("c");
    }

    public static double getANumber(String p) {

        boolean iDontHaveANumberYet = true;
        double r = 0;
        do {
            try {
                String aStr = JOptionPane.showInputDialog("Enter \"" + p + "\"");
                r = Double.parseDouble(aStr);
                iDontHaveANumberYet = false;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Hey, I can't deal with that.  Must enter legal number.");
            }
        } while (iDontHaveANumberYet);
        return r;
    }
}

复数的一般解决方案:

public class Complex {

    double r;
    double i = 0;

    public Complex(double real, double imaginary) {
        this.r = real;
        this.i = imaginary;
    }

    public Complex(double real) {
        this.r = real;
    }

    public Complex add(Complex c){
        return new Complex(r+c.r, i+c.i);
    }

    public Complex cross(Complex c){
        return new Complex(r*c.r - i*c.i, i*c.r + r*c.i);
    }

    public double getR() {
        return r;
    }

    public double getI() {
        return i;
    }

    @Override
    public String toString() {
        String result = Double.toString(r);
        if (i < 0) {
            result += " - " + ((i != -1)?Double.toString(-i):"") + "i";
        } else if (i > 0) {
            result += " + " + ((i != 1)?Double.toString(i):"") + "i";
        }
            return result;
        }


    public Complex[] squareRoot(){
        double r2 = r * r;
        double i2 = i * i;
        double rR = Math.sqrt((r+Math.sqrt(r2+i2))/2);
        double rI = Math.sqrt((-r+Math.sqrt(r2+i2))/2);
        return new Complex[]{new Complex(rR, rI),new Complex(-rR, rI)};
    }

    public static Complex[] quadraticsRoot(double a, double b, double c) {
        Complex[] result = new Complex(b*b - 4*a*c).squareRoot();
        Complex bNegative = new Complex(-b);
        Complex divisor = new Complex(1.0d / (2 * a));
        for (int j = 0; j < result.length; j++) {
            result[j] = bNegative.add(result[j]).cross(divisor);
        }
        return result;
    }

    public static void main(String[] args) {
         Complex[] sol = quadraticsRoot(1,-10,34);
         System.out.println(sol[0]);
         System.out.println(sol[1]);
    }
}