Quadratic Reading Method

I need to write a reading method for a quadratic class into which the form ax ^ 2 + bx + c is quadratically introduced. The description for the class is as follows:

Add a reading method that asks the user for an equation in a standard format and sets the three instance variables correctly. (Therefore, if the user enters 3x ^ 2 - x, you set the instance variables to 3, -1, and 0). This will require the string handling you did previously. Display the actual equation, entered as is, and correctly labeled as the expected result.

I was able to make the ax ^ 2 part using string manipulation and if else. But I'm not sure how to make bx and c parts of the equation because of the sign that may be before bx and c. This is how I made an ax ^ 2 of the method part.

public void read() { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a quadratic equation in standard format."); String formula = keyboard.next(); String a = formula.substring(0, formula.indexOf("x^2")); int a2 = Integer.parseInt(a); if (a2 == 0) { System.out.println("a = 0"); } else if (a2 == 1) { System.out.println("a = 1"); } else { System.out.println("a = " + a2); } } 

Feel free to write some code as an example. Any help would be greatly appreciated.

+8
java math quadratic
source share
3 answers
 import java.util.regex.Matcher; import java.util.regex.Pattern; public class Mini { public static void main(String[] args) { int a = 0; int b = 0; int c = 0; String formula = " -x^2 + 6x - 5"; formula = formula.replaceAll(" ", ""); if (!formula.startsWith("+") && !formula.startsWith("-")) formula = "+" + formula; String exp = "^((.*)x\\^2)?((.*)x)?([\\+\\s\\-\\d]*)?$"; Pattern p = Pattern.compile(exp); Matcher m = p.matcher(formula); System.out.println("Formula is " + formula); System.out.println("Pattern is " + m.pattern()); while (m.find()) { a = getDigit(m.group(2)); b = getDigit(m.group(4)); c = getDigit(m.group(5)); } System.out.println("a: " + a + " b: " + b + " c: " + c); } private static int getDigit(String data) { if (data == null) { return 0; } else { if (data.equals("+")) { return 1; } else if (data.equals("-")) { return -1; } else { try { int num = (int) Float.parseFloat(data); return num; } catch (NumberFormatException ex) { return 0; } } } } } 
+2
source share

Here is an example of how you could do this using a regex. So far, this works correctly if the equation is given in the format ax ^ 2 + bx + c. It could be changed further so that the order of sub-terms, missing terms, etc. could be changed. For this, I will probably try to work out regular expressions for each subsector. In any case, this should serve as your general idea:

 import java.util.regex.Pattern; import java.util.regex.Matcher; class ParseEquation { static Pattern match = Pattern.compile("([\\+\\-]?[0-9]*)x\\^2([\\+\\-]?[0-9]*)x([\\+\\-]?[0-9]*)"); static String parseEquation(String formula) { // remove all whitespace formula = formula.replaceAll(" ", ""); String a = "1"; String b = "1"; String c = "0"; Matcher m = match.matcher(formula); if (!m.matches()) return "syntax error"; a = m.group(1); if (a.length() == 0) a = "1"; if (a.length() == 1 && (a.charAt(0) == '+' || a.charAt(0) == '-')) a += "1"; b = m.group(2); if (b.length() == 0) b = "1"; if (b.length() == 1 && (b.charAt(0) == '+' || b.charAt(0) == '-')) b += "1"; c = m.group(3); return a + "x^2" + b + "x" + c; } public static void main(String[] args) { System.out.println(parseEquation("2x^2 + 3x - 25")); System.out.println(parseEquation("-2x^2 + 3x + 25")); System.out.println(parseEquation("+2x^2 + 3x + 25")); System.out.println(parseEquation("x^2 + 3x + 25")); System.out.println(parseEquation("2x^2 + x + 25")); } } 
+1
source share

Through regular expressions:

 sub quadParse { my ($inputStr) = @_; my $str = "+".$inputStr; # as the first needn't have a sign $str =~ s/\s+//g; # normalise my $squared = $1 if ($str =~ m/([+-][0-9])*x\^2/); my $ex = $1 if ($str =~ m/([+-][0-9]*)x(?!\^)/); my $const = $1 if ($str =~ m/([+-][0-9]+)(?!x)/); return "${squared}, ${ex}, ${const}"; } 

For parsing Perl strings.

Oh, move on:

 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 quad = coeff(str, "([+-][0-9]*)x\\^2" ); String ex = coeff(str, "([+-][0-9]*)x(?!\\^)"); String cnst = coeff(str, "([+-][0-9]+)(?!x)" ); return new String[] {quad, ex, cnst}; } 

Java test in ideone .

They process the formula in any order, with or without an initial character in the first term, and process the missing terms correctly. The Perl version does not commit "+" to "+1", etc. Or does not give an explicit "0" for missing terms, because I have run out of time.

+1
source share

All Articles