[英]Java program to extract coefficents from quadratic equation
Problem: Java program to split the coefficients from a quadratic equation eg if input string is: 问题:Java程序将系数从二次方程中分离,例如,如果输入字符串是:
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
. 但我得到的输出为23x2和4x和4.实际需要的输出是
23 ,- 4 , 4
。
Use Regex, the following pattern will work: 使用Regex,以下模式将起作用:
([+-]?\d+)[Xx]2\s*([+-]?\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 -
[+-]?\\d+
这匹配了多个数字,前面可选地加一个+
或-
[Xx]
this matches "X" or "x" [Xx]
这匹配“X”或“x” \\s*
this matches zero or more spaces \\s*
这匹配零个或多个空格 So 所以
([+-]?\\d+)
matches the "a" argument ([+-]?\\d+)
匹配“a”参数 [Xx]2
matches "X2" or "x2" [Xx]2
匹配“X2”或“x2” \\s*
matches optional whitespace \\s*
匹配可选的空格 ([+-]?\\d+)
matches the "b" argument ([+-]?\\d+)
匹配“b”参数 [Xx]
matches "X" or "x" [Xx]
匹配“X”或“x” \\s*
matches optional whitespace \\s*
匹配可选的空格 ([+-]?\\d+)
matches the "c" argument ([+-]?\\d+)
匹配“c”参数 \\s*=\\s*0
matches "=0" with some optional spaces \\s*=\\s*0
将“= 0”与一些可选空格匹配 Lets wrap this in a class
: 让我们把它包装在一个
class
:
private static final class QuadraticEq {
private static final Pattern EQN = Pattern.compile("([+-]?\\d+)[Xx]2\\s*([+-]?\\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. 注意
\\\\
,这是Java所必需的。
A quick test: 快速测试:
System.out.println(QuadraticEq.parseString("4x2-4x-42=0"));
Output: 输出:
QuadraticEq{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. 我可能搞乱了子串,但你得到了一般的想法。
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
The first note: if you use symbol as delimiter in regexp, you will lose it in slpitted elements. 第一个注意事项:如果在regexp中使用符号作为分隔符,则会在slpitted元素中丢失它。 I suggest you use the folllowing regexp:
我建议你使用以下正则表达式:
"x2|x|="
Then, you can got only numbers. 然后,你只能得到数字。 The full fragment of code is:
完整的代码片段是:
public class Main {
private static final char[] varNames = {'a', 'b', 'c'};
public static void main(String[] args) {
String equation = "4x2-4x-42=0";
String[] parts = equation.split("x2|x|=");
// you will get 4 elements, but the last element is always 0
for(int i=0; i<parts.length - 1; i++){
System.out.println(varNames[i] + " = " + Integer.parseInt(parts[i]));
}
}
}
But in this case you will have the '+' symbols in output. 但在这种情况下,输出中将包含“+”符号。 To avoid it, you may use
Integer.parseInt(parts[i])
instead of parts[i]
. 为了避免它,您可以使用
Integer.parseInt(parts[i])
而不是parts[i]
。
for ( int i = 0 ; i < str.length ; ++i ){
if(asciiOf(str.charAt(i)) == asciiOfIntegerValue ){
addCharInArrayList(str.charAt(i));
}else{
addInFuncList();
addCharInArrayList("_");
}
// join numbers which are not separated by _ and apply BODMAS rule and solve it
// fyi : apologies - very inefficient pseudocode, wrote in a haste
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.