Search code examples
javaparsingmathquadratic

parsing a quadratic equation in java


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

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.


Solution

  • 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]

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