问题:Java程序将系数从二次方程中分离,例如 如果输入字符串是:
String str1;
str1 = "4x2-4x-42=0"
所以我需要从给定的输入字符串中分割系数并将输出作为
输出a = 4 b = -4 c = -42
我试过了:
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]);
但我的输出为23x2和4x和4。
所需的实际输出为23 ,- 4 , 4
。
答案 0 :(得分:2)
使用Regex,以下模式将起作用:
([+-]?\d+)[Xx]2\s*([+-]?\d+)[Xx]\s*([+-]?\d+)\s*=\s*0
这将匹配二次方并提取参数,让我们弄清楚它是如何工作的:
(...)
这是捕获群组[+-]?\d+
这匹配了多个数字,前面可选+
或-
[Xx]
这匹配&#34; X&#34;或&#34; x&#34; \s*
匹配零个或多个空格所以
([+-]?\d+)
匹配&#34; a&#34;参数[Xx]2
匹配&#34; X2&#34;或&#34; x2&#34; \s*
匹配可选空格([+-]?\d+)
匹配&#34; b&#34;参数[Xx]
匹配&#34; X&#34;或&#34; x&#34; \s*
匹配可选空格([+-]?\d+)
匹配&#34; c&#34;参数\s*=\s*0
匹配&#34; = 0&#34;有一些可选空格让我们将其包装在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();
}
}
请注意\\
,这是Java所必需的。
快速测试:
System.out.println(QuadraticEq.parseString("4x2-4x-42=0"));
输出:
QuadraticEq{a=4, b=-4, c=-42}
答案 1 :(得分:0)
如果您只使用样方:
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);
我可能搞砸了子串,但你得到了一般的想法。
答案 2 :(得分:0)
您可以按如下方式使用正则表达式:
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);
}
输出:
a=4 b=-4 c=-42
答案 3 :(得分:0)
第一个注意事项:如果在regexp中使用符号作为分隔符,则会在slpitted元素中丢失它。我建议你使用以下正则表达式:
"x2|x|="
然后,你只能获得数字。 完整的代码片段是:
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]));
}
}
}
但在这种情况下,输出中会有'+'符号。为避免这种情况,您可以使用Integer.parseInt(parts[i])
代替parts[i]
。
答案 4 :(得分:0)
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