簡體   English   中英

在Java中解析二次方程

[英]parsing a quadratic equation in java

我必須為一個二次類編寫一個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));
        }
    }
}

這已經解決了正常的二階方程ax ^ 2 + bx + c = 0(例如,用戶輸入的任何方程式,例如“ 2x ^ 2 + 2x -25 = 0”),並且可以通過改變根的位置來求解,但是我不知道如何求解具有類似根的方程,例如“ 2x ^ 2 + 2x -3x -25 +15 = 0”,因此應首先將x的兩個系數求和,然后求和(-25 + 15),然后計算結果。 所以我需要知道知道如何編寫代碼來做到這一點的方法。

隨意編寫任何代碼作為示例。

我認為您需要不斷將x^2x的值相加。 我已經修改了代碼,而且看起來工作正常:

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));
        }
    }
}

這是我運行程序時的輸出:

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

因此,它能夠正確地求和系數。 我沒有改變您編寫的IMO應該能正常工作的邏輯。

編輯以使其處理隱式后的最終答案1

例如:x ^ 2-3x-2x-25

值是a:1.0 b:-5.0 c:-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));
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM