解析java中的二次方程

时间:2022-10-29 20:38:38

I have to write a read method for a quadratic class where a quadratic is entered in the form ax^2 + bx + c = 0 I found this way and here's the code:

我必须为二次类写一个read方法,其中以ax ^ 2 + bx + c = 0的形式输入二次方法我发现这种方式,这里是代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseEquation {
    public static String coeff(String str, String regex) {
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(str);
        // missing coefficient default
        String coeff = "+0"; 
        if(match.find()) 
                coeff = match.group(1);
        // always have sign, handle implicit 1
        return (coeff.length() == 1) ? coeff + "1" 
                : coeff;
    }

    public static String[] quadParse(String arg) {
        String str = ("+" + arg).replaceAll("\\s", "");

        String a = coeff(str, "([+-][0-9]*)x\\^2" );
        String b = coeff(str, "([+-][0-9]*)x(?!\\^)");
        String c = coeff(str, "([+-][0-9]+)(?!x)" );
        double a1 = Double.parseDouble(a);
        double b1 = Double.parseDouble(b);
        double c1 = Double.parseDouble(c);

        double dis = (Math.pow(b1, 2.0)) - (4 * a1 * c1);
        double d = Math.sqrt(dis);
        double X = 0,Y = 0; //root 1 & root 2, respectively

        if (dis > 0.0 || dis < 0.0 ) {
             X = (-b1 + d)/(2.0 * a1 );
             Y = (-b1 - d)/(2.0 *a1); 
             String root1 = Double.toString(X);
             String root2 = Double.toString(Y);
             return new String[]{root1,root2};
         } else if (dis == 0.0){
            X = (-b1 + 0.0)/(2.0 * a1);//repeated root
            String root2 = Double.toString(X);
            return new String[]{root2}; 
         }

         return new String[-1];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader (new InputStreamReader (System.in));
        String s;
        while ((s=r.readLine()) != null) {
            String[] pieces = quadParse(s);
            System.out.println(Arrays.toString(pieces));
        }
    }
}

this already solve normal 2nd degree equation ax^2 + bx + c = 0 ( any equation that user enter like "2x^2 + 2x -25 =0" for example) and solve it with changing the places of the roots either but I can't know how to solve equation that have similar roots like " 2x^2 + 2x -3x -25 +15 =0 " in this it should first sum the two coefficients of x and sum (-25+15 ) then calculate the result. So I need to know the way to know how to wrote code to do that.

这已经解决了正常的二次方程ax ^ 2 + bx + c = 0(用户输入的任何方程式,例如“2x ^ 2 + 2x -25 = 0”),并通过改变根的位置解决它,但我不知道如何解决具有相似根的方程,如“2x ^ 2 + 2x -3x -25 +15 = 0”,在此应首先求和x和和的两个系数(-25 + 15)然后计算结果。所以我需要知道如何编写代码来实现这一目标的方法。

Feel free to write any code as an example.

随意编写任何代码作为示例。

2 个解决方案

#1


I think you need to continuously add the values for x^2 and x. I have modified the code and it seems to be working fine:

我认为你需要不断添加x ^ 2和x的值。我修改了代码,似乎工作正常:

public class ParseEquation {
    public static double coeff(String str, String regex) {
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(str);
        // missing coefficient default
        String coeff = "+0";
        double value = 0;
        while(match.find()){
            coeff = match.group(1);
            value = value + Double.parseDouble(coeff);
        }
        // always have sign, handle implicit 1
        return (coeff.length() == 1) ? (value + 1) : value;
    }
    public static String[] quadParse(String arg) {
        String str = ("+" + arg).replaceAll("\\s", "");

        double a1 = coeff(str, "([+-][0-9]*)x\\^2");
        double b1 = coeff(str, "([+-][0-9]*)x(?!\\^)");
        double c1= coeff(str, "([+-][0-9]+)(?!x)");
        System.out.println("Values are a: " + a1 + " b: " + b1 + " c: " + c1);
        double dis = (Math.pow(b1, 2.0)) - (4 * a1 * c1);
        double d = Math.sqrt(dis);
        double X = 0, Y = 0; //root 1 & root 2, respectively

        if (dis > 0.0 || dis < 0.0) {
            X = (-b1 + d) / (2.0 * a1);
            Y = (-b1 - d) / (2.0 * a1);
            String root1 = Double.toString(X);
            String root2 = Double.toString(Y);
            return new String[]{root1, root2};
        } else if (dis == 0.0) {
            X = (-b1 + 0.0) / (2.0 * a1);//repeated root
            String root2 = Double.toString(X);
            return new String[]{root2};
        }
        return new String[-1];
    }
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
        String s;
        while ((s=r.readLine()) != null) {
            String[] pieces = quadParse(s);
            System.out.println(Arrays.toString(pieces));
        }
    }
}

And here is the output when I run the program:

这是我运行程序时的输出:

2x^2 + 2x -3x -25 +15 =0 Values are a: 2.0 b: -1.0 c: -10.0 [2.5, -2.0]

2x ^ 2 + 2x -3x -25 +15 = 0值为a:2.0 b:-1.0 c:-10.0 [2.5,-2.0]

So it is able to sum the coefficients correctly. I have not changed the logic written by you which IMO should be working fine.

因此它能够正确地对系数求和。我没有改变你所写的逻辑,IMO应该正常工作。

#2


final answer after edit to make it handle implicit 1

编辑后的最终答案使其处理隐式1

ex: x^2-3x-2x-25

Values are a: 1.0 b: -5.0 c: -25.0

值为:1.0b:-5.0c:-25.0

[8.090169943749475, -3.0901699437494745]

public class ParseEquation {
public static String coeff(String str, String regex) {
    Pattern patt = Pattern.compile(regex);
    Matcher match = patt.matcher(str);
    // missing coefficient default
    String coeff = "+0";
    double value = 0;

   if(match.find()) 
        coeff = match.group(1);
    // always have sign, handle implicit 1
    value= Double.parseDouble((coeff.length() == 1) ? coeff + "1" 
        : coeff);

    while(match.find()){

        coeff = match.group(1);
        value = value + Double.parseDouble(coeff);
    }
    String value2 =String.valueOf(value);
    return (value2.length() == 1) ? (value2 + "1") : value2;
}
public static String[] quadParse(String arg) {
    String str = ("+" + arg).replaceAll("\\s", "");

    double a1 = Double.parseDouble(coeff(str, "([+-][0-9]*)([a-z]\\^2)"));
    double b1 = Double.parseDouble(coeff(str, "([+-][0-9]*)([a-z](?!\\^))"));
    double c1= Double.parseDouble(coeff(str, "([+-][0-9]+)(?![a-z])"));
    System.out.println("Values are a: " + a1 + " b: " + b1 + " c: " + c1);
    double dis = (Math.pow(b1, 2.0)) - (4 * a1 * c1);
    double d = Math.sqrt(dis);
    double X = 0, Y = 0; //root 1 & root 2, respectively

    if (dis > 0.0 || dis < 0.0) {
        X = (-b1 + d) / (2.0 * a1);
        Y = (-b1 - d) / (2.0 * a1);
        String root1 = Double.toString(X);
        String root2 = Double.toString(Y);
        return new String[]{root1, root2};
    } else if (dis == 0.0) {
        X = (-b1 + 0.0) / (2.0 * a1);//repeated root
        String root2 = Double.toString(X);
        return new String[]{root2};
    }
    return new String[-1];
}
public static void main(String[] args) throws IOException {
    BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
    String s;
    while ((s=r.readLine()) != null) {
        String[] pieces = quadParse(s);
        System.out.println(Arrays.toString(pieces));
    }
}
}

#1


I think you need to continuously add the values for x^2 and x. I have modified the code and it seems to be working fine:

我认为你需要不断添加x ^ 2和x的值。我修改了代码,似乎工作正常:

public class ParseEquation {
    public static double coeff(String str, String regex) {
        Pattern patt = Pattern.compile(regex);
        Matcher match = patt.matcher(str);
        // missing coefficient default
        String coeff = "+0";
        double value = 0;
        while(match.find()){
            coeff = match.group(1);
            value = value + Double.parseDouble(coeff);
        }
        // always have sign, handle implicit 1
        return (coeff.length() == 1) ? (value + 1) : value;
    }
    public static String[] quadParse(String arg) {
        String str = ("+" + arg).replaceAll("\\s", "");

        double a1 = coeff(str, "([+-][0-9]*)x\\^2");
        double b1 = coeff(str, "([+-][0-9]*)x(?!\\^)");
        double c1= coeff(str, "([+-][0-9]+)(?!x)");
        System.out.println("Values are a: " + a1 + " b: " + b1 + " c: " + c1);
        double dis = (Math.pow(b1, 2.0)) - (4 * a1 * c1);
        double d = Math.sqrt(dis);
        double X = 0, Y = 0; //root 1 & root 2, respectively

        if (dis > 0.0 || dis < 0.0) {
            X = (-b1 + d) / (2.0 * a1);
            Y = (-b1 - d) / (2.0 * a1);
            String root1 = Double.toString(X);
            String root2 = Double.toString(Y);
            return new String[]{root1, root2};
        } else if (dis == 0.0) {
            X = (-b1 + 0.0) / (2.0 * a1);//repeated root
            String root2 = Double.toString(X);
            return new String[]{root2};
        }
        return new String[-1];
    }
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
        String s;
        while ((s=r.readLine()) != null) {
            String[] pieces = quadParse(s);
            System.out.println(Arrays.toString(pieces));
        }
    }
}

And here is the output when I run the program:

这是我运行程序时的输出:

2x^2 + 2x -3x -25 +15 =0 Values are a: 2.0 b: -1.0 c: -10.0 [2.5, -2.0]

2x ^ 2 + 2x -3x -25 +15 = 0值为a:2.0 b:-1.0 c:-10.0 [2.5,-2.0]

So it is able to sum the coefficients correctly. I have not changed the logic written by you which IMO should be working fine.

因此它能够正确地对系数求和。我没有改变你所写的逻辑,IMO应该正常工作。

#2


final answer after edit to make it handle implicit 1

编辑后的最终答案使其处理隐式1

ex: x^2-3x-2x-25

Values are a: 1.0 b: -5.0 c: -25.0

值为:1.0b:-5.0c:-25.0

[8.090169943749475, -3.0901699437494745]

public class ParseEquation {
public static String coeff(String str, String regex) {
    Pattern patt = Pattern.compile(regex);
    Matcher match = patt.matcher(str);
    // missing coefficient default
    String coeff = "+0";
    double value = 0;

   if(match.find()) 
        coeff = match.group(1);
    // always have sign, handle implicit 1
    value= Double.parseDouble((coeff.length() == 1) ? coeff + "1" 
        : coeff);

    while(match.find()){

        coeff = match.group(1);
        value = value + Double.parseDouble(coeff);
    }
    String value2 =String.valueOf(value);
    return (value2.length() == 1) ? (value2 + "1") : value2;
}
public static String[] quadParse(String arg) {
    String str = ("+" + arg).replaceAll("\\s", "");

    double a1 = Double.parseDouble(coeff(str, "([+-][0-9]*)([a-z]\\^2)"));
    double b1 = Double.parseDouble(coeff(str, "([+-][0-9]*)([a-z](?!\\^))"));
    double c1= Double.parseDouble(coeff(str, "([+-][0-9]+)(?![a-z])"));
    System.out.println("Values are a: " + a1 + " b: " + b1 + " c: " + c1);
    double dis = (Math.pow(b1, 2.0)) - (4 * a1 * c1);
    double d = Math.sqrt(dis);
    double X = 0, Y = 0; //root 1 & root 2, respectively

    if (dis > 0.0 || dis < 0.0) {
        X = (-b1 + d) / (2.0 * a1);
        Y = (-b1 - d) / (2.0 * a1);
        String root1 = Double.toString(X);
        String root2 = Double.toString(Y);
        return new String[]{root1, root2};
    } else if (dis == 0.0) {
        X = (-b1 + 0.0) / (2.0 * a1);//repeated root
        String root2 = Double.toString(X);
        return new String[]{root2};
    }
    return new String[-1];
}
public static void main(String[] args) throws IOException {
    BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
    String s;
    while ((s=r.readLine()) != null) {
        String[] pieces = quadParse(s);
        System.out.println(Arrays.toString(pieces));
    }
}
}