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.
我认为你需要持续增加值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));
}
}
}
这里是当我运行该程序的输出:
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-3倍-2X-25
值是: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));
}
}
}