Java program to extract coefficents from quadratic equation
Problem: Java program to split the coefficients from a quadratic equation eg if input string is:
String str1;
str1 = "4x2-4x-42=0"
So I need to split the coefficients from the given input string and to get output as
a = 4 b = -4 c = -42
I tried this:
String equation = "ax2+bx-c=0";
String[] parts = equation.split("+|-|=");
for (int i = 0; i < parts.length - 2; i++) {
String part = parts[i].toLowerCase();
System.out.println(part.substring(0, part.indexOf("x")));
}
System.out.println(parts[2]);
But I got the output as 23x2 and 4x and 4. Actual output needed is 23 ,- 4 , 4
.
Use Regex, the following pattern will work:
([+-]?d+)[Xx]2s*([+-]?d+)[Xx]s*([+-]?d+)s*=s*0
This will match the quadratic and extract the parameters, lets work out how it works:
(...)
this is capturing group [+-]?d+
this matches a number of digits, preceded optionally by a +
or -
[Xx]
this matches "X" or "x" s*
this matches zero or more spaces So
([+-]?d+)
matches the "a" argument [Xx]2
matches "X2" or "x2" s*
matches optional whitespace ([+-]?d+)
matches the "b" argument [Xx]
matches "X" or "x" s*
matches optional whitespace ([+-]?d+)
matches the "c" argument s*=s*0
matches "=0" with some optional spaces Lets wrap this in a class
:
private static final class QuadraticEq {
private static final Pattern EQN = Pattern.compile("([+-]?d+)[Xx]2s*([+-]?d+)[Xx]s*([+-]?d+)s*=s*0");
private final int a;
private final int b;
private final int c;
private QuadraticEq(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public static QuadraticEq parseString(final String eq) {
final Matcher matcher = EQN.matcher(eq);
if (!matcher.matches()) {
throw new IllegalArgumentException("Not a valid pattern " + eq);
}
final int a = Integer.parseInt(matcher.group(1));
final int b = Integer.parseInt(matcher.group(2));
final int c = Integer.parseInt(matcher.group(3));
return new QuadraticEq(a, b, c);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("QuadraticEq{");
sb.append("a=").append(a);
sb.append(", b=").append(b);
sb.append(", c=").append(c);
sb.append('}');
return sb.toString();
}
}
Note the , this is required by Java.
A quick test:
System.out.println(QuadraticEq.parseString("4x2-4x-42=0"));
Output:
QuadraticEq{a=4, b=-4, c=-42}
You can use a regex as follows:
final String regex = "([+-]?d+)x2([+-]d+)x([+-]d+)=0";
Pattern pattern = Pattern.compile(regex);
final String equation = "4x2-4x-42=0";
Matcher matcher = pattern.matcher(equation);
if (matcher.matches()) {
int a = Integer.parseInt(matcher.group(1));
int b = Integer.parseInt(matcher.group(2));
int c = Integer.parseInt(matcher.group(3));
System.out.println("a=" + a + " b=" + b + " c=" + c);
}
Output:
a=4 b=-4 c=-42
If you are only using quadratics:
int xsqrd = equation.indexOf("x2");
int x = equation.indexOf("x", xsqrd);
int equ = equation.indexOf("=");
String a = equation.subString(0,xsqrd);
String b = equation.subString(xsqrd+1,x);
String c = equation.subString(x,equ);
I may have messed up the substrings but you get the general idea.
链接地址: http://www.djcxy.com/p/84530.html上一篇: 从nodejs调用postgres函数或存储过程的最佳/正确方法是什么?
下一篇: Java程序从二次方程中提取系数