在Java中解析二次方程 [英] parsing a quadratic equation in java

查看:119
本文介绍了在Java中解析二次方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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)然后计算结果.因此,我需要知道知道如何编写代码来做到这一点的方法.

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.

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

Feel free to write any code as an example.

推荐答案

我认为您需要不断添加x^2x的值.我已经修改了代码,而且看起来工作正常:

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:

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 Values are a: 2.0 b: -1.0 c: -10.0 [2.5, -2.0]

因此它能够正确求和系数.我没有改变您写的IMO应该能正常工作的逻辑.

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

这篇关于在Java中解析二次方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆