数式/方程式をプログラムに送信して、結果の答えを得ることができるようにしたいと考えています。操作する数値をデコードするために、正規表現を使用しています。これは追加のための私の現在の正規表現コードです:
Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
これは の 2 番目の数値に対してはうまく機能しますが、演算子記号の前の最後の数値のみを返しますm.group(2)
。m.group(1)
私は何を間違っていますか?
編集:これらは3つの解決方法であり、私がテストしていたテスト出力の一部があります。
public double solve(String e) {
if (!validExpression(e))
return 0;
e = e.replace(" ", "");
boolean doPar = false;
if (e.contains("(") || e.contains(")")) {
doPar = true;
int par = 0;
for (char c : e.toCharArray()) {
if (c == '(')
par++;
else if (c == ')')
par--;
}
if (par != 0)
return 0;
}
if (doPar)
e = solveParentheses(e);
e = solveSmallExpression(e);
return Double.parseDouble(e.replace("(", "").replace(")", ""));
}
private static String solveSmallExpression(String e) {
System.out.println(e);
while(e.contains("^")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\^\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Exponentiation.raise(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
while(e.contains("/") || e.contains("*")) {
if(e.contains("/")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*/\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Division.divide(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
if(e.contains("*")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,}+)\\s*\\*\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Multiplication.multiply(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
}
while(e.contains("-") || e.contains("+")) {
if(e.contains("-")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\-\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Subtraction.subtract(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
if(e.contains("+")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
}
System.out.println(e);
System.out.println();
return e;
}
private static String solveParentheses(String e) {
while (e.contains("(") && e.contains(")")) {
int start = -1;
int end = -1;
for(int i = 0; i < e.length(); i++) {
char c = e.charAt(i);
if(c == '(')
start = i + 1;
else if(c == ')')
end = i;
}
String sub = e.substring(start, end);
if(sub.contains("(") && sub.contains(")"))
sub = solveParentheses(sub);
sub = solveSmallExpression(sub);
e = e.replace(e.subSequence(start - 1, end + 1), sub);
}
return e;
}
private static boolean validExpression(String e) {
for (char c : e.toCharArray()) {
if (!Character.isDigit(c) && c != '(' && c != ')' && c != '^' && c != '+' && c != '-' && c != '/' && c != '*' && c != ' ')
return false;
}
return true;
}
これを印刷すると:
System.out.println((new ExpressionSolver()).solve("(24 * 100) - (24 / 12)"));
これは出力です:
24/12
4 12
0
24*100
4 100
400
400-0
0 0
0
0.0